-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaoc_02.py
More file actions
58 lines (48 loc) · 1.59 KB
/
aoc_02.py
File metadata and controls
58 lines (48 loc) · 1.59 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
"""
Advent of Code - tentative pour J2.
Daniel Kessler (aka Dalker), le 2021.12.02
"""
DAY = "02"
HINTDATA = ["forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"]
def get_data(day) -> list[str]:
"""Read the day's input file and return contents as a list of ints."""
with open(f"input{day}.txt") as datafile:
data = [line for line in datafile]
return data
def compute_course(data: list[str]) -> int:
"""Compute submarine course."""
depth, x_pos = 0, 0
for line in data:
command, delta = line.split()
delta = int(delta)
if command == "forward":
x_pos += delta
elif command == "up":
depth -= delta
elif command == "down":
depth += delta
else:
print(command, "command not known")
return depth * x_pos
def compute_with_aim(data: list[str]) -> int:
"""Compute course again, reinterpreting 'up' and 'down'."""
aim, depth, x_pos = 0, 0, 0
for line in data:
command, delta = line.split()
delta = int(delta)
if command == "up":
aim -= delta
elif command == "down":
aim += delta
elif command == "forward":
x_pos += delta
depth += aim*delta
else:
print(command, "command not known")
return depth * x_pos
if __name__ == "__main__":
data = get_data(DAY)
print("check hint 1:", compute_course(HINTDATA))
print("check hint 2:", compute_with_aim(HINTDATA))
print(" solution 1:", compute_course(data))
print(" solution 2:", compute_with_aim(data))