-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday1.py
67 lines (52 loc) · 1.8 KB
/
day1.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
import pytest
from typing import List
@pytest.mark.parametrize('instructions, expected',
[
(['R2', 'L3'], 5),
(['R2', 'R2', 'R2'], 2),
(['R5', 'L5', 'R5', 'R3'], 12),
])
def test_part1(instructions: List[str], expected: int):
assert part1(instructions) == expected
@pytest.mark.parametrize('instructions, expected',
[
(['R8', 'R4', 'R4', 'R8'], 4),
])
def test_part2(instructions: List[str], expected: int):
assert part2(instructions) == expected
def parse_input(filename: str) -> List[str]:
return open(filename).read().strip().split(', ')
def part2(instructions: List[str]) -> int:
position = 0
visited = [position]
direction = 1j
for instruction in instructions:
turn = instruction[0]
steps = int(instruction[1:])
if turn == 'R':
direction *= -1j
else:
direction *= 1j
for _ in range(steps):
position += direction
if position in visited:
return int(abs(position.real) + abs(position.imag))
visited.append(position)
def part1(instructions: List[str]) -> int:
position = 0
direction = 1j
for instruction in instructions:
turn = instruction[0]
steps = int(instruction[1:])
if turn == 'R':
direction *= -1j
else:
direction *= 1j
position += direction * steps
return int(abs(position.real) + abs(position.imag))
def main():
directions = parse_input('input/day1.txt')
print(f'Part 1: {part1(directions)}')
print(f'Part 2: {part2(directions)}')
if __name__ == "__main__":
main()