/
aoc14.py
85 lines (72 loc) · 2.07 KB
/
aoc14.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
from aoc_util import numbers, get_data_lines
import copy
data = get_data_lines(14)
grid = {}
maxy = 0
for line in data:
spots = line.split("->")
for (von, zu) in zip(spots, spots[1:]):
(vonx, vony) = numbers(von)
(zux, zuy) = numbers(zu)
if vonx == zux:
(a, b) = sorted([vony, zuy])
for idx in range(a, b + 1):
grid[(vonx, idx)] = "#"
else:
(a, b) = sorted([vonx, zux])
for idx in range(a, b + 1):
grid[(idx, vony)] = "#"
maxy = max([maxy, vony, zuy])
og_grid = copy.deepcopy(grid)
grains = 0
while True:
sandloc = (500, 0)
alldone = False
while True:
if sandloc[1] > maxy:
alldone = True
break
if grid.get((sandloc[0], sandloc[1] + 1)) is None:
sandloc = (sandloc[0], sandloc[1] + 1)
continue
if grid.get((sandloc[0] - 1, sandloc[1] + 1)) is None:
sandloc = (sandloc[0] - 1, sandloc[1] + 1)
continue
if grid.get((sandloc[0] + 1, sandloc[1] + 1)) is None:
sandloc = (sandloc[0] + 1, sandloc[1] + 1)
continue
break
if alldone:
break
grid[sandloc] = "o"
grains += 1
print(grains)
grid = og_grid
for simx in range(-3000, 3000):
grid[(simx, maxy + 2)] = "#"
maxy += 3
grains = 0
while True:
sandloc = (500, 0)
alldone = False
while True:
if sandloc[1] > maxy:
alldone = True
break
if grid.get((sandloc[0], sandloc[1] + 1)) is None:
sandloc = (sandloc[0], sandloc[1] + 1)
continue
if grid.get((sandloc[0] - 1, sandloc[1] + 1)) is None:
sandloc = (sandloc[0] - 1, sandloc[1] + 1)
continue
if grid.get((sandloc[0] + 1, sandloc[1] + 1)) is None:
sandloc = (sandloc[0] + 1, sandloc[1] + 1)
continue
break
if alldone:
assert False, "floor not wide enough"
grid[sandloc] = "o"
grains += 1
if sandloc == (500, 0):
break
print(grains)