-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
64 lines (50 loc) · 1.42 KB
/
solver.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
#!/usr/bin/env python3
from pathlib import Path
currdir = Path(__file__).parent.absolute()
# paste the example from the problem here ↓
test_input = """
forward 5
down 5
forward 8
up 3
down 8
forward 2
""".strip()
def main():
raw_input = open(currdir.joinpath("input.txt")).read()
# raw_input = test_input # testing with the example - comment for real input
instructions = [
(instr, int(amount))
for (instr, amount) in [line.split(" ") for line in raw_input.splitlines()]
]
print("Solution to Part 1:")
print(part1(instructions))
print("Solution to Part 2:")
print(part2(instructions))
def part1(p_input):
horizontal = depth = 0
for command, value in p_input:
if command == "forward":
horizontal += value
if command == "up":
depth -= value
if command == "down":
depth += value
print(f"horizontal={horizontal}, depth={depth}")
solution = horizontal * depth
return solution
def part2(p_input):
horizontal = depth = aim = 0
for command, value in p_input:
if command == "forward":
horizontal += value
depth += aim * value
if command == "up":
aim -= value
if command == "down":
aim += value
print(f"horizontal={horizontal}, depth={depth}")
solution = horizontal * depth
return solution
if __name__ == "__main__":
main()