-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.py
More file actions
90 lines (81 loc) · 1.87 KB
/
Copy pathday03.py
File metadata and controls
90 lines (81 loc) · 1.87 KB
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
89
90
import numpy as np
def sumofadjacent(x, y, array):
# print(x, y)
tmp = 0
tmp += array[x + 1][y]
tmp += array[x + 1][y + 1]
tmp += array[x + 1][y - 1]
tmp += array[x][y + 1]
tmp += array[x][y - 1]
tmp += array[x - 1][y]
tmp += array[x - 1][y + 1]
tmp += array[x - 1][y - 1]
if x == 500 and y == 500:
tmp += 1
return tmp
number = int(open("day03.txt").read())
counter = 1
num = 1
distance = 2
while True:
num += distance * 4
distance += 2
if num >= number:
# print(distance)
distance -= 2
# print("dist:", distance)
tmp = num - number
print("part 1:", distance - (tmp - 3 * distance))
break
x, y = 500, 500
array = np.zeros((1000, 1000))
distance = 2
array[x][y] = sumofadjacent(x, y, array)
x += 1
exit = False
while not exit:
if exit:
break
for i in range(distance - 1):
array[x][y] = sumofadjacent(x, y, array)
if array[x][y] > number:
exit = True
break
y -= 1
if exit:
break
for i in range(distance):
array[x][y] = sumofadjacent(x, y, array)
if array[x][y] > number:
exit = True
break
x -= 1
if exit:
break
for i in range(distance):
array[x][y] = sumofadjacent(x, y, array)
if array[x][y] > number:
exit = True
break
y += 1
if exit:
break
for i in range(distance):
array[x][y] = sumofadjacent(x, y, array)
if array[x][y] > number:
exit = True
break
x += 1
if exit:
break
for i in range(2):
array[x][y] = sumofadjacent(x, y, array)
if array[x][y] > number:
exit = True
break
x += 1
if exit:
break
x -= 1
distance += 2
print("part 2:", array[x][y])