def main(): code = read_input(2020, 8) game_console = GameConsole.from_code(code) for i in range(len(game_console.instructions)): old_instruction = None game_console.reset() if isinstance(game_console.instructions[i], JmpInstruction): old_instruction = game_console.instructions[i] game_console.instructions[i] = NopInstruction( old_instruction.value) elif isinstance(game_console.instructions[i], NopInstruction): old_instruction = game_console.instructions[i] game_console.instructions[i] = JmpInstruction( old_instruction.value) game_console.run() if not game_console.infinite_loop: break if old_instruction is not None: game_console.instructions[i] = old_instruction print(game_console.acc)
def main(): mem = defaultdict(lambda: 0) for line in read_input(2020, 14): [command, value] = line.split(" = ") if command == "mask": mask = value else: match = re.search(r"mem\[(\d+)\]", command) if match: loc = bin(int(match.group(1))).replace("0b", "") loc = ("0" * 36) + loc loc = list(loc[-36:]) for i, bit in enumerate(mask): if bit == "0": continue else: loc[i] = bit loc = "".join(loc) for address in get_all_addresses(loc): mem[address] = int(value) print(sum(mem.values()))
def read_map_into_matrix(): """Read input into a usable map matrix.""" matrix = [] for line in read_input(2020, 3): matrix.append(list(line)) return matrix
def main(): ship = ComplexShip() for instruction in read_input(2020, 12): direction, units = instruction[0], int(instruction[1:]) ship.move(direction, units) print(sum(map(abs, ship.origin)))
def main(): lexer = CalcLexer() parser = BasicCalcParser() sum_ = 0 for line in read_input(2020, 18): sum_ += parser.parse(lexer.tokenize(line)) print(sum_)
def get_rules(): rules = {} for line in read_input(2020, 7): match = re.search(r"^([a-z]+ [a-z]+) bags", line) definition_name = match.group(1) matches = re.findall(r"(?:(no|\d+) ((?:[a-z]+ )?[a-z]+) bags?)+", line) matches = [match for match in matches if match[0] != "no"] rules[definition_name] = { color: int(number) for number, color in matches } return rules
def main(): mem = defaultdict(lambda: 0) for line in read_input(2020, 14): [command, value] = line.split(" = ") if command == "mask": or_mask = int(value.replace("X", "0"), 2) and_mask = int(value.replace("X", "1"), 2) continue else: match = re.search(r"mem\[(\d+)\]", command) if match: loc = int(match.group(1)) mem[loc] = int(value) & and_mask | or_mask print(sum(mem.values()))
def get_passwords_and_policies( ) -> Iterator[Tuple[str, Dict[str, Union[str, Dict[str, int]]]]]: """Get all of the passwords and their associated policies from the input text.""" for line in read_input(2020, 2): parts = line.split() [range_start, range_end] = parts[0].split("-") letter = parts[1].replace(":", "") yield ( parts[2], { "range": { "start": int(range_start), "end": int(range_end) }, "letter": letter, }, )
def main(): numbers = [int(number) for number in next(read_input(2020, 15)).split(",")] numbers_occurrences = { number: index + 1 for index, number in enumerate(numbers) } del numbers_occurrences[numbers[-1]] index = len(numbers) + 1 while index <= 2020: last_number = numbers[-1] if last_number not in numbers_occurrences: numbers.append(0) else: numbers.append(index - 1 - numbers_occurrences[last_number]) numbers_occurrences[last_number] = index - 1 index += 1 print(numbers[-1])
def main(): lines = read_input(2020, 13) _ = next(lines) buses = next(lines).split(",") increment = int(buses[0]) common = 0 for i in range(1, len(buses)): if buses[i] == "x": continue while True: common += increment # We found the correct number if (common + i) % int(buses[i]) == 0: # Start incrementing by the product of the current increment # and this bus - the next common time must be a multiple of this number increment *= int(buses[i]) break print(common)
def get_boarding_passes() -> Iterator[str]: return read_input(2020, 5)
def get_seat_grid(): if DEBUG: return [list(line) for line in test_input.strip().split("\n")] else: return [list(line) for line in read_input(2020, 11)]
def main(): code = read_input(2020, 8) game_console = GameConsole.from_code(code) game_console.run() print(game_console.acc)