|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": """Step C must be finished before step A can begin. |
| 8 | +Step C must be finished before step F can begin. |
| 9 | +Step A must be finished before step B can begin. |
| 10 | +Step A must be finished before step D can begin. |
| 11 | +Step B must be finished before step E can begin. |
| 12 | +Step D must be finished before step E can begin. |
| 13 | +Step F must be finished before step E can begin.""", |
| 14 | + "expected": ['CABDFE', 'CABFDE'], |
| 15 | + } |
| 16 | + |
| 17 | +test = 'real' |
| 18 | +input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt')) |
| 19 | +test_data[test] = {"input": open(input_file, "r+").read().strip(), |
| 20 | + "expected": ['OVXCKZBDEHINPFSTJLUYRWGAMQ', '955'], |
| 21 | + } |
| 22 | + |
| 23 | +# -------------------------------- Control program execution -------------------------------- # |
| 24 | + |
| 25 | +case_to_test = 1 |
| 26 | +part_to_test = 1 |
| 27 | +verbose_level = 1 |
| 28 | + |
| 29 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 30 | + |
| 31 | +puzzle_input = test_data[case_to_test]['input'] |
| 32 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 33 | +puzzle_actual_result = 'Unknown' |
| 34 | + |
| 35 | + |
| 36 | +# -------------------------------- Actual code execution -------------------------------- # |
| 37 | + |
| 38 | +def list_remove (remove_list, element): |
| 39 | + try: |
| 40 | + remove_list.remove(element) |
| 41 | + return remove_list |
| 42 | + except ValueError: |
| 43 | + return remove_list |
| 44 | + |
| 45 | +if part_to_test == 1: |
| 46 | + predecessors = {} |
| 47 | + dots = [] |
| 48 | + for string in puzzle_input.split('\n'): |
| 49 | + _, source, _, _, _, _, _, target, *_ = string.split(' ') |
| 50 | + if not target in predecessors: |
| 51 | + predecessors[target] = [source] |
| 52 | + else: |
| 53 | + predecessors[target].append(source) |
| 54 | + |
| 55 | + dots.append(target) |
| 56 | + dots.append(source) |
| 57 | + |
| 58 | + dots = set(dots) |
| 59 | + |
| 60 | + path = '' |
| 61 | + while len(path) != len(dots): |
| 62 | + next_dot = sorted(x for x in dots if x not in predecessors and x not in path)[0] |
| 63 | + path += next_dot |
| 64 | + predecessors = {x:list_remove(predecessors[x], next_dot) for x in predecessors} |
| 65 | + predecessors = {x:predecessors[x] for x in predecessors if len(predecessors[x])} |
| 66 | + |
| 67 | + puzzle_actual_result = path |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +else: |
| 73 | + predecessors = {} |
| 74 | + dots = [] |
| 75 | + for string in puzzle_input.split('\n'): |
| 76 | + _, source, _, _, _, _, _, target, *_ = string.split(' ') |
| 77 | + if not target in predecessors: |
| 78 | + predecessors[target] = [source] |
| 79 | + else: |
| 80 | + predecessors[target].append(source) |
| 81 | + |
| 82 | + dots.append(target) |
| 83 | + dots.append(source) |
| 84 | + |
| 85 | + dots = set(dots) |
| 86 | + |
| 87 | + |
| 88 | + path = '' |
| 89 | + construction = [] |
| 90 | + tick = 0 |
| 91 | + while len(path) != len(dots): |
| 92 | + tick = 0 if len(construction) == 0 else min(x[2] for x in construction) |
| 93 | + finished = [x for x in construction if x[2] == tick] |
| 94 | + path += ''.join(x[0] for x in sorted(finished)) |
| 95 | + predecessors = {x:list(set(predecessors[x]) - set(path)) for x in predecessors} |
| 96 | + predecessors = {x:predecessors[x] for x in predecessors if len(predecessors[x])} |
| 97 | + |
| 98 | + construction = list(set(construction) - set(finished)) |
| 99 | + in_construction = [x[0] for x in construction] |
| 100 | + |
| 101 | + next_dots = sorted(x for x in dots if x not in predecessors and x not in path and x not in in_construction) |
| 102 | + workers_busy = sum(1 for worker in construction if worker[1] <= tick and worker[2] >= tick) |
| 103 | + |
| 104 | + if len(next_dots) and workers_busy < 5: |
| 105 | + next_dots = sorted(next_dots)[:5-workers_busy] |
| 106 | + construction += [(next_dot, tick, tick + ord(next_dot) - ord('A') + 60 + 1) for next_dot in next_dots] |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + puzzle_actual_result = tick |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +# -------------------------------- Outputs / results -------------------------------- # |
| 115 | + |
| 116 | +if verbose_level >= 3: |
| 117 | + print ('Input : ' + puzzle_input) |
| 118 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 119 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + |
0 commit comments