|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os, pathfinding |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """0 <-> 2 |
| 8 | +1 <-> 1 |
| 9 | +2 <-> 0, 3, 4 |
| 10 | +3 <-> 2, 4 |
| 11 | +4 <-> 2, 3, 6 |
| 12 | +5 <-> 6 |
| 13 | +6 <-> 4, 5""", |
| 14 | + "expected": ['Unknown', 'Unknown'], |
| 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().strip(), |
| 25 | + "expected": ['378', 'Unknown'], |
| 26 | + } |
| 27 | + |
| 28 | +# -------------------------------- Control program execution -------------------------------- # |
| 29 | + |
| 30 | +case_to_test = 'real' |
| 31 | +part_to_test = 2 |
| 32 | +verbose_level = 1 |
| 33 | + |
| 34 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 35 | + |
| 36 | +puzzle_input = test_data[case_to_test]['input'] |
| 37 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 38 | +puzzle_actual_result = 'Unknown' |
| 39 | + |
| 40 | + |
| 41 | +# -------------------------------- Actual code execution -------------------------------- # |
| 42 | + |
| 43 | +pipes = {} |
| 44 | +programs = [] |
| 45 | +for string in puzzle_input.split('\n'): |
| 46 | + source, _, *targets = string.split(' ') |
| 47 | + targets = [x.replace(',', '') for x in targets] |
| 48 | + |
| 49 | + if not source in pipes: |
| 50 | + pipes[source] = [] |
| 51 | + |
| 52 | + pipes[source] += targets |
| 53 | + |
| 54 | + for target in targets: |
| 55 | + if not target in pipes: |
| 56 | + pipes[target] = [] |
| 57 | + else: |
| 58 | + pipes[target].append(source) |
| 59 | + |
| 60 | +programs = pipes.keys() |
| 61 | + |
| 62 | +village = pathfinding.Graph(programs, pipes) |
| 63 | +village.breadth_first_search('0') |
| 64 | + |
| 65 | +if part_to_test == 1: |
| 66 | + puzzle_actual_result = len(village.distance_from_start) |
| 67 | + |
| 68 | +else: |
| 69 | + nb_groups = 1 |
| 70 | + programs_in_groups = list(village.distance_from_start.keys()) |
| 71 | + for program in programs: |
| 72 | + if program in programs_in_groups: |
| 73 | + continue |
| 74 | + |
| 75 | + nb_groups += 1 |
| 76 | + |
| 77 | + village.reset_search() |
| 78 | + village.breadth_first_search(program) |
| 79 | + programs_in_groups += list(village.distance_from_start.keys()) |
| 80 | + |
| 81 | + puzzle_actual_result = nb_groups |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | +# -------------------------------- Outputs / results -------------------------------- # |
| 88 | + |
| 89 | +if verbose_level >= 3: |
| 90 | + print ('Input : ' + puzzle_input) |
| 91 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 92 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
0 commit comments