|
| 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 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +def lmap(func, *iterables): |
| 10 | + return list(map(func, *iterables)) |
| 11 | + |
| 12 | + |
| 13 | +def ints(s: str): |
| 14 | + return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +def positive_ints(s: str): |
| 18 | + return lmap(int, re.findall(r"\d+", s)) # thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +def floats(s: str): |
| 22 | + return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s)) |
| 23 | + |
| 24 | + |
| 25 | +def positive_floats(s: str): |
| 26 | + return lmap(float, re.findall(r"\d+(?:\.\d+)?", s)) |
| 27 | + |
| 28 | + |
| 29 | +def words(s: str): |
| 30 | + return re.findall(r"[a-zA-Z]+", s) |
| 31 | + |
| 32 | + |
| 33 | +test_data = {} |
| 34 | + |
| 35 | +test = 1 |
| 36 | +test_data[test] = { |
| 37 | + "input": """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1 |
| 38 | +
|
| 39 | +22 13 17 11 0 |
| 40 | + 8 2 23 4 24 |
| 41 | +21 9 14 16 7 |
| 42 | + 6 10 3 18 5 |
| 43 | + 1 12 20 15 19 |
| 44 | +
|
| 45 | + 3 15 0 2 22 |
| 46 | + 9 18 13 17 5 |
| 47 | +19 8 7 25 23 |
| 48 | +20 11 10 24 4 |
| 49 | +14 21 16 12 6 |
| 50 | +
|
| 51 | +14 21 17 24 4 |
| 52 | +10 16 15 9 19 |
| 53 | +18 8 23 26 20 |
| 54 | +22 11 13 6 5 |
| 55 | + 2 0 12 3 7""", |
| 56 | + "expected": ["4512", "1924"], |
| 57 | +} |
| 58 | + |
| 59 | +test = "real" |
| 60 | +input_file = os.path.join( |
| 61 | + os.path.dirname(__file__), |
| 62 | + "Inputs", |
| 63 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 64 | +) |
| 65 | +test_data[test] = { |
| 66 | + "input": open(input_file, "r+").read(), |
| 67 | + "expected": ["39984", "8468"], |
| 68 | +} |
| 69 | + |
| 70 | + |
| 71 | +# -------------------------------- Control program execution ------------------------- # |
| 72 | + |
| 73 | +case_to_test = "real" |
| 74 | +part_to_test = 2 |
| 75 | + |
| 76 | +# -------------------------------- Initialize some variables ------------------------- # |
| 77 | + |
| 78 | +puzzle_input = test_data[case_to_test]["input"] |
| 79 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 80 | +puzzle_actual_result = "Unknown" |
| 81 | + |
| 82 | + |
| 83 | +# -------------------------------- Actual code execution ----------------------------- # |
| 84 | + |
| 85 | +# Conver integer to 36-character binary |
| 86 | +# str_value = "{0:>036b}".format(value) |
| 87 | +# Convert binary string to number |
| 88 | +# value = int(str_value, 2) |
| 89 | + |
| 90 | + |
| 91 | +if part_to_test == 1: |
| 92 | + numbers_drawn = ints(puzzle_input.split("\n")[0]) |
| 93 | + |
| 94 | + cards_init = {} |
| 95 | + cards = {} |
| 96 | + |
| 97 | + for i, card in enumerate(puzzle_input.split("\n\n")[1:]): |
| 98 | + cards_init[i] = {} |
| 99 | + cards[i] = {} |
| 100 | + for r, row in enumerate(card.split("\n")): |
| 101 | + cards_init[i][r] = ints(row) |
| 102 | + cards[i][r] = ints(row) |
| 103 | + |
| 104 | + for n in numbers_drawn: |
| 105 | + cards = { |
| 106 | + i: {r: [c if c != n else "x" for c in cards[i][r]] for r in cards[i]} |
| 107 | + for i in cards |
| 108 | + } |
| 109 | + |
| 110 | + # Check rows |
| 111 | + for i in cards: |
| 112 | + for r in cards[i]: |
| 113 | + if cards[i][r] == ["x", "x", "x", "x", "x"]: |
| 114 | + winner_numbers = [ |
| 115 | + cards_init[i][row][col] |
| 116 | + for row in cards[i] |
| 117 | + for col in range(5) |
| 118 | + if cards[i][row][col] != "x" |
| 119 | + ] |
| 120 | + puzzle_actual_result = sum(winner_numbers) * int(n) |
| 121 | + break |
| 122 | + if puzzle_actual_result != "Unknown": |
| 123 | + break |
| 124 | + if puzzle_actual_result != "Unknown": |
| 125 | + break |
| 126 | + |
| 127 | + # Check columns |
| 128 | + for i in cards: |
| 129 | + for c in range(5): |
| 130 | + if all(cards[i][r][c] == "x" for r in range(5)): |
| 131 | + winner_numbers = [ |
| 132 | + cards_init[i][row][col] |
| 133 | + for row in cards[i] |
| 134 | + for col in range(5) |
| 135 | + if cards[i][row][col] != "x" |
| 136 | + ] |
| 137 | + puzzle_actual_result = sum(winner_numbers) * int(n) |
| 138 | + break |
| 139 | + if puzzle_actual_result != "Unknown": |
| 140 | + break |
| 141 | + if puzzle_actual_result != "Unknown": |
| 142 | + break |
| 143 | + |
| 144 | + |
| 145 | +else: |
| 146 | + numbers_drawn = ints(puzzle_input.split("\n")[0]) |
| 147 | + |
| 148 | + cards_init = {} |
| 149 | + cards = {} |
| 150 | + |
| 151 | + last_card = "Unknown" |
| 152 | + |
| 153 | + for i, card in enumerate(puzzle_input.split("\n\n")[1:]): |
| 154 | + cards_init[i] = {} |
| 155 | + cards[i] = {} |
| 156 | + for r, row in enumerate(card.split("\n")): |
| 157 | + cards_init[i][r] = ints(row) |
| 158 | + cards[i][r] = ints(row) |
| 159 | + |
| 160 | + for n in numbers_drawn: |
| 161 | + cards = { |
| 162 | + i: {r: [c if c != n else "x" for c in cards[i][r]] for r in cards[i]} |
| 163 | + for i in cards |
| 164 | + } |
| 165 | + |
| 166 | + # Check rows |
| 167 | + to_remove = [] |
| 168 | + for i in cards: |
| 169 | + for r in cards[i]: |
| 170 | + if cards[i][r] == ["x", "x", "x", "x", "x"]: |
| 171 | + to_remove.append(i) |
| 172 | + break |
| 173 | + |
| 174 | + # Check columns |
| 175 | + for i in cards: |
| 176 | + for c in range(5): |
| 177 | + if all(cards[i][r][c] == "x" for r in range(5)): |
| 178 | + to_remove.append(i) |
| 179 | + break |
| 180 | + |
| 181 | + if len(cards) == 1: |
| 182 | + last_card = list(cards.keys())[0] |
| 183 | + if last_card in to_remove: |
| 184 | + winner_numbers = [ |
| 185 | + cards_init[last_card][row][col] |
| 186 | + for row in range(5) |
| 187 | + for col in range(5) |
| 188 | + if cards[last_card][row][col] != "x" |
| 189 | + ] |
| 190 | + puzzle_actual_result = sum(winner_numbers) * int(n) |
| 191 | + break |
| 192 | + |
| 193 | + cards = {i: cards[i] for i in cards if i not in to_remove} |
| 194 | + |
| 195 | + |
| 196 | +# -------------------------------- Outputs / results --------------------------------- # |
| 197 | + |
| 198 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 199 | +print("Expected result : " + str(puzzle_expected_result)) |
| 200 | +print("Actual result : " + str(puzzle_actual_result)) |
| 201 | +# Date created: 2021-12-05 18:08:14.982011 |
| 202 | +# Part 1 : 2021-12-05 19:05:21 |
| 203 | +# Part 2 : 2021-12-05 19:16:15 |
0 commit comments