-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday03.py
88 lines (78 loc) · 2.01 KB
/
day03.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python3
from collections import defaultdict
"""
Advent of Code 2037 Day 03.
"""
"""
directions we want to travel in order:
dx dy
RIGHT 1 0
UP 0 1
LEFT -1 0
DOWN 0 -1
To get the next value:
dx = -1 * old_dy
dy = old_dx
"""
class Day03:
"""Main module for solving Day03."""
@staticmethod
def should_rotate(x, y):
if x == 0 and y == 0:
return False
# Top right and bottom left corners
if x == y:
return True
# Bottom right
if x > 0 and x * -1 == y - 1:
return True
# Top Left
if x < 0 and x == -y:
return True
return False
@staticmethod
def part1(target):
x = -1
y = 0
delta_x = 1
delta_y = 0
for _ in range(0, target):
if Day03.should_rotate(x, y):
# print("rotating")
delta_x, delta_y = delta_y * -1, delta_x
x += delta_x
y += delta_y
return abs(x) + abs(y)
def neighbor_sum(grid, x, y):
if x == 0 and y == 0:
return 1
sum = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
sum += grid[(x + dx, y + dy)]
return sum
def part2(target):
grid = defaultdict(lambda: 0)
grid[(0, 0)] = 1
x = -1
y = 0
delta_x = 1
delta_y = 0
while 1:
if Day03.should_rotate(x, y):
# print("rotating")
delta_x, delta_y = delta_y * -1, delta_x
x += delta_x
y += delta_y
neighbor_sum = Day03.neighbor_sum(grid, x, y)
grid[(x, y)] = neighbor_sum
if neighbor_sum > target:
return neighbor_sum
# print(f"x{x} y{y} sum{neighbor_sum}")
if __name__ == "__main__":
print("Part1: ")
print(Day03.part1(265149))
print("Part2: ")
print(Day03.part2(265149))