|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, pathfinding |
| 3 | + |
| 4 | +from complex_utils import * |
| 5 | +from math import pi |
| 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 | +for asteroid in grid.vertices: |
| 64 | + visible = set() |
| 65 | + for other in grid.vertices: |
| 66 | + if other == asteroid: |
| 67 | + continue |
| 68 | + visible.add(SuperComplex(other - asteroid).phase()) |
| 69 | + visible_count.append((len(visible), SuperComplex(asteroid))) |
| 70 | + |
| 71 | +if part_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 | + for target in grid.vertices: |
| 80 | + if target == station: |
| 81 | + continue |
| 82 | + vector = SuperComplex(target - station) |
| 83 | + order = ( |
| 84 | + pi / 2 - vector.phase() |
| 85 | + if vector.phase() <= pi / 2 |
| 86 | + else 10 * 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 | + while destroyed < 200: |
| 97 | + for phase in phases: |
| 98 | + if phase in targets and len(targets[phase]) > 0: |
| 99 | + targets[phase].sort(key=lambda a: a[0]) |
| 100 | + target = targets[phase][0][1] |
| 101 | + del targets[phase][0] |
| 102 | + destroyed += 1 |
| 103 | + if destroyed == 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)) |
0 commit comments