|
| 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": """NNCB |
| 38 | +
|
| 39 | +CH -> B |
| 40 | +HH -> N |
| 41 | +CB -> H |
| 42 | +NH -> C |
| 43 | +HB -> C |
| 44 | +HC -> B |
| 45 | +HN -> C |
| 46 | +NN -> C |
| 47 | +BH -> H |
| 48 | +NC -> B |
| 49 | +NB -> B |
| 50 | +BN -> B |
| 51 | +BB -> N |
| 52 | +BC -> B |
| 53 | +CC -> N |
| 54 | +CN -> C""", |
| 55 | + "expected": ["1588", "2188189693529"], |
| 56 | +} |
| 57 | + |
| 58 | +test = "real" |
| 59 | +input_file = os.path.join( |
| 60 | + os.path.dirname(__file__), |
| 61 | + "Inputs", |
| 62 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 63 | +) |
| 64 | +test_data[test] = { |
| 65 | + "input": open(input_file, "r+").read(), |
| 66 | + "expected": ["3259", "3459174981021"], |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | +# -------------------------------- Control program execution ------------------------- # |
| 71 | + |
| 72 | +case_to_test = "real" |
| 73 | +part_to_test = 2 |
| 74 | + |
| 75 | +# -------------------------------- Initialize some variables ------------------------- # |
| 76 | + |
| 77 | +puzzle_input = test_data[case_to_test]["input"] |
| 78 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 79 | +puzzle_actual_result = "Unknown" |
| 80 | + |
| 81 | + |
| 82 | +# -------------------------------- Actual code execution ----------------------------- # |
| 83 | + |
| 84 | +# Conver integer to 36-character binary |
| 85 | +# str_value = "{0:>036b}".format(value) |
| 86 | +# Convert binary string to number |
| 87 | +# value = int(str_value, 2) |
| 88 | + |
| 89 | +nb_counts = 10 if part_to_test == 1 else 40 |
| 90 | + |
| 91 | + |
| 92 | +# This was the first, obvious solution |
| 93 | +# Works well for part 1, not for part 2 |
| 94 | +# source = puzzle_input.split("\n\n")[0] |
| 95 | +# maps = puzzle_input.split("\n\n")[1] |
| 96 | +# mapping = {} |
| 97 | +# for string in maps.split("\n"): |
| 98 | +# mapping[string.split(' -> ')[0]] = string.split(' -> ')[1] + string[1] |
| 99 | + |
| 100 | +# word = source |
| 101 | +# for j in range(nb_counts): |
| 102 | +# target = word[0] |
| 103 | +# target += ''.join([mapping[word[i:i+2]] if word[i:i+2] in mapping else word[i+1] for i in range(len(word)-1)]) |
| 104 | + |
| 105 | +# word = target |
| 106 | + |
| 107 | + |
| 108 | +# occurrences = Counter(word) |
| 109 | +# print (occurrences) |
| 110 | +# puzzle_actual_result = max(occurrences.values()) - min(occurrences.values()) |
| 111 | + |
| 112 | + |
| 113 | +source = puzzle_input.split("\n\n")[0] |
| 114 | +maps = puzzle_input.split("\n\n")[1] |
| 115 | +mapping = {} |
| 116 | +for string in maps.split("\n"): |
| 117 | + mapping[string.split(" -> ")[0]] = string.split(" -> ")[1] |
| 118 | + |
| 119 | +elem_count = Counter(source) |
| 120 | +pair_count = defaultdict(int) |
| 121 | +for i in range(len(source) - 1): |
| 122 | + pair_count[source[i : i + 2]] += 1 |
| 123 | + |
| 124 | +print(elem_count, pair_count) |
| 125 | + |
| 126 | +for j in range(nb_counts): |
| 127 | + for pair, nb_pair in pair_count.copy().items(): |
| 128 | + pair_count[pair] -= nb_pair |
| 129 | + new_elem = mapping[pair] |
| 130 | + pair_count[pair[0] + new_elem] += nb_pair |
| 131 | + pair_count[new_elem + pair[1]] += nb_pair |
| 132 | + elem_count[new_elem] += nb_pair |
| 133 | + |
| 134 | + |
| 135 | +puzzle_actual_result = max(elem_count.values()) - min(elem_count.values()) |
| 136 | + |
| 137 | +# -------------------------------- Outputs / results --------------------------------- # |
| 138 | + |
| 139 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 140 | +print("Expected result : " + str(puzzle_expected_result)) |
| 141 | +print("Actual result : " + str(puzzle_actual_result)) |
| 142 | +# Date created: 2021-12-14 08:37:51.348152 |
| 143 | +# Part 1: 2021-12-14 08:42:56 |
| 144 | +# Part 2: 2021-12-14 08:56:13 |
0 commit comments