|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,grid,graph,dot,assembly,re,itertools |
| 3 | +fromcollectionsimportCounter,deque,defaultdict |
| 4 | +importcopy |
| 5 | +fromcompassimport* |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +deflmap(func,*iterables): |
| 10 | +returnlist(map(func,*iterables)) |
| 11 | + |
| 12 | + |
| 13 | +defints(s:str): |
| 14 | +returnlmap(int,re.findall(r"-?\d+",s))# thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +defpositive_ints(s:str): |
| 18 | +returnlmap(int,re.findall(r"\d+",s))# thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +deffloats(s:str): |
| 22 | +returnlmap(float,re.findall(r"-?\d+(?:\.\d+)?",s)) |
| 23 | + |
| 24 | + |
| 25 | +defpositive_floats(s:str): |
| 26 | +returnlmap(float,re.findall(r"\d+(?:\.\d+)?",s)) |
| 27 | + |
| 28 | + |
| 29 | +defwords(s:str): |
| 30 | +returnre.findall(r"[a-zA-Z]+",s) |
| 31 | + |
| 32 | + |
| 33 | +test_data= {} |
| 34 | + |
| 35 | +test=1 |
| 36 | +test_data[test]= { |
| 37 | +"input":"""L.LL.LL.LL |
| 38 | +LLLLLLL.LL |
| 39 | +L.L.L..L.. |
| 40 | +LLLL.LL.LL |
| 41 | +L.LL.LL.LL |
| 42 | +L.LLLLL.LL |
| 43 | +..L.L..... |
| 44 | +LLLLLLLLLL |
| 45 | +L.LLLLLL.L |
| 46 | +L.LLLLL.LL""", |
| 47 | +"expected": ["37","26"], |
| 48 | +} |
| 49 | + |
| 50 | +test="real" |
| 51 | +input_file=os.path.join( |
| 52 | +os.path.dirname(__file__), |
| 53 | +"Inputs", |
| 54 | +os.path.basename(__file__).replace(".py",".txt"), |
| 55 | +) |
| 56 | +test_data[test]= { |
| 57 | +"input":open(input_file,"r+").read(), |
| 58 | +"expected": ["2324","2068"], |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +# -------------------------------- Control program execution ------------------------- # |
| 63 | + |
| 64 | +case_to_test="real" |
| 65 | +part_to_test=2 |
| 66 | + |
| 67 | +# -------------------------------- Initialize some variables ------------------------- # |
| 68 | + |
| 69 | +puzzle_input=test_data[case_to_test]["input"] |
| 70 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 71 | +puzzle_actual_result="Unknown" |
| 72 | + |
| 73 | + |
| 74 | +# -------------------------------- Actual code execution ----------------------------- # |
| 75 | + |
| 76 | + |
| 77 | +dot.all_directions=directions_diagonals |
| 78 | +all_directions=directions_diagonals |
| 79 | +dot.Dot.allowed_direction_map= { |
| 80 | +".": {dir:all_directionsfordirinall_directions}, |
| 81 | +"#": {}, |
| 82 | +" ": {}, |
| 83 | +"+": {dir:all_directionsfordirinall_directions}, |
| 84 | +"|": {north: [north,south],south: [north,south]}, |
| 85 | +"^": {north: [north,south],south: [north,south]}, |
| 86 | +"v": {north: [north,south],south: [north,south]}, |
| 87 | +"-": {east: [east,west],west: [east,west]}, |
| 88 | +">": {east: [east,west],west: [east,west]}, |
| 89 | +"<": {east: [east,west],west: [east,west]}, |
| 90 | +"\\": {north: [east],east: [north],south: [west],west: [south]}, |
| 91 | +"/": {north: [west],east: [south],south: [east],west: [north]}, |
| 92 | +"X": {dir:all_directionsfordirinall_directions}, |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +grid.Grid.all_directions=directions_diagonals |
| 97 | + |
| 98 | +ifpart_to_test==1: |
| 99 | +seats=grid.Grid() |
| 100 | +seats.all_directions=directions_diagonals |
| 101 | +seats.text_to_dots(puzzle_input) |
| 102 | + |
| 103 | +new_seats=grid.Grid() |
| 104 | +new_seats.all_directions=directions_diagonals |
| 105 | +new_seats.text_to_dots(puzzle_input) |
| 106 | + |
| 107 | +i=0 |
| 108 | +whileTrue: |
| 109 | +i+=1 |
| 110 | +watch= [1-1j] |
| 111 | +fordotinseats.dots: |
| 112 | +ifseats.dots[dot].terrain=="L"andall( |
| 113 | + [d.terrainin ("L",".")fordinseats.dots[dot].get_neighbors()] |
| 114 | + ): |
| 115 | +new_seats.dots[dot].terrain="#" |
| 116 | +elif ( |
| 117 | +seats.dots[dot].terrain=="#" |
| 118 | +andsum( |
| 119 | + [1fordinseats.dots[dot].get_neighbors()ifd.terrain=="#"] |
| 120 | + ) |
| 121 | +>=4 |
| 122 | + ): |
| 123 | +new_seats.dots[dot].terrain="L" |
| 124 | +else: |
| 125 | +new_seats.dots[dot].terrain=seats.dots[dot].terrain |
| 126 | + |
| 127 | +ifall( |
| 128 | + [seats.dots[d].terrain==new_seats.dots[d].terrainfordinseats.dots] |
| 129 | + ): |
| 130 | +break |
| 131 | + |
| 132 | +seats=copy.deepcopy(new_seats) |
| 133 | +new_seats.text_to_dots(puzzle_input) |
| 134 | +print(i) |
| 135 | + |
| 136 | +puzzle_actual_result=sum([1fordinseats.dotsifseats.dots[d].terrain=="#"]) |
| 137 | + |
| 138 | + |
| 139 | +else: |
| 140 | + |
| 141 | +defget_neighbors_map(dot): |
| 142 | +neighbors= [] |
| 143 | +ifdot.grid.widthisNone: |
| 144 | +dot.grid.get_size() |
| 145 | +fordirectionindot.allowed_directions: |
| 146 | +neighbor=dot+direction |
| 147 | +whileneighborisnotNone: |
| 148 | +ifneighbor.terrainin ("L","#"): |
| 149 | +neighbors.append(neighbor.position) |
| 150 | +break |
| 151 | +else: |
| 152 | +neighbor+=direction |
| 153 | +returnneighbors |
| 154 | + |
| 155 | +seats=grid.Grid() |
| 156 | +seats.all_directions=directions_diagonals |
| 157 | +seats.text_to_dots(puzzle_input) |
| 158 | +seats.neighbors_map= { |
| 159 | +dot:get_neighbors_map(seats.dots[dot])fordotinseats.dots |
| 160 | + } |
| 161 | + |
| 162 | +new_seats=copy.deepcopy(seats) |
| 163 | + |
| 164 | +defget_neighbors(self): |
| 165 | +return { |
| 166 | +self.grid.dots[neighbor]:1 |
| 167 | +forneighborinself.grid.neighbors_map[self.position] |
| 168 | + } |
| 169 | + |
| 170 | +dot.Dot.get_neighbors=get_neighbors |
| 171 | + |
| 172 | +i=0 |
| 173 | + |
| 174 | +whileTrue: |
| 175 | +i+=1 |
| 176 | +watch= [2] |
| 177 | +fordotinseats.dots: |
| 178 | +ifseats.dots[dot].terrain=="L"andall( |
| 179 | + [d.terrainin ("L",".")fordinseats.dots[dot].get_neighbors()] |
| 180 | + ): |
| 181 | +new_seats.dots[dot].terrain="#" |
| 182 | +elif ( |
| 183 | +seats.dots[dot].terrain=="#" |
| 184 | +andsum( |
| 185 | + [1fordinseats.dots[dot].get_neighbors()ifd.terrain=="#"] |
| 186 | + ) |
| 187 | +>=5 |
| 188 | + ): |
| 189 | +new_seats.dots[dot].terrain="L" |
| 190 | +else: |
| 191 | +new_seats.dots[dot].terrain=seats.dots[dot].terrain |
| 192 | + |
| 193 | +ifall( |
| 194 | + [seats.dots[d].terrain==new_seats.dots[d].terrainfordinseats.dots] |
| 195 | + ): |
| 196 | +break |
| 197 | + |
| 198 | +seats=copy.deepcopy(new_seats) |
| 199 | +print(i) |
| 200 | + |
| 201 | +puzzle_actual_result=sum([1fordinseats.dotsifseats.dots[d].terrain=="#"]) |
| 202 | + |
| 203 | + |
| 204 | +# -------------------------------- Outputs / results --------------------------------- # |
| 205 | + |
| 206 | +print("Case :",case_to_test,"- Part",part_to_test) |
| 207 | +print("Expected result : "+str(puzzle_expected_result)) |
| 208 | +print("Actual result : "+str(puzzle_actual_result)) |
| 209 | +# Date created: 2020-12-11 06:00:07.140562 |
| 210 | +# Part 1: 2020-12-11 06:22:46 |
| 211 | +# Part 2: 2020-12-11 06:37:29 |