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,
|
0b00011000_00011000,
|
||||||
]
|
]
|
||||||
|
|
||||||
jets = itertools.cycle(">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>")
|
jets = ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>"
|
||||||
jets = itertools.cycle(open("input").read().strip())
|
|
||||||
|
jets = open("input").read().strip()
|
||||||
|
|
||||||
rocki = itertools.cycle(rocks)
|
rocki = itertools.cycle(rocks)
|
||||||
|
|
||||||
|
@ -22,6 +23,7 @@ def drop(a, r, jets):
|
||||||
h = a.bit_length() + 8*3
|
h = a.bit_length() + 8*3
|
||||||
if h % 8 != 0:
|
if h % 8 != 0:
|
||||||
h += 8 - h%8
|
h += 8 - h%8
|
||||||
|
numjets = 0
|
||||||
while True:
|
while True:
|
||||||
j = next(jets)
|
j = next(jets)
|
||||||
if j == '<':
|
if j == '<':
|
||||||
|
@ -35,6 +37,7 @@ def drop(a, r, jets):
|
||||||
x >>= 8
|
x >>= 8
|
||||||
if x & (a>>h) == 0:
|
if x & (a>>h) == 0:
|
||||||
r = x
|
r = x
|
||||||
|
numjets += 1
|
||||||
|
|
||||||
# continue falling?
|
# continue falling?
|
||||||
if r & (a>>(h-8)) == 0:
|
if r & (a>>(h-8)) == 0:
|
||||||
|
@ -43,19 +46,53 @@ def drop(a, r, jets):
|
||||||
else:
|
else:
|
||||||
# stop
|
# stop
|
||||||
a |= (r<<h)
|
a |= (r<<h)
|
||||||
return a
|
return a, numjets
|
||||||
|
|
||||||
|
|
||||||
a = 0b11111111
|
a = 0b11111111
|
||||||
wall = 0b10000000_10000000_10000000_10000000_10000000
|
wall = 0b10000000_10000000_10000000_10000000_10000000
|
||||||
stopped = 0
|
stopped = 0
|
||||||
while stopped < 2022:
|
jeti = itertools.cycle(jets)
|
||||||
r = next(rocki)
|
i = 0
|
||||||
a = drop(a,r,jets)
|
j = 0
|
||||||
stopped += 1
|
seen = dict()
|
||||||
#for i in reversed(range(0,a.bit_length(),8)):
|
def head(a):
|
||||||
# print("{:08b}".format((a>>i)&0xff))
|
h = a.bit_length()
|
||||||
#print()
|
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
|
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))
|
print(math.ceil((a>>8).bit_length()/8))
|
||||||
|
|
Loading…
Reference in New Issue