diff --git a/d14/part1.py b/d14/part1.py new file mode 100644 index 0000000..08d96be --- /dev/null +++ b/d14/part1.py @@ -0,0 +1,48 @@ +import sys + +W = 1000 +H = 200 + +L = [[0 for _ in range(W)] for _ in range(H)] + +def pr(): + t = ".#o+" + print("\n".join("".join(t[i] for i in l[489:489+92]) for l in L)) + +def s2li(s,sep=','): + return list(map(int,s.split(sep))) + +for l in sys.stdin.read().splitlines(): + lp = l.split(" -> ") + for f,t in zip(lp,lp[1:]): + (a,b,c,d)= (*s2li(f),*s2li(t)) + x0 = min(a,c) + y0 = min(b,d) + x1 = max(a,c) + y1 = max(b,d) + for x in range(x0,x1+1): + for y in range(y0,y1+1): + L[y][x] = 1 +stop = False +N = 0 +while not stop: + N += 1 + y = 0 + x = 500 + more = True + while more and not stop: + more = False + while L[y][x]==0 and not stop: # if only air + y+=1 + stop = y >= H-3 + if L[y][x-1]==0: # can fall left + x -= 1 + more = True + elif L[y][x+1]==0: # can fall right + x += 1 + more = True + L[y-1][x] = 2 # mark sand + +pr() + +print(N-1) \ No newline at end of file diff --git a/d14/part2.py b/d14/part2.py new file mode 100644 index 0000000..35f0aea --- /dev/null +++ b/d14/part2.py @@ -0,0 +1,69 @@ +import sys + +W = 1000 +H = 200 + +L = [[0 for _ in range(W)] for _ in range(H)] + +def prPGM(): + print("P2") + print("367 184") + print("2") + t = "201" + print("\n".join(" ".join(t[i] for i in l[317:684]) for l in L)) + +def pr(): + if len(sys.argv)>1 and sys.argv[1] == "G": + prPGM() + else: + t = ".#o+" + print("\n".join("".join(t[i] for i in l[450:550]) for l in L)) + +def s2li(s,sep=','): + return list(map(int,s.split(sep))) + +MAX_Y = 0 +for l in sys.stdin.read().splitlines(): + lp = l.split(" -> ") + for f,t in zip(lp,lp[1:]): + (a,b,c,d)= (*s2li(f),*s2li(t)) + x0 = min(a,c) + y0 = min(b,d) + x1 = max(a,c) + y1 = max(b,d) + MAX_Y = max(MAX_Y, y1) + for x in range(x0,x1+1): + for y in range(y0,y1+1): + L[y][x] = 1 + +for x in range(W): + L[MAX_Y+2][x] = 1 +L = L[:MAX_Y+3] + +stop = False +N = 0 +while not stop: + N += 1 + y = 0 + x = 500 + more = True # can all left or right + while more and not stop: + more = False + while L[y][x]==0 : #and not stop: # if only air + #print(N, x,y) + y+=1 + stop = y == MAX_Y+2 + #print(N, x,y) + if L[y][x-1]==0: # can fall left + x -= 1 + more = True + elif L[y][x+1]==0: # can fall right + x += 1 + more = True + else: + stop = y == 1 # can't put more sand + L[y-1][x] = 2 # mark sand + +pr() + +print(N) \ No newline at end of file