-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
79 lines (64 loc) · 1.95 KB
/
day2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Solution for Advent of Code day 2."""
from pathlib import Path
from typing import Iterator
import doctest
import click
def course_updates(filename: Path) -> Iterator[int]:
"""Generator thats yield each line from a file as a number.
Args:
filename (Path): path to the input file.
Yields:
int: The next number from the file.
Examples:
>>> next(course_updates(Path("test_data/day_2.data")))
(5, 0)
>>> [i for i in course_updates(Path("test_data/day_2.data"))]
[(5, 0), (0, 5), (8, 0), (0, -3), (0, 8), (2, 0)]
"""
with filename.open("r") as file:
for line in file:
if line.startswith("forward"):
yield int(line.strip()[-1]), 0
elif line.startswith("down"):
yield 0, int(line.strip()[-1])
else:
yield 0, -int(line.strip()[-1])
@click.group()
def main():
"""CLI for the solution of day 2
Advent of code 2021 (https://adventofcode.com/2021/day/2)
"""
@main.command()
@click.argument(
"filename",
required=False,
type=Path,
default=Path("test_data/day_2.data"),
)
def part_1(filename: Path):
"""Part one of day two. (position)"""
position = (0, 0)
for horizontal, vertical in course_updates(filename):
position = tuple(sum(x) for x in zip(position, (horizontal, vertical)))
print(position[0] * position[1])
@main.command()
@click.argument(
"filename",
required=False,
type=Path,
default=Path("test_data/day_2.data"),
)
def part_2(filename: Path):
"""Part two of day two. (position & aim)"""
position = (0, 0)
aim = 0
for pos_delta, aim_delta in course_updates(filename):
aim = aim + aim_delta
position = tuple(sum(x) for x in zip(position, (pos_delta, pos_delta * aim)))
print(position[0] * position[1])
@main.command()
def test():
"""run doctest."""
print(doctest.testmod())
if __name__ == "__main__":
main()