-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
73 lines (59 loc) · 1.66 KB
/
part2.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
from pathlib import Path
goal = int(Path(Path(__file__).parent, "input").read_text())
i_steps = 0
position = (0, 0)
numbers_per_position: dict[tuple[int, int], int] = {(0, 0): 1}
current = 1
def get_sum_neighbors(position: tuple[int, int]) -> int:
global numbers_per_position
sum_neighbors = 0
for dx, dy in (
(0, -1),
(1, -1),
(1, 0),
(1, 1),
(0, 1),
(-1, 1),
(-1, 0),
(-1, -1),
):
neighbor = (position[0] + dx, position[1] + dy)
sum_neighbors += numbers_per_position.get(neighbor, 0)
numbers_per_position[position] = sum_neighbors
return sum_neighbors
while True:
position = (position[0] + 1, position[1])
current = get_sum_neighbors(position)
i_steps += 1
if current > goal:
break
for _ in range(i_steps):
position = (position[0], position[1] - 1)
current = get_sum_neighbors(position)
if current > goal:
break
if current > goal:
break
i_steps += 1
for _ in range(i_steps):
position = (position[0] - 1, position[1])
current = get_sum_neighbors(position)
if current > goal:
break
if current > goal:
break
for _ in range(i_steps):
position = (position[0], position[1] + 1)
current = get_sum_neighbors(position)
if current > goal:
break
if current > goal:
break
for _ in range(i_steps):
position = (position[0] + 1, position[1])
current = get_sum_neighbors(position)
if current > goal:
break
if current > goal:
break
print(f"Result: {current}")