-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd14.py
60 lines (51 loc) · 1.4 KB
/
d14.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
data = [x.strip('\n') for x in open("i14.txt").readlines()]
# data = [ x.strip('\n') for x in open("t14.txt").readlines() ]
m = dict()
def print_map(m):
keys = m.keys()
a = list(zip(*map(sorted,(zip(*keys)))))
x1, y1 = a[0]
x2, y2 = a[-1]
for y in range(y1, y2+1):
for x in range(x1, x2+1):
print(m.get((x,y), " "), end="")
print()
for l in data:
path = [list(map(int,p.strip().split(','))) for p in l.split('->')]
for p1, p2 in zip(path, path[1:]):
xs,ys = list(map(sorted, zip(p1,p2)))
for x in range(xs[0],xs[1]+1):
for y in range(ys[0],ys[1]+1):
m[(x,y)] = "#"
m[(500,0)]='+'
# print_map(m)
keys = m.keys()
a = list(zip(*map(sorted,(zip(*keys)))))
x1, y1 = a[0]
x2, y2 = a[-1]
count = -1
done = False
part1 = None
while not done:
at_rest = False
sand = (500,0)
count += 1
while not at_rest:
x,y = sand
at_rest = True
for dx,dy in [(0,1), (-1,1),(1,1)]:
tp = (x + dx, y + dy)
if (tp[1] < (y2+2)) and (tp not in m or m[tp] == ' '):
sand = tp
if sand[1] > y2 and not part1:
part1 = count
# print(count)
at_rest = False
break
m[sand] = "o"
# print_map(m)
if sand[1] == 0:
done = True
# print_map(m)
print(part1)
print(count+1)