-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.py
47 lines (38 loc) · 1.23 KB
/
02.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
#!/usr/bin/env python3
'''Day 2: Dive!
https://adventofcode.com/2021/day/2
'''
from utils.basepuzzle import BasePuzzle
class Puzzle(BasePuzzle):
def part1(self, lines: list[str]) -> int:
commands, units = self.parse_input(lines)
directions = {'forward': 0, 'down': 0, 'up': 0}
for c, u in zip(commands, units):
directions[c] += u
horizontal = directions['forward']
depth = directions['down'] - directions['up']
return horizontal * depth
def part2(self, lines: list[str]) -> int:
commands, units = self.parse_input(lines)
horizontal = 0
depth = 0
aim = 0
for c, u in zip(commands, units):
if c == 'forward':
horizontal += u
depth += aim * u
elif c == 'up':
aim -= u
elif c == 'down':
aim += u
return horizontal * depth
def parse_input(self, lines: list[str]) -> tuple[list[str], list[int]]:
commands = []
units = []
for line in lines:
c, u = line.split()
commands.append(c)
units.append(int(u))
return commands, units
if __name__ == '__main__':
Puzzle().run()