day 17 python tweaks
parent
8c1696fa32
commit
bbc4075137
34
day17/sol.py
34
day17/sol.py
|
@ -10,7 +10,6 @@ rocks = [
|
||||||
wall = 0b10000000_10000000_10000000_10000000_10000000
|
wall = 0b10000000_10000000_10000000_10000000_10000000
|
||||||
|
|
||||||
jets = ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>"
|
jets = ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>"
|
||||||
|
|
||||||
jets = open("input").read().strip()
|
jets = open("input").read().strip()
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,10 +19,10 @@ def ceil(n):
|
||||||
return n
|
return n
|
||||||
|
|
||||||
def drop(a, r, jets):
|
def drop(a, r, jets):
|
||||||
# start falling
|
|
||||||
h = ceil(a.bit_length()) + 8*3
|
h = ceil(a.bit_length()) + 8*3
|
||||||
numjets = 0
|
numjets = 0
|
||||||
while True:
|
while True:
|
||||||
|
# push left or right
|
||||||
j = next(jets)
|
j = next(jets)
|
||||||
if j == '<':
|
if j == '<':
|
||||||
x = (r<<1)
|
x = (r<<1)
|
||||||
|
@ -48,14 +47,28 @@ def drop(a, r, jets):
|
||||||
return a, numjets
|
return a, numjets
|
||||||
|
|
||||||
def head(a):
|
def head(a):
|
||||||
|
# technically this isn't sufficient.
|
||||||
|
# you could imagine a stack of overhangs like
|
||||||
|
#
|
||||||
|
# |# #|
|
||||||
|
# |#### #|
|
||||||
|
# |# #|
|
||||||
|
# |# #|
|
||||||
|
# |# ###|
|
||||||
|
# |# #|
|
||||||
|
#
|
||||||
|
# that a rock might be able to navigate through indefinitely,
|
||||||
|
# but our stacks are much messier than that, so no need to worry.
|
||||||
|
#
|
||||||
|
# we could probably just take the top 10 or so rows and call it good.
|
||||||
h = ceil(a.bit_length())
|
h = ceil(a.bit_length())
|
||||||
x = 0
|
x = 0x80
|
||||||
while h >= 8 and x < 0x7f:
|
while h >= 8:
|
||||||
h -= 8
|
h -= 8
|
||||||
x |= (a>>h)&0xff
|
x |= (a>>h)&0xff
|
||||||
if x >= 0x7f:
|
if x == 0xff:
|
||||||
return a>>h
|
return a>>h
|
||||||
return 0
|
return a
|
||||||
|
|
||||||
def height(a):
|
def height(a):
|
||||||
return ceil(a.bit_length())//8
|
return ceil(a.bit_length())//8
|
||||||
|
@ -93,14 +106,15 @@ try:
|
||||||
#for i in reversed(range(0,a.bit_length(),8)):
|
#for i in reversed(range(0,a.bit_length(),8)):
|
||||||
# print("{:08b}".format((a>>i)&0xff))
|
# print("{:08b}".format((a>>i)&0xff))
|
||||||
#print()
|
#print()
|
||||||
finally:
|
except:
|
||||||
pass #print(i, j, stopped)
|
print(i, j, stopped)
|
||||||
|
raise
|
||||||
|
|
||||||
b, old = seen[(i,j,head(a))]
|
b, old = seen[(i,j,head(a))]
|
||||||
sdiff = stopped - old
|
sdiff = stopped - old
|
||||||
hdiff = height(a) - height(b)
|
hdiff = height(a) - height(b)
|
||||||
|
|
||||||
goal = 1000000000000
|
goal = 10**12
|
||||||
rounds, extra = divmod(goal - stopped, sdiff)
|
rounds, extra = divmod(goal - stopped, sdiff)
|
||||||
for _ in range(extra):
|
for _ in range(extra):
|
||||||
r = next(rocki)
|
r = next(rocki)
|
||||||
|
|
Loading…
Reference in New Issue