Repository navigation
Expand file tree
/
Copy pathday02.py
More file actions
executable file
·87 lines (71 loc) · 1.97 KB
/
Copy pathday02.py
File metadata and controls
executable file
·87 lines (71 loc) · 1.97 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
from collections import namedtuple
### Day 2
# Part 1
Position = namedtuple("Position", ["horizontal", "depth"])
origin = Position(0,0)
def move(cmd, pos=origin):
pieces = cmd.split()
direction, steps = pieces[0], int(pieces[1])
horizontal, depth = pos
if direction == 'forward':
horizontal += steps
elif direction == 'down':
depth += steps
elif direction == 'up':
depth -= steps
return Position(horizontal, depth)
day2_test_input = [
'forward 5',
'down 5',
'forward 8',
'up 3',
'down 8',
'forward 2',
]
def moves(cmds, pos=origin):
for cmd in cmds:
pos = move(cmd, pos)
return pos
got = moves(day2_test_input)
want = Position(15,10)
if got != want:
raise Exception('Bad moves: got %s want %s' % (got, want))
def PosMul(pos):
return pos.horizontal * pos.depth
with open('inputs/day02.input') as f:
day2_input = f.read().splitlines()
day2a = moves(day2_input)
# => (2165,933) *=> 20199445 <-- Wrong!
print('Day 2 Part 1 =>', PosMul(day2a))
# Part 2
class Location:
def __init__(self):
self.horizontal = 0
self.depth = 0
self.aim = 0
def move(self, cmd):
pieces = cmd.split()
direction, steps = pieces[0], int(pieces[1])
if direction == 'forward':
self.horizontal += steps
self.depth += steps * self.aim
elif direction == 'down':
self.aim += steps
elif direction == 'up':
self.aim -= steps
else:
raise Exception('bad direction %s in cmd %s' % (direction, cmd))
def moves(self, cmds):
for cmd in cmds:
self.move(cmd)
def mult(self):
return self.horizontal * self.depth
loc = Location()
loc.moves(day2_test_input)
got = loc.mult()
if got != 900:
raise Exception('location test: got %s want 900' % got)
day2b = Location()
day2b.moves(day2_input)
print('Day 2 Part 2 =>', day2b.mult()) # => 1599311480