From 72433f9e9df3c08339e88e1a5eafcb00ba9ada2b Mon Sep 17 00:00:00 2001 From: setop Date: Thu, 29 Dec 2022 12:02:17 +0100 Subject: [PATCH] day 23, a bit faster with bool instead of None --- d23/run.co.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/d23/run.co.py b/d23/run.co.py index f852213..0d16f3b 100644 --- a/d23/run.co.py +++ b/d23/run.co.py @@ -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