-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
51 lines (39 loc) · 1.08 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
from itertools import cycle
from time import perf_counter
from os.path import basename
program_name = basename(__file__).rstrip(".py")
with open(f"./input/{program_name}.in") as f:
data = f.read().splitlines()
n_lines = len(data)
data = cycle(data)
def solve_part1():
depth, hor = 0, 0
for _ in range(n_lines):
_dir, step = next(data).split()
step = int(step)
if _dir == "forward":
hor += step
elif _dir == "up":
depth -= step
elif _dir == "down":
depth += step
return depth * hor
def solve_part2():
depth, hor, aim = 0, 0, 0
for _ in range(n_lines):
_dir, step = next(data).split()
step = int(step)
if _dir == "forward":
hor += step
depth += step * aim
elif _dir == "up":
aim -= step
elif _dir == "down":
aim += step
return depth * hor
start = perf_counter()
s1 = solve_part1()
print("Solution 1:", s1)
s2 = solve_part2()
print("Solution 2:", s2)
print("Execution took", perf_counter() - start, "s")