def run() -> None: with open_input(__file__) as file: lines = file.read().splitlines() for method in (value_masking, address_masking): memory = mutate_memory(lines, method) print(sum(memory.values()))
def run() -> None: with open_input(__file__) as file: lines: List[str] = file.read().splitlines() # part 1 print(sum(interpret_left_to_right(line) for line in lines)) # part 2 print(sum(interpret_swapped_add_and_mul(line) for line in lines))
def run() -> None: with open_input(__file__) as file: lines = list(file.readlines()) for validator in ( LetterRepeatsPasswordValidator, LetterAtPositionsPasswordValidator, ): n_valid_passwords = sum( validator(line).validate() for line in lines) print(n_valid_passwords)
def run() -> None: with open_input(__file__) as file: lines = file.read().splitlines() # part 1 seats = [BoardingPass.decode_from(line).seat for line in lines] print(max(seats)) # part 2 my_seat = one(get_free_seats(seats)) print(my_seat)
def run() -> None: with open_input(__file__) as file: sequence = list(map(int, file.read().splitlines())) # part 1 first_invalid_number = find_first_invalid_number_in(sequence, preamble_length=25) print(first_invalid_number) # part 2 subsequence = find_subsequence_that_sums_to(first_invalid_number, sequence, min_subsequence_length=2) print(min(subsequence) + max(subsequence))
def run() -> None: with open_input(__file__) as file: joltages = list(map(int, file.read().splitlines())) # part 1 valid_adapters_chain = next(chain for chain in permutations(sorted(joltages)) if is_valid_adapters_chain(chain)) differences_counts: t.Counter[int] = Counter( iterate_differences(valid_adapters_chain)) print(reduce(mul, differences_counts.values(), 1)) # part 2 print(count_possible_arrangements(joltages))
def run() -> None: with open_input(__file__) as file: lines = file.read().splitlines() # part 1 grid = CubicGrid.from_lines(lines, n_dims=3) for _ in range(6): grid = next(grid) print(grid.count_active()) # part 2 grid = CubicGrid.from_lines(lines, n_dims=4) for _ in range(6): grid = next(grid) print(grid.count_active())
def run() -> None: with open_input(__file__) as file: lines = file.read().splitlines() # part 1 state = ShipState() for line in lines: state = advance(state, line, use_waypoints=False) print(manhattan_distance(state.position)) # part 2 state = ShipState(orientation=10 * DIRECTIONS["E"] + 1 * DIRECTIONS["N"]) for line in lines: state = advance(state, line, use_waypoints=True) print(manhattan_distance(state.position))
def run() -> None: with open_input(__file__) as file: lines = file.read().splitlines() groups: List[List[str]] = list(split_at(lines, lambda line: line == "")) # part 1 result = sum( pipe(chain.from_iterable(group), set, len) for group in groups) # type: ignore print(result) # part 2 result = sum( len(reduce(set.intersection, map(set, group))) # type: ignore for group in groups) print(result)
def run(): with open_input(__file__) as file: lines = file.read().splitlines() lookup = defaultdict(dict) for line in lines: container, contents = map(str.strip, line.split("bags contain")) for content in map(str.strip, contents[:-1].split(",")): qty, *name, _ = content.split() name = " ".join(name).strip() if qty != "no": lookup[container][name] = int(qty) # part 1 print( sum( can_contain_bag_of_color(bag, "shiny gold", lookup) for bag in lookup)) # part 2 print(count_bags("shiny gold", lookup))
def run() -> None: with open_input(__file__) as file: lines = list(file.read().splitlines()) earliest_depart_t, buses = int(lines[0]), lines[1] # part 1 active_buses = [ int(bus_id) for bus_id in buses.split(",") if bus_id != "x" ] earliest_bus_id, earliest_bus_departure_t = min( ((bus_id, round_up(earliest_depart_t, bus_id)) for bus_id in active_buses), key=itemgetter(1), ) print(earliest_bus_id * (earliest_bus_departure_t - earliest_depart_t)) # part 2 buses = [(offset, int(bus_id)) for offset, bus_id in enumerate( bus for bus in buses.split(",")) if bus_id != "x"] print( crt( [bus_id for _, bus_id in buses], [bus_id - offset for offset, bus_id in buses], )[0])
def run() -> None: with open_input(__file__) as file: lines = file.read().splitlines() instructions = [ Instruction(opcode, int(argument)) for opcode, argument in map(str.split, lines) ] # part 1 print(get_state_after_first_repeated_execution(instructions).accumulator) # part 2 for opcode in {"nop", "jmp"}: for instruction_index, instruction in enumerate(instructions): if instruction.opcode == opcode: modified_instructions = [*instructions] modified_instructions[instruction_index] = Instruction( "nop" if instruction.opcode == "jmp" else "jmp", instruction.argument, ) state = get_state_after_termination(modified_instructions) if state: print(state.accumulator)