day 17 python refactor

main
magical 2022-12-16 21:45:42 -08:00
parent 40b9f86f4c
commit db649d4a56
1 changed files with 33 additions and 26 deletions

View File

@ -16,39 +16,46 @@ a = 0b11111111
wall = 0b10000000_10000000_10000000_10000000_10000000 wall = 0b10000000_10000000_10000000_10000000_10000000
stopped = 0 stopped = 0
r = 0 r = 0
while stopped < 2022:
if r == 0: def drop(a, r, jets):
# start falling # start falling
r = next(rocki) 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 while True:
#for i in reversed(range(0,a.bit_length(),8)): j = next(jets)
# print("{:08b}".format((a>>i)&0xff)) if j == '<':
#print() x = (r<<1)
else: if x & wall == 0:
if x & (a>>h) == 0:
r = x
elif j == '>':
x = (r<<7)
if x & wall == 0:
x >>= 8
if x & (a>>h) == 0:
r = x
# continue falling? # continue falling?
if r & (a>>(h-8)) == 0: if r & (a>>(h-8)) == 0:
h -= 8 h -= 8
assert h > 0
else: else:
# stop # stop
a |= (r<<h) a |= (r<<h)
r = 0 return a
stopped += 1
continue
j = next(jets)
if j == '<': a = 0b11111111
x = (r<<1) wall = 0b10000000_10000000_10000000_10000000_10000000
if x & wall == 0: stopped = 0
if x & (a>>h) == 0: while stopped < 2022:
r = x r = next(rocki)
elif j == '>': a = drop(a,r,jets)
x = (r<<7) stopped += 1
if x & wall == 0: #for i in reversed(range(0,a.bit_length(),8)):
x >>= 8 # print("{:08b}".format((a>>i)&0xff))
if x & (a>>h) == 0: #print()
r = x
import math import math
print(math.ceil((a>>8).bit_length()/8)) print(math.ceil((a>>8).bit_length()/8))