You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
1.5 KiB
71 lines
1.5 KiB
#!/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")
|
|
|
|
|