|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, grid, graph, dot, assembly, re, itertools |
| 3 | +from collections import Counter, deque, defaultdict |
| 4 | + |
| 5 | +from compass import * |
| 6 | +from copy import deepcopy |
| 7 | + |
| 8 | + |
| 9 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 10 | +def lmap(func, *iterables): |
| 11 | + return list(map(func, *iterables)) |
| 12 | + |
| 13 | + |
| 14 | +def ints(s: str): |
| 15 | + return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano! |
| 16 | + |
| 17 | + |
| 18 | +def positive_ints(s: str): |
| 19 | + return lmap(int, re.findall(r"\d+", s)) # thanks mserrano! |
| 20 | + |
| 21 | + |
| 22 | +def floats(s: str): |
| 23 | + return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s)) |
| 24 | + |
| 25 | + |
| 26 | +def positive_floats(s: str): |
| 27 | + return lmap(float, re.findall(r"\d+(?:\.\d+)?", s)) |
| 28 | + |
| 29 | + |
| 30 | +def words(s: str): |
| 31 | + return re.findall(r"[a-zA-Z]+", s) |
| 32 | + |
| 33 | + |
| 34 | +test_data = {} |
| 35 | + |
| 36 | +test = 1 |
| 37 | +test_data[test] = { |
| 38 | + "input": """class: 1-3 or 5-7 |
| 39 | +row: 6-11 or 33-44 |
| 40 | +seat: 13-40 or 45-50 |
| 41 | +
|
| 42 | +your ticket: |
| 43 | +7,1,14 |
| 44 | +
|
| 45 | +nearby tickets: |
| 46 | +7,3,47 |
| 47 | +40,4,50 |
| 48 | +55,2,20 |
| 49 | +38,6,12""", |
| 50 | + "expected": ["71", "Unknown"], |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +test = 2 |
| 55 | +test_data[test] = { |
| 56 | + "input": """class: 0-1 or 4-19 |
| 57 | +row: 0-5 or 8-19 |
| 58 | +seat: 0-13 or 16-19 |
| 59 | +
|
| 60 | +your ticket: |
| 61 | +11,12,13 |
| 62 | +
|
| 63 | +nearby tickets: |
| 64 | +3,9,18 |
| 65 | +15,1,5 |
| 66 | +5,14,9""", |
| 67 | + "expected": ["Unknown", "row, class, seat ==> 0"], |
| 68 | +} |
| 69 | + |
| 70 | +test = "real" |
| 71 | +input_file = os.path.join( |
| 72 | + os.path.dirname(__file__), |
| 73 | + "Inputs", |
| 74 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 75 | +) |
| 76 | +test_data[test] = { |
| 77 | + "input": open(input_file, "r+").read(), |
| 78 | + "expected": ["32835", "Unknown"], |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +# -------------------------------- Control program execution ------------------------- # |
| 83 | + |
| 84 | +case_to_test = "real" |
| 85 | +part_to_test = 2 |
| 86 | + |
| 87 | +# -------------------------------- Initialize some variables ------------------------- # |
| 88 | + |
| 89 | +puzzle_input = test_data[case_to_test]["input"] |
| 90 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 91 | +puzzle_actual_result = "Unknown" |
| 92 | + |
| 93 | + |
| 94 | +# -------------------------------- Actual code execution ----------------------------- # |
| 95 | +validations = {} |
| 96 | + |
| 97 | +section = 0 |
| 98 | +tickets = [] |
| 99 | + |
| 100 | +for string in puzzle_input.split("\n"): |
| 101 | + if string == "": |
| 102 | + section += 1 |
| 103 | + else: |
| 104 | + if section == 0: |
| 105 | + field, numbers = string.split(": ") |
| 106 | + numbers = positive_ints(numbers) |
| 107 | + validations[field] = list(range(numbers[0], numbers[1] + 1)) + list( |
| 108 | + range(numbers[2], numbers[3] + 1) |
| 109 | + ) |
| 110 | + elif section == 1: |
| 111 | + if string == "your ticket:": |
| 112 | + pass |
| 113 | + else: |
| 114 | + my_ticket = ints(string) |
| 115 | + elif section == 2: |
| 116 | + if string == "nearby tickets:": |
| 117 | + pass |
| 118 | + else: |
| 119 | + tickets.append(ints(string)) |
| 120 | + |
| 121 | +if part_to_test == 1: |
| 122 | + invalid_fields = 0 |
| 123 | + for ticket in tickets: |
| 124 | + invalid_fields += sum( |
| 125 | + [ |
| 126 | + field |
| 127 | + for field in ticket |
| 128 | + if all(field not in val for val in validations.values()) |
| 129 | + ] |
| 130 | + ) |
| 131 | + |
| 132 | + puzzle_actual_result = invalid_fields |
| 133 | + |
| 134 | +else: |
| 135 | + valid_tickets = [] |
| 136 | + invalid_fields = 0 |
| 137 | + for ticket in tickets: |
| 138 | + if ( |
| 139 | + len( |
| 140 | + [ |
| 141 | + field |
| 142 | + for field in ticket |
| 143 | + if all(field not in val for val in validations.values()) |
| 144 | + ] |
| 145 | + ) |
| 146 | + == 0 |
| 147 | + ): |
| 148 | + valid_tickets.append(ticket) |
| 149 | + |
| 150 | + field_order = {} |
| 151 | + for field in validations.keys(): |
| 152 | + possible_order = list(range(len(validations))) |
| 153 | + allowed_values = validations[field] |
| 154 | + for position in range(len(validations)): |
| 155 | + for ticket in valid_tickets: |
| 156 | + # #print (field, ticket, position, possible_order, allowed_values) |
| 157 | + value = ticket[position] |
| 158 | + if value not in allowed_values: |
| 159 | + try: |
| 160 | + possible_order.remove(position) |
| 161 | + except ValueError: |
| 162 | + pass |
| 163 | + field_order[field] = possible_order |
| 164 | + |
| 165 | + for val in field_order: |
| 166 | + print(field_order[val], val) |
| 167 | + while any(len(val) > 1 for val in field_order.values()): |
| 168 | + new_field_order = deepcopy(field_order) |
| 169 | + for field in field_order: |
| 170 | + if len(field_order[field]) == 1: |
| 171 | + for field2 in new_field_order: |
| 172 | + if field2 == field: |
| 173 | + pass |
| 174 | + else: |
| 175 | + new_field_order[field2] = [ |
| 176 | + val |
| 177 | + for val in new_field_order[field2] |
| 178 | + if val not in field_order[field] |
| 179 | + ] |
| 180 | + field_order = deepcopy(new_field_order) |
| 181 | + |
| 182 | + ticket_value = 1 |
| 183 | + for val in field_order: |
| 184 | + print(field_order[val], val) |
| 185 | + for field in validations.keys(): |
| 186 | + if field[:9] == "departure": |
| 187 | + print( |
| 188 | + my_ticket, field, field_order[field], my_ticket[field_order[field][0]] |
| 189 | + ) |
| 190 | + ticket_value *= my_ticket[field_order[field][0]] |
| 191 | + |
| 192 | + puzzle_actual_result = ticket_value |
| 193 | + |
| 194 | + |
| 195 | +# -------------------------------- Outputs / results --------------------------------- # |
| 196 | + |
| 197 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 198 | +print("Expected result : " + str(puzzle_expected_result)) |
| 199 | +print("Actual result : " + str(puzzle_actual_result)) |
| 200 | +# Date created: 2020-12-16 06:05:34.085933 |
| 201 | +# Part 1: 2020-12-16 06:23:05 |
| 202 | +# Part 2: 2020-12-16 06:59:59 |
0 commit comments