-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.py
119 lines (97 loc) · 3.54 KB
/
day14.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
def moveOne(starting, map):
if map[starting[0], starting[1]-1] == 0:
return [starting[0], starting[1]-1]
elif map[starting[0], starting[1]-1] > 0:
if map[starting[0]-1, starting[1]-1] == 0:
return [starting[0]-1, starting[1]-1]
elif map[starting[0]+1, starting[1]-1] == 0:
return [starting[0]+1, starting[1]-1]
return starting
def move(starting, map):
ending = moveOne(starting, map)
while ending != starting:
starting = ending
ending = moveOne(starting, map)
if ending[1] < 2:
return ending
return ending
f = open("ressources/day14.txt", "r")
lines = f.readlines()
sizeX = 200
sizeY = 200
map = np.zeros((sizeX,sizeY))
for line in lines:
path = line.replace("\n", "").split("->")
instructions = []
for item in path:
coordinates = item.split(",")
x = coordinates[0]
y = coordinates[1]
instructions.append([int(x),int(y)])
for i in range(1,len(instructions)):
lineStartX = instructions[i-1][0]
lineEndX = instructions[i][0]
if lineStartX > lineEndX:
lineStartX, lineEndX = lineEndX, lineStartX
lineStartY = instructions[i-1][1]+1
lineEndY = instructions[i][1]+1
if lineStartY < lineEndY:
lineStartY, lineEndY = lineEndY, lineStartY
if lineStartX != lineEndX:
map[lineStartX-500+sizeX//2:lineEndX-500+sizeX//2+1, sizeY-lineStartY] = 1
elif lineStartY != lineEndY:
map[lineStartX-500+sizeX//2, sizeY-lineStartY:sizeY-lineEndY+1] = 1
plt.imshow(255-np.flip(np.transpose(map), axis=0)*120, cmap='gray', vmin=0, vmax=255)
plt.show()
sandStart = [sizeX//2, sizeY]
nbSand = 0
while True:
sandPosition = move(sandStart, map)
map[sandPosition[0], sandPosition[1]] = 2
nbSand += 1
if sandPosition[1] < 2:
break
print("Number of sand falling before the abyss: {}".format(nbSand-1))
plt.imshow(255-np.flip(np.transpose(map), axis=0)*120, cmap='gray', vmin=0, vmax=255)
plt.show()
sizeX = 500
sizeY = 200
map = np.zeros((sizeX,sizeY))
map = np.zeros((sizeX,sizeY))
for line in lines:
path = line.replace("\n", "").split("->")
instructions = []
for item in path:
coordinates = item.split(",")
x = coordinates[0]
y = coordinates[1]
instructions.append([int(x),int(y)])
for i in range(1,len(instructions)):
lineStartX = instructions[i-1][0]
lineEndX = instructions[i][0]
if lineStartX > lineEndX:
lineStartX, lineEndX = lineEndX, lineStartX
lineStartY = instructions[i-1][1]+1
lineEndY = instructions[i][1]+1
if lineStartY < lineEndY:
lineStartY, lineEndY = lineEndY, lineStartY
if lineStartX != lineEndX:
map[lineStartX-500+sizeX//2:lineEndX-500+sizeX//2+1, sizeY-lineStartY] = 1
elif lineStartY != lineEndY:
map[lineStartX-500+sizeX//2, sizeY-lineStartY:sizeY-lineEndY+1] = 1
floorlevel = np.amax(sizeY-np.argwhere(map>0)[:,1])
map[:, sizeY-floorlevel-2] = 1
sandStart = [sizeX//2, sizeY]
nbSand = 0
while True:
sandPosition = move(sandStart, map)
map[sandPosition[0], sandPosition[1]] = 2
nbSand += 1
if sandPosition[1] == sizeY-1:
break
print("Number of sand before reaching the top: {}".format(nbSand))
plt.imshow(255-np.flip(np.transpose(map), axis=0)*120, cmap='gray', vmin=0, vmax=255)
plt.show()