|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +importos |
| 3 | + |
| 4 | +test_data= {} |
| 5 | + |
| 6 | +test=1 |
| 7 | +test_data[test]= {"input":""".#.#.# |
| 8 | +...##. |
| 9 | +#....# |
| 10 | +..#... |
| 11 | +#.#..# |
| 12 | +####..""", |
| 13 | +"iterations":5,# 4 for Part 1 |
| 14 | +"expected": ['4','17'], |
| 15 | + } |
| 16 | + |
| 17 | +test+=1 |
| 18 | +test_data[test]= {"input":"""""", |
| 19 | +"expected": ['Unknown','Unknown'], |
| 20 | + } |
| 21 | + |
| 22 | +test='real' |
| 23 | +input_file=os.path.join(os.path.dirname(__file__),'Inputs',os.path.basename(__file__).replace('.py','.txt')) |
| 24 | +test_data[test]= {"input":open(input_file,"r+").read(), |
| 25 | +"iterations":100, |
| 26 | +"expected": ['821','886'], |
| 27 | + } |
| 28 | +# 865 too low |
| 29 | +# -------------------------------- Control program execution -------------------------------- # |
| 30 | + |
| 31 | +case_to_test='real' |
| 32 | +part_to_test=2 |
| 33 | +verbose_level=1 |
| 34 | + |
| 35 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 36 | + |
| 37 | +puzzle_input=test_data[case_to_test]['input'] |
| 38 | +puzzle_iterations=test_data[case_to_test]['iterations'] |
| 39 | +puzzle_expected_result=test_data[case_to_test]['expected'][part_to_test-1] |
| 40 | +puzzle_actual_result='Unknown' |
| 41 | + |
| 42 | + |
| 43 | +# -------------------------------- Actual code execution -------------------------------- # |
| 44 | + |
| 45 | +lights=puzzle_input.split('\n') |
| 46 | +lights_width=len(lights[0]) |
| 47 | +lights_height=len(lights) |
| 48 | + |
| 49 | +# For Part 2 : updating the corners of initial situation for the calculation |
| 50 | +ifpart_to_test==2: |
| 51 | +new_lights='' |
| 52 | +foryinrange (lights_height): |
| 53 | +forxinrange (lights_width): |
| 54 | +if (y,x)in ((0,0), (0,lights_width-1), (lights_height-1,0), (lights_height-1,lights_width-1)): |
| 55 | +new_lights+='#' |
| 56 | +else: |
| 57 | +new_lights+=lights[y][x] |
| 58 | +new_lights+='\n' |
| 59 | + |
| 60 | +lights=new_lights.split('\n') |
| 61 | + |
| 62 | + |
| 63 | +forninrange(0,puzzle_iterations): |
| 64 | +new_lights='' |
| 65 | +foryinrange (lights_height): |
| 66 | +forxinrange (lights_width): |
| 67 | +# For Part 2 : updating the corners for the calculation |
| 68 | +ifpart_to_test==2and (y,x)in ((0,0), (0,lights_width-1), (lights_height-1,0), (lights_height-1,lights_width-1)): |
| 69 | +new_lights+='#' |
| 70 | +continue |
| 71 | +neighbors_x=range(max(x-1,0),min(x+2,lights_width)) |
| 72 | +neighbors_y=range(max(y-1,0),min(y+2,lights_height)) |
| 73 | + |
| 74 | +neighbors= [1iflights[i][j]=='#'and (i,j)!= (y,x)else0foriinneighbors_yforjinneighbors_x] |
| 75 | +count_on=sum(neighbors) |
| 76 | +iflights[y][x]=='#'andcount_onin (2,3): |
| 77 | +new_lights+='#' |
| 78 | +eliflights[y][x]=='#': |
| 79 | +new_lights+='.' |
| 80 | +eliflights[y][x]=='.'andcount_on==3: |
| 81 | +new_lights+='#' |
| 82 | +else: |
| 83 | +new_lights+='.' |
| 84 | +new_lights+='\n' |
| 85 | +lights=new_lights.split('\n') |
| 86 | +new_lights='' |
| 87 | +puzzle_actual_result=sum([1iflights[y][x]=='#'else0forxinrange(lights_width)foryinrange(lights_height)]) |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +# -------------------------------- Outputs / results -------------------------------- # |
| 93 | + |
| 94 | +ifverbose_level>=3: |
| 95 | +print ('Input : '+puzzle_input) |
| 96 | +print ('Expected result : '+str(puzzle_expected_result)) |
| 97 | +print ('Actual result : '+str(puzzle_actual_result)) |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |