-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.py
63 lines (51 loc) · 1.36 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
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
def get_commands_from_file(file_path="day2_input.txt"):
with open(file_path) as f:
return [l.strip().split() for l in f]
def get_final_position(commands):
hor, depth = 0, 0
for c, n in commands:
n = int(n)
if c == "forward":
hor += n
elif c == "down":
depth += n
else:
assert c == "up"
depth -= n
return hor * depth
def get_final_position2(commands):
hor, depth, aim = 0, 0, 0
for c, n in commands:
n = int(n)
if c == "forward":
hor += n
depth += n * aim
elif c == "down":
aim += n
else:
assert c == "up"
aim -= n
return hor * depth
def run_tests():
commands = [
["forward", "5"],
["down", "5"],
["forward", "8"],
["up", "3"],
["down", "8"],
["forward", "2"],
]
assert get_final_position(commands) == 150
assert get_final_position2(commands) == 900
def get_solutions():
commands = get_commands_from_file()
print(get_final_position(commands))
print(get_final_position2(commands))
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)