-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday12.py
72 lines (57 loc) · 1.99 KB
/
day12.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
import pytest
from typing import List, Tuple
@pytest.mark.parametrize('data, expected',
[
('', 0),
])
def test_part1(data: str, expected: int):
assert part1(data) == expected
@pytest.mark.parametrize('data, expected',
[
('', 0),
])
def test_part2(data: str, expected: int):
assert part2(data) == expected
def parse_input(filename: str) -> List[Tuple[str, int]]:
lines = [line.strip() for line in open(filename).readlines()]
return [(line[0], int(line[1:])) for line in lines]
def part1(instructions: List[Tuple[str, int]]) -> int:
dirs = {'E': 1, 'S': -1j, 'W': -1, 'N': 1j}
position = 0
direction = dirs['E']
for instruction in instructions:
move_dir, steps = instruction
if move_dir == 'R':
for i in range(steps // 90):
direction *= -1j
elif move_dir == 'L':
for i in range(steps // 90):
direction *= 1j
elif move_dir == 'F':
position += steps * direction
else:
position += steps * dirs[move_dir]
return int(abs(position.real) + abs(position.imag))
def part2(instructions: List[Tuple[str, int]]) -> int:
dirs = {'E': 1, 'S': -1j, 'W': -1, 'N': 1j}
ship = 0
waypoint = 10 + 1j
for instruction in instructions:
inst, steps = instruction
if inst == 'F':
ship += steps * waypoint
elif inst == 'R':
for i in range(steps // 90):
waypoint *= -1j
elif inst == 'L':
for i in range(steps // 90):
waypoint *= 1j
else:
waypoint += steps * dirs[inst]
return int(abs(ship.real) + abs(ship.imag))
def main():
instructions = parse_input('input/day12.txt')
print(f'Part 1: {part1(instructions)}')
print(f'Part 2: {part2(instructions)}')
if __name__ == "__main__":
main()