-
Notifications
You must be signed in to change notification settings - Fork 0
/
14b.py
83 lines (63 loc) · 2.16 KB
/
14b.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
from math import inf
xmin,ymin = inf,0
xmax,ymax = 0,0
xsource,ysource = 500,0
with open('input14','r') as fic:
for ligne in fic.read().splitlines():
path = ligne.split(' -> ')
for s in path:
x,y = tuple(map(int, s.split(',') ))
if x > xmax: xmax = x
if x < xmin: xmin = x
if y > ymax: ymax = y
ymax = ymax + 2 # On rajoute le sol
n = ymax+1
m = xmax+1000 # on fait de la place... (c'est dégueu oui)
## Fabrication de la carte
carte = [ [0 for j in range(m+1)] for i in range(n+1) ]
carte[ysource][xsource] = '+'
with open('input14','r') as fic:
for ligne in fic.read().splitlines():
path = ligne.split(' -> ')
for i in range(len(path)-1):
xdeb,ydeb = tuple(map(int, path[i].split(',') ))
xfin,yfin = tuple(map(int, path[i+1].split(',') ))
if xfin != xdeb:
if xfin < xdeb: xfin, xdeb = xdeb, xfin
for j in range(xdeb,xfin+1):
carte[ydeb][j] = 1
elif yfin != ydeb:
if yfin < ydeb: yfin, ydeb = ydeb, yfin
for k in range(ydeb,yfin+1):
carte[k][xdeb] = 1
for j in range(m): # On rajoute le sol
carte[ymax][j] = 1
##
# def affiche(carte):
# for i in range(0,ymax+1):
# ligne = ''
# for j in range(xmin-150,xmax):
# if carte[i][j] == '+': ligne += '+'
# elif carte[i][j] == 0: ligne += '.'
# elif carte[i][j] == 1: ligne += '#'
# else: ligne += 'o'
# print(ligne)
def onestep(carte,xs,ys):
if carte[ys+1][xs] == 0: return xs,ys+1
elif carte[ys+1][xs-1] == 0: return xs-1,ys+1
elif carte[ys+1][xs+1] == 0: return xs+1,ys+1
def blocked(carte,xs,ys):
return carte[ys+1][xs] != 0 and carte[ys+1][xs-1] != 0 and carte[ys+1][xs+1] != 0
# Chute du sable
compteur = 0
while True:
xs,ys = xsource,ysource
while not blocked(carte,xs,ys):
xs, ys = onestep(carte,xs,ys)
if blocked(carte,xs,ys):
compteur += 1
if (xs,ys) == (xsource,ysource): # Nouvelle condition d'arrêt
break
carte[ys][xs] = -1
#affiche(carte)
print(compteur)