-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday11.py
58 lines (43 loc) · 1.41 KB
/
day11.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
from typing import List
import pytest
DIRECTIONS = {'n': (0, 1),
'ne': (0.5, 0.5),
'se': (0.5, -0.5),
's': (0, -1),
'sw': (-0.5, -0.5),
'nw': (-0.5, 0.5)}
@pytest.mark.parametrize('data, expected',
[
(['ne', 'ne', 'ne'], 3),
(['ne', 'ne', 'sw', 'sw'], 0),
(['ne', 'ne', 's', 's'], 2),
(['se', 'sw', 'se', 'sw', 'sw'], 3),
])
def test_part1(data: List[str], expected: int):
assert part1(data) == expected
def parse_input(filename: str) -> List[str]:
return [d for d in open(filename).read().strip().split(',')]
def part1(directions: List[str]) -> int:
x, y = 0, 0
for direction in directions:
dx, dy = DIRECTIONS[direction]
x += dx
y += dy
return int(abs(x) + abs(y))
def part2(directions: List[str]) -> int:
max_distance = 0
x, y = 0, 0
for direction in directions:
dx, dy = DIRECTIONS[direction]
x += dx
y += dy
distance = abs(x) + abs(y)
if distance > max_distance:
max_distance = distance
return int(max_distance)
def main():
data = parse_input('input/day11.txt')
print(f'Part 1: {part1(data)}')
print(f'Part 2: {part2(data)}')
if __name__ == "__main__":
main()