day 17 python cleanup
parent
799fefa59a
commit
de987b60db
64
day17/sol.py
64
day17/sol.py
|
@ -7,22 +7,21 @@ rocks = [
|
||||||
0b00011000_00011000,
|
0b00011000_00011000,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
wall = 0b10000000_10000000_10000000_10000000_10000000
|
||||||
|
|
||||||
jets = ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>"
|
jets = ">>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>"
|
||||||
|
|
||||||
jets = open("input").read().strip()
|
jets = open("input").read().strip()
|
||||||
|
|
||||||
rocki = itertools.cycle(rocks)
|
|
||||||
|
|
||||||
a = 0b11111111
|
def ceil(n):
|
||||||
wall = 0b10000000_10000000_10000000_10000000_10000000
|
if n%8 != 0:
|
||||||
stopped = 0
|
n += 8 - n%8
|
||||||
r = 0
|
return n
|
||||||
|
|
||||||
def drop(a, r, jets):
|
def drop(a, r, jets):
|
||||||
# start falling
|
# start falling
|
||||||
h = a.bit_length() + 8*3
|
h = ceil(a.bit_length()) + 8*3
|
||||||
if h % 8 != 0:
|
|
||||||
h += 8 - h%8
|
|
||||||
numjets = 0
|
numjets = 0
|
||||||
while True:
|
while True:
|
||||||
j = next(jets)
|
j = next(jets)
|
||||||
|
@ -48,27 +47,39 @@ def drop(a, r, jets):
|
||||||
a |= (r<<h)
|
a |= (r<<h)
|
||||||
return a, numjets
|
return a, numjets
|
||||||
|
|
||||||
|
def head(a):
|
||||||
|
h = ceil(a.bit_length())
|
||||||
|
x = 0
|
||||||
|
while h >= 8 and x < 0x7f:
|
||||||
|
h -= 8
|
||||||
|
x |= (a>>h)&0xff
|
||||||
|
if x >= 0x7f:
|
||||||
|
return a>>h
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def height(a):
|
||||||
|
return ceil(a.bit_length())//8
|
||||||
|
|
||||||
|
|
||||||
|
# Part 1
|
||||||
|
a = 0b11111111
|
||||||
|
jeti = itertools.cycle(jets)
|
||||||
|
rocki = itertools.cycle(rocks)
|
||||||
|
for _ in range(2022):
|
||||||
|
r = next(rocki)
|
||||||
|
a, _ = drop(a,r,jeti)
|
||||||
|
print(height(a>>8))
|
||||||
|
|
||||||
|
|
||||||
|
# Part 2
|
||||||
a = 0b11111111
|
a = 0b11111111
|
||||||
wall = 0b10000000_10000000_10000000_10000000_10000000
|
|
||||||
stopped = 0
|
stopped = 0
|
||||||
jeti = itertools.cycle(jets)
|
jeti = itertools.cycle(jets)
|
||||||
|
rocki = itertools.cycle(rocks)
|
||||||
i = 0
|
i = 0
|
||||||
j = 0
|
j = 0
|
||||||
seen = dict()
|
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:
|
try:
|
||||||
while True:
|
while True:
|
||||||
r = next(rocki)
|
r = next(rocki)
|
||||||
|
@ -82,17 +93,16 @@ 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:
|
||||||
|
pass #print(i, j, stopped)
|
||||||
|
|
||||||
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 = 1000000000000
|
||||||
rounds, extra = divmod(goal - stopped, sdiff)
|
rounds, extra = divmod(goal - stopped, sdiff)
|
||||||
h = rounds*hdiff
|
|
||||||
for _ in range(extra):
|
for _ in range(extra):
|
||||||
r = next(rocki)
|
r = next(rocki)
|
||||||
a, n = drop(a,r,jeti)
|
a, n = drop(a,r,jeti)
|
||||||
print(rounds*hdiff + height(a)-1)
|
print(rounds*hdiff + height(a)-1)
|
||||||
finally:
|
|
||||||
print(i, j, stopped)
|
|
||||||
|
|
||||||
print(math.ceil((a>>8).bit_length()/8))
|
|
||||||
|
|
Loading…
Reference in New Issue