-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday3.py
53 lines (39 loc) · 1.27 KB
/
day3.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
from collections import defaultdict
def part1(number: int) -> int:
position = 0
direction = -1j
for i in range(1, number):
x, y = position.real, position.imag
# turn at the corners
if x == y or (x == -y and x < 0) or (x == 1 - y and x > 0):
direction *= 1j
# move forward
position += direction
return abs(int(position.real)) + abs(int(position.imag))
def part2(number: int) -> int:
neighbors = [-1, -1 + 1j, 1j, 1 + 1j, 1, 1 - 1j, -1j, -1 - 1j]
values = defaultdict(int)
position = 0
values[0] = 1
direction = -1j
while True:
x, y = position.real, position.imag
# turn at the corners
if x == y or (x == -y and x < 0) or (x == 1 - y and x > 0):
direction *= 1j
# move forward
position += direction
# calculate value based on neighbor positions
pos_value = 0
for neighbor in neighbors:
pos_value += values[position + neighbor]
if pos_value > number:
return pos_value
else:
values[position] = pos_value
def main():
puzzle_input = 368078
print(f'Part 1: {part1(puzzle_input)}')
print(f'Part 2: {part2(puzzle_input)}')
if __name__ == "__main__":
main()