-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday2.py
61 lines (52 loc) · 1.35 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
SAMPLE = '''forward 5
down 5
forward 8
up 3
down 8
forward 2
'''
def parse_data(txt):
"""
>>> list(parse_data(SAMPLE))
[('forward', 5), ('down', 5), ('forward', 8), ('up', 3), ('down', 8), ('forward', 2)]
"""
for line in txt.strip().split('\n'):
direction, amount = line.split()
amount = int(amount)
yield direction, amount
def get_position(directions):
x, y = 0, 0
for direction, amount in directions:
if direction == 'forward':
x += amount
elif direction == 'down':
y += amount
elif direction == 'up':
y -= amount
return x, y
def part1(txt):
directions = parse_data(txt)
x, y = get_position(directions)
print(f'{x=} {y=} {x*y=}')
def get_aim_position(directions):
x, y, aim = 0, 0, 0
for direction, amount in directions:
if direction == 'forward':
x += amount
y += aim*amount
elif direction == 'down':
aim += amount
elif direction == 'up':
aim -= amount
return x, y
def part2(txt):
directions = parse_data(txt)
x, y = get_aim_position(directions)
print(f'{x=} {y=} {x*y=}')
if __name__ == '__main__':
import doctest
doctest.testmod()
part1(SAMPLE)
part1(open('day2.txt').read())
part2(SAMPLE)
part2(open('day2.txt').read())