|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, grid, graph, dot, assembly, re, itertools, statistics |
| 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": """[({(<(())[]>[[{[]{<()<>> |
| 38 | +[(()[<>])]({[<{<<[]>>( |
| 39 | +{([(<{}[<>[]}>{[]{[(<()> |
| 40 | +(((({<>}<{<{<>}{[]{[]{} |
| 41 | +[[<[([]))<([[{}[[()]]] |
| 42 | +[{[{({}]{}}([{[{{{}}([] |
| 43 | +{<[[]]>}<{[{[{[]{()[[[] |
| 44 | +[<(<(<(<{}))><([]([]() |
| 45 | +<{([([[(<>()){}]>(<<{{ |
| 46 | +<{([{{}}[<[[[<>{}]]]>[]]""", |
| 47 | + "expected": ["26397", "288957"], |
| 48 | +} |
| 49 | + |
| 50 | +test = "real" |
| 51 | +input_file = os.path.join( |
| 52 | + os.path.dirname(__file__), |
| 53 | + "Inputs", |
| 54 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 55 | +) |
| 56 | +test_data[test] = { |
| 57 | + "input": open(input_file, "r+").read(), |
| 58 | + "expected": ["268845", "4038824534"], |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +# -------------------------------- Control program execution ------------------------- # |
| 63 | + |
| 64 | +case_to_test = "real" |
| 65 | +part_to_test = 2 |
| 66 | + |
| 67 | +# -------------------------------- Initialize some variables ------------------------- # |
| 68 | + |
| 69 | +puzzle_input = test_data[case_to_test]["input"] |
| 70 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 71 | +puzzle_actual_result = "Unknown" |
| 72 | + |
| 73 | + |
| 74 | +# -------------------------------- Actual code execution ----------------------------- # |
| 75 | + |
| 76 | +# Conver integer to 36-character binary |
| 77 | +# str_value = "{0:>036b}".format(value) |
| 78 | +# Convert binary string to number |
| 79 | +# value = int(str_value, 2) |
| 80 | + |
| 81 | + |
| 82 | +if part_to_test == 1: |
| 83 | + symbols = ["()", "[]", "<>", "{}"] |
| 84 | + opening_symbols = ["(", "[", "<", "{"] |
| 85 | + match = {"(": ")", "[": "]", "<": ">", "{": "}"} |
| 86 | + score = {")": 3, "]": 57, ">": 25137, "}": 1197} |
| 87 | + syntax_score = 0 |
| 88 | + for string in puzzle_input.split("\n"): |
| 89 | + for i in range(15): |
| 90 | + for symbol in symbols: |
| 91 | + string = string.replace(symbol, "") |
| 92 | + |
| 93 | + while string != "" and string[-1] in opening_symbols: |
| 94 | + string = string[:-1] |
| 95 | + |
| 96 | + if string == "": |
| 97 | + continue |
| 98 | + |
| 99 | + for i in range(len(string)): |
| 100 | + if string[i] in opening_symbols: |
| 101 | + last_character = string[i] |
| 102 | + else: |
| 103 | + if string[i] == match[last_character]: |
| 104 | + print("Cant compute") |
| 105 | + else: |
| 106 | + syntax_score += score[string[i]] |
| 107 | + break |
| 108 | + |
| 109 | + puzzle_actual_result = syntax_score |
| 110 | + |
| 111 | + |
| 112 | +else: |
| 113 | + symbols = ["()", "[]", "<>", "{}"] |
| 114 | + opening_symbols = ["(", "[", "<", "{"] |
| 115 | + match = {"(": ")", "[": "]", "<": ">", "{": "}"} |
| 116 | + score = {")": 1, "]": 2, ">": 4, "}": 3} |
| 117 | + all_scores = [] |
| 118 | + print_it = False |
| 119 | + for string in puzzle_input.split("\n"): |
| 120 | + syntax_score = 0 |
| 121 | + string2 = string |
| 122 | + # Determine whether it's an incomplete or erroneous line |
| 123 | + for i in range(10): |
| 124 | + for symbol in symbols: |
| 125 | + string2 = string2.replace(symbol, "") |
| 126 | + |
| 127 | + while string2 != "" and string2[-1] in opening_symbols: |
| 128 | + string2 = string2[:-1] |
| 129 | + |
| 130 | + if string2 != "": |
| 131 | + continue |
| 132 | + |
| 133 | + # Remove matching elements |
| 134 | + for i in range(15): |
| 135 | + for symbol in symbols: |
| 136 | + string = string.replace(symbol, "") |
| 137 | + |
| 138 | + missing_letters = "" |
| 139 | + for letter in string: |
| 140 | + if letter in match: |
| 141 | + missing_letters = match[letter] + missing_letters |
| 142 | + |
| 143 | + for letter in missing_letters: |
| 144 | + syntax_score *= 5 |
| 145 | + syntax_score += score[letter] |
| 146 | + |
| 147 | + all_scores.append(syntax_score) |
| 148 | + |
| 149 | + puzzle_actual_result = statistics.median(all_scores) |
| 150 | + |
| 151 | +# -------------------------------- Outputs / results --------------------------------- # |
| 152 | + |
| 153 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 154 | +print("Expected result : " + str(puzzle_expected_result)) |
| 155 | +print("Actual result : " + str(puzzle_actual_result)) |
| 156 | +# Date created: 2021-12-10 07:58:18.043288 |
| 157 | +# Part 1: 2021-12-10 08:06:21 |
| 158 | +# Part 2: 2021-12-10 08:30:02 |
0 commit comments