-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
37 lines (33 loc) · 898 Bytes
/
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
def part1(lines):
"""
>>> part1(["forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"])
150
"""
x, y = 0, 0
for line in lines:
match line.partition(" "):
case ("forward", _, d):
x += int(d)
case ("down", _, d):
y += int(d)
case ("up", _, d):
y -= int(d)
return x * y
def part2(lines):
"""
>>> part2(["forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"])
900
"""
x, y, depth = 0, 0, 0
for line in lines:
match line.partition(" "):
case ("forward", _, d):
d = int(d)
x += d
y += d * depth
case ("down", _, d):
depth += int(d)
case ("up", _, d):
depth -= int(d)
return x * y
parts = (part1, part2)