-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay2.py
94 lines (60 loc) · 2.05 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
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
88
89
90
91
92
93
94
f = open("PuzzelInputs/Day2.txt", "r")
rawInput = f.read()
class Instruction:
def __init__(self, unparsedRow):
splited = list(unparsedRow.split())
self.direction = splited[0]
self.distance = int(splited[1])
def increceDepth(position, value):
position["depth"] = position["depth"] + value
return position
def decreseDepth(position, value):
position["depth"] = position["depth"] - value
return position
def increceDistance(position, value):
position["distance"] = position["distance"] + value
return position
def interpretInstruction(position, instruction):
switcher = {
"forward": increceDistance,
"up": decreseDepth,
"down": increceDepth,
}
func = switcher.get(instruction.direction)
position = func(position, instruction.distance)
return position
inputRows = list(rawInput.split("\n"))
instructions = list(map(Instruction, inputRows))
position = {}
position["depth"] = 0
position["distance"] = 0
for instruction in instructions:
position = interpretInstruction(position, instruction)
print(position["depth"], position["distance"], position["depth"] * position["distance"])
# part2
position = {}
position["depth"] = 0
position["distance"] = 0
position["aim"] = 0
def down(position, value):
position["aim"] = position["aim"] + value
return position
def up(position, value):
position["aim"] = position["aim"] - value
return position
def forward(position, value):
position["distance"] = position["distance"] + value
position["depth"] = position["depth"] + position["aim"] * value
return position
def interpretInstruction2(position, instruction):
switcher = {
"forward": forward,
"up": up,
"down": down,
}
func = switcher.get(instruction.direction)
position = func(position, instruction.distance)
return position
for instruction in instructions:
position = interpretInstruction2(position, instruction)
print(position["depth"], position["distance"], position["depth"] * position["distance"])