day 17 python part 2 solve
parent
db649d4a56
commit
799fefa59a
59
day17/sol.py
59
day17/sol.py
|
@ -7,8 +7,9 @@ rocks = [
|
|||
0b00011000_00011000,
|
||||
]
|
||||
|
||||
jets = itertools.cycle(">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>")
|
||||
jets = itertools.cycle(open("input").read().strip())
|
||||
jets = ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>"
|
||||
|
||||
jets = open("input").read().strip()
|
||||
|
||||
rocki = itertools.cycle(rocks)
|
||||
|
||||
|
@ -22,6 +23,7 @@ def drop(a, r, jets):
|
|||
h = a.bit_length() + 8*3
|
||||
if h % 8 != 0:
|
||||
h += 8 - h%8
|
||||
numjets = 0
|
||||
while True:
|
||||
j = next(jets)
|
||||
if j == '<':
|
||||
|
@ -35,6 +37,7 @@ def drop(a, r, jets):
|
|||
x >>= 8
|
||||
if x & (a>>h) == 0:
|
||||
r = x
|
||||
numjets += 1
|
||||
|
||||
# continue falling?
|
||||
if r & (a>>(h-8)) == 0:
|
||||
|
@ -43,19 +46,53 @@ def drop(a, r, jets):
|
|||
else:
|
||||
# stop
|
||||
a |= (r<<h)
|
||||
return a
|
||||
return a, numjets
|
||||
|
||||
|
||||
a = 0b11111111
|
||||
wall = 0b10000000_10000000_10000000_10000000_10000000
|
||||
stopped = 0
|
||||
while stopped < 2022:
|
||||
r = next(rocki)
|
||||
a = drop(a,r,jets)
|
||||
stopped += 1
|
||||
#for i in reversed(range(0,a.bit_length(),8)):
|
||||
# print("{:08b}".format((a>>i)&0xff))
|
||||
#print()
|
||||
|
||||
jeti = itertools.cycle(jets)
|
||||
i = 0
|
||||
j = 0
|
||||
seen = dict()
|
||||
def head(a):
|
||||
h = a.bit_length()
|
||||
h -= h % 8
|
||||
x = (a>>h)&0xff
|
||||
while h >= 8 and x != 0x7f:
|
||||
h -= 8
|
||||
x |= (a>>h)&0xff
|
||||
if x == 0x7f:
|
||||
return a>>h
|
||||
return 0
|
||||
import math
|
||||
def height(a):
|
||||
return math.ceil(a.bit_length()/8)
|
||||
try:
|
||||
while True:
|
||||
r = next(rocki)
|
||||
a, n = drop(a,r,jeti)
|
||||
stopped += 1
|
||||
i = (i+1) % len(rocks)
|
||||
j = (j+n) % len(jets)
|
||||
if (i,j,head(a)) in seen:
|
||||
break
|
||||
seen[(i,j,head(a))] = (a,stopped)
|
||||
#for i in reversed(range(0,a.bit_length(),8)):
|
||||
# print("{:08b}".format((a>>i)&0xff))
|
||||
#print()
|
||||
b, old = seen[(i,j,head(a))]
|
||||
sdiff = stopped - old
|
||||
hdiff = height(a) - height(b)
|
||||
goal = 1000000000000
|
||||
rounds, extra = divmod(goal - stopped, sdiff)
|
||||
h = rounds*hdiff
|
||||
for _ in range(extra):
|
||||
r = next(rocki)
|
||||
a, n = drop(a,r,jeti)
|
||||
print(rounds*hdiff + height(a)-1)
|
||||
finally:
|
||||
print(i, j, stopped)
|
||||
|
||||
print(math.ceil((a>>8).bit_length()/8))
|
||||
|
|
Loading…
Reference in New Issue