day 23, a bit faster with bool instead of None

This commit is contained in:
setop 2022-12-29 12:02:17 +01:00
parent 1172b9bb2e
commit 72433f9e9d
1 changed files with 10 additions and 11 deletions

View File

@ -24,19 +24,18 @@ for r in range(int(sys.argv[1])): # rounds
can6 = (y+1,x) not in E
can7 = (y+1,x-1) not in E
can8 = (y,x-1) not in E
canN = (y-1,x) if can1 and can2 and can3 else None
canS = (y+1,x) if can5 and can6 and can7 else None
canW = (y,x-1) if can7 and can8 and can1 else None
canE = (y,x+1) if can3 and can4 and can5 else None
moves = [canN, canS, canW, canE]
moves = [moves[(r+0)%4], moves[(r+1)%4], moves[(r+2)%4], moves[(r+3)%4]]
if all(m is not None for m in moves): # far away
canN = can1 and can2 and can3
canS = can5 and can6 and can7
canW = can7 and can8 and can1
canE = can3 and can4 and can5
if all((canN, canS, canW, canE)): # far away
continue
if all(m is None for m in moves): # all packed
if not any((canN, canS, canW, canE)): # all packed
continue
#moves = [moves[(r+0)%4], moves[(r+1)%4], moves[(r+2)%4], moves[(r+3)%4]]
p:Tuple[int,int] = next(filter(lambda m: m is not None ,moves))
#p:Tuple[int,int] = next(filter(lambda m: m is not None, (moves[(r+i)%4] for i in (0,1,2,3)) ))
moves = (((y-1,x),canN), ((y+1,x),canS), ((y,x-1),canW), ((y,x+1),canE))
moves = (moves[(r+0)%4], moves[(r+1)%4], moves[(r+2)%4], moves[(r+3)%4])
p:Tuple[int,int] = next(filter(lambda m: m[1], moves))[0]
#p:Tuple[int,int] = next(filter(lambda m: m[1], (moves[(r+i)%4] for i in (0,1,2,3)) ))[0] # using generator is slower than a tuple in codon ^^
if p not in P: # can move
P[p] = (y,x)
else: # occupied, invalidate move for all