|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +importos,pathfinding |
| 3 | + |
| 4 | +fromcomplex_utilsimport* |
| 5 | +frommathimportpi |
| 6 | + |
| 7 | +test_data= {} |
| 8 | + |
| 9 | +test=1 |
| 10 | +test_data[test]= { |
| 11 | +"input":""".#..##.###...####### |
| 12 | +##.############..##. |
| 13 | +.#.######.########.# |
| 14 | +.###.#######.####.#. |
| 15 | +#####.##.#.##.###.## |
| 16 | +..#####..#.######### |
| 17 | +#################### |
| 18 | +#.####....###.#.#.## |
| 19 | +##.################# |
| 20 | +#####.##.###..####.. |
| 21 | +..######..##.####### |
| 22 | +####.##.####...##..# |
| 23 | +.#####..#.######.### |
| 24 | +##...#.##########... |
| 25 | +#.##########.####### |
| 26 | +.####.#.###.###.#.## |
| 27 | +....##.##.###..##### |
| 28 | +.#.#.###########.### |
| 29 | +#.#.#.#####.####.### |
| 30 | +###.##.####.##.#..##""", |
| 31 | +"expected": ["210","802"], |
| 32 | +} |
| 33 | + |
| 34 | +test="real" |
| 35 | +input_file=os.path.join( |
| 36 | +os.path.dirname(__file__), |
| 37 | +"Inputs", |
| 38 | +os.path.basename(__file__).replace(".py",".txt"), |
| 39 | +) |
| 40 | +test_data[test]= { |
| 41 | +"input":open(input_file,"r+").read().strip(), |
| 42 | +"expected": ["256","1707"], |
| 43 | +} |
| 44 | + |
| 45 | +# -------------------------------- Control program execution ------------------------- # |
| 46 | + |
| 47 | +case_to_test="real" |
| 48 | +part_to_test=2 |
| 49 | + |
| 50 | +# -------------------------------- Initialize some variables ------------------------- # |
| 51 | + |
| 52 | +puzzle_input=test_data[case_to_test]["input"] |
| 53 | +puzzle_expected_result=test_data[case_to_test]["expected"][part_to_test-1] |
| 54 | +puzzle_actual_result="Unknown" |
| 55 | + |
| 56 | + |
| 57 | +# -------------------------------- Actual code execution ----------------------------- # |
| 58 | + |
| 59 | +grid=pathfinding.Graph() |
| 60 | +grid.grid_to_vertices(puzzle_input,wall=".") |
| 61 | + |
| 62 | +visible_count= [] |
| 63 | +forasteroidingrid.vertices: |
| 64 | +visible=set() |
| 65 | +forotheringrid.vertices: |
| 66 | +ifother==asteroid: |
| 67 | +continue |
| 68 | +visible.add(SuperComplex(other-asteroid).phase()) |
| 69 | +visible_count.append((len(visible),SuperComplex(asteroid))) |
| 70 | + |
| 71 | +ifpart_to_test==1: |
| 72 | +puzzle_actual_result=max(visible_count)[0] |
| 73 | + |
| 74 | + |
| 75 | +else: |
| 76 | +station=max(visible_count)[1] |
| 77 | +targets= {} |
| 78 | + |
| 79 | +fortargetingrid.vertices: |
| 80 | +iftarget==station: |
| 81 | +continue |
| 82 | +vector=SuperComplex(target-station) |
| 83 | +order= ( |
| 84 | +pi/2-vector.phase() |
| 85 | +ifvector.phase()<=pi/2 |
| 86 | +else10*pi/4-vector.phase() |
| 87 | + ) |
| 88 | +try: |
| 89 | +targets[order].append((vector.amplitude(),target)) |
| 90 | +except: |
| 91 | +targets[order]= [(vector.amplitude(),target)] |
| 92 | + |
| 93 | +phases=list(targets.keys()) |
| 94 | +phases.sort() |
| 95 | +destroyed=0 |
| 96 | +whiledestroyed<200: |
| 97 | +forphaseinphases: |
| 98 | +ifphaseintargetsandlen(targets[phase])>0: |
| 99 | +targets[phase].sort(key=lambdaa:a[0]) |
| 100 | +target=targets[phase][0][1] |
| 101 | +deltargets[phase][0] |
| 102 | +destroyed+=1 |
| 103 | +ifdestroyed==200: |
| 104 | +break |
| 105 | + |
| 106 | +puzzle_actual_result=int(target.real*100-target.imag) |
| 107 | + |
| 108 | + |
| 109 | +# -------------------------------- Outputs / results --------------------------------- # |
| 110 | + |
| 111 | +print("Expected result : "+str(puzzle_expected_result)) |
| 112 | +print("Actual result : "+str(puzzle_actual_result)) |