|
| 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": """mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X |
| 38 | +mem[8] = 11 |
| 39 | +mem[7] = 101 |
| 40 | +mem[8] = 0""", |
| 41 | + "expected": ["Unknown", "Unknown"], |
| 42 | +} |
| 43 | + |
| 44 | +test = 2 |
| 45 | +test_data[test] = { |
| 46 | + "input": """mask = 000000000000000000000000000000X1001X |
| 47 | +mem[42] = 100 |
| 48 | +mask = 00000000000000000000000000000000X0XX |
| 49 | +mem[26] = 1""", |
| 50 | + "expected": ["Unknown", "Unknown"], |
| 51 | +} |
| 52 | + |
| 53 | +test = "real" |
| 54 | +input_file = os.path.join( |
| 55 | + os.path.dirname(__file__), |
| 56 | + "Inputs", |
| 57 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 58 | +) |
| 59 | +test_data[test] = { |
| 60 | + "input": open(input_file, "r+").read(), |
| 61 | + "expected": ["Unknown", "Unknown"], |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +# -------------------------------- Control program execution ------------------------- # |
| 66 | + |
| 67 | +case_to_test = "real" |
| 68 | +part_to_test = 2 |
| 69 | + |
| 70 | +# -------------------------------- Initialize some variables ------------------------- # |
| 71 | + |
| 72 | +puzzle_input = test_data[case_to_test]["input"] |
| 73 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 74 | +puzzle_actual_result = "Unknown" |
| 75 | + |
| 76 | + |
| 77 | +# -------------------------------- Actual code execution ----------------------------- # |
| 78 | + |
| 79 | +if part_to_test == 1: |
| 80 | + data = puzzle_input.split("\n") |
| 81 | + |
| 82 | + memory = defaultdict(int) |
| 83 | + |
| 84 | + for string in data: |
| 85 | + if string[:4] == "mask": |
| 86 | + mask = string[7:] |
| 87 | + else: |
| 88 | + address, value = ints(string) |
| 89 | + # print ('{0:>036b}'.format(value)) |
| 90 | + for position, bit in enumerate(mask): |
| 91 | + if bit == "X": |
| 92 | + pass |
| 93 | + elif bit == "1": |
| 94 | + str_value = "{0:>036b}".format(value) |
| 95 | + str_value = str_value[:position] + "1" + str_value[position + 1 :] |
| 96 | + value = int(str_value, 2) |
| 97 | + elif bit == "0": |
| 98 | + str_value = "{0:>036b}".format(value) |
| 99 | + str_value = str_value[:position] + "0" + str_value[position + 1 :] |
| 100 | + value = int(str_value, 2) |
| 101 | + # print ('{0:>036b}'.format(value)) |
| 102 | + memory[address] = value |
| 103 | + |
| 104 | + puzzle_actual_result = sum(memory.values()) |
| 105 | + |
| 106 | + |
| 107 | +else: |
| 108 | + data = puzzle_input.split("\n") |
| 109 | + |
| 110 | + memory = defaultdict(int) |
| 111 | + |
| 112 | + for string in data: |
| 113 | + if string[:4] == "mask": |
| 114 | + mask = string[7:] |
| 115 | + else: |
| 116 | + address, value = ints(string) |
| 117 | + adresses = ["0"] |
| 118 | + for position, bit in enumerate(mask): |
| 119 | + if bit == "0": |
| 120 | + adresses = [ |
| 121 | + add + "{0:>036b}".format(address)[position] for add in adresses |
| 122 | + ] |
| 123 | + elif bit == "1": |
| 124 | + adresses = [add + "1" for add in adresses] |
| 125 | + elif bit == "X": |
| 126 | + adresses = [add + "1" for add in adresses] + [ |
| 127 | + add + "0" for add in adresses |
| 128 | + ] |
| 129 | + for add in set(adresses): |
| 130 | + memory[add] = value |
| 131 | + |
| 132 | + puzzle_actual_result = sum(memory.values()) |
| 133 | + |
| 134 | +# -------------------------------- Outputs / results --------------------------------- # |
| 135 | + |
| 136 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 137 | +print("Expected result : " + str(puzzle_expected_result)) |
| 138 | +print("Actual result : " + str(puzzle_actual_result)) |
| 139 | +# Date created: 2020-12-14 06:55:33.216654 |
| 140 | +# Part 1: 2020-12-14 07:11:07 |
| 141 | +# Part 2: 2020-12-14 07:17:27 |
0 commit comments