Day 14
This commit is contained in:
1 parent
435d4c6c83
commit
f63d67f005
4 files changed
+181
No files matched your search
Executable
+71
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
import numpy as np
|
||||
|
||||
|
||||
def show(rocks):
|
||||
st = ''
|
||||
for line in rocks:
|
||||
st += ''.join(line)
|
||||
return st
|
||||
|
||||
|
||||
def tilt_north(r):
|
||||
rocks = deepcopy(r)
|
||||
for i in range(0, len(rocks)):
|
||||
for j, rock in enumerate(rocks[i]):
|
||||
if rock == 'O':
|
||||
for a in range(i-1,-1,-1):
|
||||
if rocks[a][j] == '.':
|
||||
rocks[a+1][j] = '.'
|
||||
rocks[a][j] = 'O'
|
||||
else: break
|
||||
return rocks
|
||||
|
||||
|
||||
def cycle(rocks):
|
||||
new = deepcopy(rocks)
|
||||
for _ in range(4):
|
||||
new = tilt_north(new)
|
||||
new = np.rot90(new,3)
|
||||
return new
|
||||
|
||||
|
||||
def load(rocks):
|
||||
res = 0
|
||||
for i, line in enumerate(rocks):
|
||||
res += (len(rocks)-i) * np.count_nonzero(np.array(line) == 'O')
|
||||
return res
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
rocks = [list(line.strip('\n')) for line in open(sys.argv[1])]
|
||||
|
||||
# challenge 1
|
||||
tilted = tilt_north(rocks)
|
||||
show(tilted)
|
||||
res1 = load(tilted)
|
||||
|
||||
# challenge 2
|
||||
cycled = cycle(rocks)
|
||||
forms, maps = {}, {}
|
||||
forms[show(cycled)] = 0
|
||||
maps[0] = cycled
|
||||
for i in range(1,1000000000):
|
||||
cycled = cycle(cycled)
|
||||
if show(cycled) in forms.keys():
|
||||
round = forms[show(cycled)]
|
||||
n = round + ((1000000000-round-1)%(i-round))
|
||||
val = maps[n]
|
||||
break
|
||||
forms[show(cycled)] = i
|
||||
maps[i] = cycled
|
||||
|
||||
res1 = str(res1)
|
||||
print(f"challenge 1:\n{res1}\n")
|
||||
|
||||
res2 = str(load(val))
|
||||
print(f"challenge 2:\n{res2}\n")
|
||||
|
||||
Reference in new issue
Block a user