|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +importos,itertools |
| 3 | +importpathfinding |
| 4 | + |
| 5 | +test_data= {} |
| 6 | + |
| 7 | +test=1 |
| 8 | +test_data[test]= {"input":"""""", |
| 9 | +"expected": ['Unknown','Unknown'], |
| 10 | + } |
| 11 | + |
| 12 | +test+=1 |
| 13 | +test_data[test]= {"input":"""""", |
| 14 | +"expected": ['Unknown','Unknown'], |
| 15 | + } |
| 16 | + |
| 17 | +test='real' |
| 18 | +test_data[test]= {"input":'11112123333', |
| 19 | +"expected": ['31','Unknown'], |
| 20 | + } |
| 21 | + |
| 22 | +# -------------------------------- Control program execution -------------------------------- # |
| 23 | + |
| 24 | +case_to_test='real' |
| 25 | +part_to_test=2 |
| 26 | +verbose_level=1 |
| 27 | + |
| 28 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 29 | + |
| 30 | +puzzle_input=test_data[case_to_test]['input'] |
| 31 | +puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1] |
| 32 | +puzzle_actual_result='Unknown' |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +# -------------------------------- Actual code execution -------------------------------- # |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +ifpart_to_test==1: |
| 42 | +# -------------------------------- Graph-related functions -------------------------------- # |
| 43 | +# Re-implement the heuristic to match this graph |
| 44 | +defheuristic (self,current_node,target_node): |
| 45 | +returnsum([abs(int(target_node[i])-int(current_node[i]))foriinrange (1,len(current_node))])//2 |
| 46 | +pathfinding.WeightedGraph.heuristic=heuristic |
| 47 | + |
| 48 | + |
| 49 | +# How to determine neighbors |
| 50 | +defneighbors (self,state): |
| 51 | +globalstates |
| 52 | +E=int(state[0]) |
| 53 | +movables= [xforxinrange(1,len(state))ifstate[x]==state[0]] |
| 54 | + |
| 55 | +# Connecting if we move 1 element only |
| 56 | +possible_neighbors= [] |
| 57 | +formovableinmovables: |
| 58 | +ifE>1: |
| 59 | +neighbor=str(E-1)+state[1:movable]+str(int(state[movable])-1)+state[movable+1:] |
| 60 | +possible_neighbors.append(neighbor) |
| 61 | +ifE<4: |
| 62 | +neighbor=str(E+1)+state[1:movable]+str(int(state[movable])+1)+state[movable+1:] |
| 63 | +possible_neighbors.append(neighbor) |
| 64 | + |
| 65 | +iflen(movables)>=2: |
| 66 | +formoved_objectsinitertools.combinations(movables,2): |
| 67 | +mov1,mov2=moved_objects |
| 68 | +# No use to bring 2 items downstairs |
| 69 | +# if E > 1: |
| 70 | +# neighbor = str(E-1) + state[1:mov1] + str(int(state[mov1])-1) + state[mov1+1:mov2] + str(int(state[mov2])-1) + state[mov2+1:] |
| 71 | +# possible_neighbors.append(neighbor) |
| 72 | +ifE<4: |
| 73 | +neighbor=str(E+1)+state[1:mov1]+str(int(state[mov1])+1)+state[mov1+1:mov2]+str(int(state[mov2])+1)+state[mov2+1:] |
| 74 | +possible_neighbors.append(neighbor) |
| 75 | + |
| 76 | +return [xforxinpossible_neighborsifxinstates] |
| 77 | + |
| 78 | +pathfinding.WeightedGraph.neighbors=neighbors |
| 79 | + |
| 80 | +defcost(self,current_node,next_node): |
| 81 | +return1 |
| 82 | +pathfinding.WeightedGraph.cost=cost |
| 83 | + |
| 84 | + |
| 85 | +# -------------------------------- Graph construction & execution -------------------------------- # |
| 86 | + |
| 87 | +# state = (E, TG, TM, PtG, PtM, SG, SM, PrG, PrM, RG, RM) |
| 88 | +# Forbidden states: Any G + M if G for M is absent |
| 89 | +# Forbidden transitions: E changes, the rest is identical |
| 90 | + |
| 91 | +states=set([''.join([str(E),str(TG),str(TM),str(PtG),str(PtM),str(SG),str(SM),str(PrG),str(PrM),str(RG),str(RM)]) |
| 92 | +forEinrange(1,5) |
| 93 | +forTGinrange(1,5) |
| 94 | +forTMinrange(1,5) |
| 95 | +forPtGinrange(1,5) |
| 96 | +forPtMinrange(1,5) |
| 97 | +forSGinrange(1,5) |
| 98 | +forSMinrange(1,5) |
| 99 | +forPrGinrange(1,5) |
| 100 | +forPrMinrange(1,5) |
| 101 | +forRGinrange(1,5) |
| 102 | +forRMinrange(1,5) |
| 103 | + |
| 104 | +if (TG==TMorTMnotin (TG,PtG,SG,PrG,RG)) |
| 105 | +and (PtG==PtMorPtMnotin (TG,PtG,SG,PrG,RG)) |
| 106 | +and (SG==SMorSMnotin (TG,PtG,SG,PrG,RG)) |
| 107 | +and (PrG==PrMorPrMnotin (TG,PtG,SG,PrG,RG)) |
| 108 | +and (RG==RMorRMnotin (TG,PtG,SG,PrG,RG)) |
| 109 | + ]) |
| 110 | + |
| 111 | +end='4'*11 |
| 112 | + |
| 113 | +print ('number of states',len(states)) |
| 114 | + |
| 115 | +graph=pathfinding.WeightedGraph() |
| 116 | +came_from,total_cost=graph.a_star_search(puzzle_input,end) |
| 117 | + |
| 118 | +puzzle_actual_result=total_cost[end] |
| 119 | + |
| 120 | +else: |
| 121 | +# -------------------------------- Graph-related functions -------------------------------- # |
| 122 | +# Part 2 was completely rewritten for performance improvements |
| 123 | + |
| 124 | +defvalid_state (state): |
| 125 | +pairs= [(state[x],state[x+1])forxinrange (1,len(state),2)] |
| 126 | +generators=state[1::2] |
| 127 | + |
| 128 | +forpairinpairs: |
| 129 | +ifpair[0]!=pair[1]:# Microchip is not with generator |
| 130 | +ifpair[1]ingenerators:# Microchip is with a generator |
| 131 | +returnFalse |
| 132 | + |
| 133 | +returnTrue |
| 134 | + |
| 135 | +defvisited_state(state): |
| 136 | +globalvisited_coded_states |
| 137 | + |
| 138 | +pairs= [(state[x],state[x+1])forxinrange (1,len(state),2)] |
| 139 | + |
| 140 | +coded_state= [(state[0],pair)forpairinsorted(pairs)] |
| 141 | + |
| 142 | +ifcoded_stateinvisited_coded_states: |
| 143 | +returnTrue |
| 144 | +else: |
| 145 | +visited_coded_states.append(coded_state) |
| 146 | +returnFalse |
| 147 | + |
| 148 | + |
| 149 | +# -------------------------------- BFS implementation -------------------------------- # |
| 150 | +start=list(map(int,puzzle_input))+ [1]*4 |
| 151 | +end= [4]*15 |
| 152 | + |
| 153 | +visited_coded_states= [] |
| 154 | +frontier= [(start,0)] |
| 155 | + |
| 156 | +forstatusinfrontier: |
| 157 | +state,curr_steps=status |
| 158 | + |
| 159 | +# Determine potential states to go to |
| 160 | +elev_position=state[0] |
| 161 | +# The +1 ignores the elevator |
| 162 | +elements_at_level= [item+1foritem,levelinenumerate(state[1:])iflevel==elev_position] |
| 163 | + |
| 164 | +movables=list(itertools.combinations(elements_at_level,2))+elements_at_level |
| 165 | + |
| 166 | +ifelev_position==1: |
| 167 | +directions= [1] |
| 168 | +elifelev_position==4: |
| 169 | +directions= [-1] |
| 170 | +else: |
| 171 | +directions= [1,-1] |
| 172 | + |
| 173 | +fordirectionindirections: |
| 174 | +formovableinmovables: |
| 175 | +new_state=state.copy() |
| 176 | + |
| 177 | +new_floor=elev_position+direction |
| 178 | +new_state[0]=new_floor |
| 179 | +ifisinstance(movable,tuple): |
| 180 | +# No point in moving 2 items downwards |
| 181 | +ifdirection==-1: |
| 182 | +continue |
| 183 | +new_state[movable[0]]=new_floor |
| 184 | +new_state[movable[1]]=new_floor |
| 185 | +else: |
| 186 | +new_state[movable]=new_floor |
| 187 | + |
| 188 | +ifvalid_state(new_state): |
| 189 | +ifvisited_state(new_state): |
| 190 | +continue |
| 191 | +else: |
| 192 | +frontier.append((new_state,curr_steps+1)) |
| 193 | + |
| 194 | +ifnew_state==end: |
| 195 | +puzzle_actual_result=curr_steps+1 |
| 196 | +break |
| 197 | + |
| 198 | +ifpuzzle_actual_result!='Unknown': |
| 199 | +break |
| 200 | + |
| 201 | +ifpuzzle_actual_result!='Unknown': |
| 202 | +break |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +puzzle_actual_result=curr_steps+1 |
| 211 | + |
| 212 | + |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +# -------------------------------- Outputs / results -------------------------------- # |
| 219 | + |
| 220 | +ifverbose_level>=3: |
| 221 | +print ('Input : '+puzzle_input) |
| 222 | +print ('Expected result : '+str(puzzle_expected_result)) |
| 223 | +print ('Actual result : '+str(puzzle_actual_result)) |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |