-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.py
38 lines (32 loc) · 981 Bytes
/
day02.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
import fileinput
from functools import reduce
def convertMove(move):
dir, amt = move.strip().split()
return (dir, int(amt))
moves = list(map(convertMove, fileinput.input()))
# Part 1
def reducer(curPos, newPos):
curDy, curDx = curPos
dir, amt = newPos
if dir == 'forward':
return (curDy, curDx + amt)
elif dir == 'down':
return (curDy + amt, curDx)
else:
return (curDy - amt, curDx)
finalDx, finalDy = reduce(reducer, moves, (0, 0))
print(finalDx * finalDy)
# Part 2
def reducer2(curPos, newPos):
curDy, curDx, curAim = curPos
dir, amt = newPos
if dir == 'forward':
newDy = curAim * amt + curDy
newDx = curDx + amt
return (newDy, newDx, curAim)
elif dir == 'down':
return (curDy, curDx, curAim + amt)
else:
return (curDy, curDx, curAim - amt)
finalDy, finalDx, _ = reduce(reducer2, moves, (0, 0, 0))
print(finalDy * finalDx)