My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Prune rearrangements of identical states

+10 -5
+10 -5
2016/day11.py
··· 23 23 return True 24 24 25 25 26 + def ordered(state): 27 + substate = sorted(zip(state[::2], state[1::2])) 28 + return tuple(item for subl in substate for item in subl) 29 + 26 30 27 31 def solve(state, start_floor=0): 28 32 if start_floor == 3 and complete(state): ··· 43 47 moves.extend([(2, floor + 1), (1, floor + 1)]) 44 48 45 49 if floor > 0: 46 - moves.extend([(1, floor - 1), (2, floor - 1)]) 50 + # Don't move downstairs if the floors below are empty 51 + if any(f < floor for f in state): 52 + moves.extend([(1, floor - 1), (2, floor - 1)]) 47 53 48 54 moved_two_up = False 49 55 moved_one_down = False ··· 62 68 if complete(next_state): 63 69 return steps + 1 64 70 65 - if valid(next_state) and (new_floor, next_state) not in seen: 66 - pair = (new_floor, next_state) 67 - new_horizon.append(pair) 68 - seen.add(pair) 71 + if valid(next_state) and (new_floor, ordered(next_state)) not in seen: 72 + new_horizon.append((new_floor, next_state)) 73 + seen.add((new_floor, ordered(next_state))) 69 74 70 75 if n == 2 and new_floor > floor: 71 76 moved_two_up = True