return keys == set(required_passport_keys) def is_valid(passport: dict) -> bool: for key in passport: if not required_passport_keys[key](passport[key]): return False return True passports = [] # Parse passports passport = {} for line in read_lines("day04"): if not line.strip(): passports.append(passport) passport = {} continue for pair in line.split(" "): passport[pair.split(":")[0]] = pair.split(":")[1] passports.append(passport) # Part one count_valid_passports = len([x for x in passports if is_present(x)]) aoc_print( f"The passport list contains {count_valid_passports} valid passports.")
from assertions import assert_equals from input_reader import read_lines from printer import aoc_print lines = read_lines("day02") # Part one valid_passwords = 0 for line in lines: parts = line.split(" ") lower_bound = int(parts[0].split("-")[0]) upper_bound = int(parts[0].split("-")[1]) letter = parts[1][:-1] password = parts[2] letter_count = len([x for x in password if x == letter]) if lower_bound <= letter_count <= upper_bound: valid_passwords += 1 aoc_print(f"{valid_passwords} passwords are valid. (old interpretation)") assert_equals(424, valid_passwords) # Part two valid_passwords = 0 for line in lines:
def terminates(self) -> bool: executed_lines = set() while self.instruction_pointer not in executed_lines: executed_lines.add(program.instruction_pointer) if self.instruction_pointer >= len(self.instructions): return True self.run() return False instructions = read_lines("day08") program = Program(instructions) # Part one program.terminates() aoc_print( f"The program gets interrupted with an accumulator of {program.accumulator}." ) assert_equals(1801, program.accumulator) # Part two def replace_all(base_list: [str], old: str, new: str) -> [[str]]: total = list() index = 0
if contains_bag(bag[0], needle): return True return False def count_bags(root: str) -> int: total_sum = 0 for rule in bag_rules[root]: total_sum += rule[1] + rule[1] * count_bags(rule[0]) return total_sum # Parse input (plz forgive me python god and regex skiller) rule_list = read_lines("day07") # {str, [(int,str)]} bag_rules = {} for rule in rule_list: rule = rule[:-1].replace("bags", "").replace("bag", "") bag = rule.split(" contain ")[0].strip() container_bags = [] container = rule.split(" contain ")[1].strip() if "," in container: for single_bag in container.split(", "): count = int(single_bag.split(" ")[0]) bag_type = single_bag[len(single_bag.split(" ")[0]):].strip()
upper_bound = math.floor((lower_bound + upper_bound) / 2) elif letter == upper_char: lower_bound = math.ceil((lower_bound + upper_bound) / 2) return lower_bound def calc_seat(line: str) -> (int, int): return binary_search(line[0:7], 0, 127, "F", "B"), binary_search(line[7:10], 0, 7, "L", "R") def calc_seat_id(seat: (int, int)) -> int: return seat[0] * 8 + seat[1] lines = read_lines("day05") # Calculate seats seats = list(map(calc_seat, lines)) # Part one result = max([calc_seat_id(seat) for seat in seats]) aoc_print(f"The highest seat ID on the boarding pass is {result}.") assert_equals(878, result) # Part two sorted_seats = sorted(seats, key=lambda tup: (tup[0], tup[1])) min_seat = min(sorted_seats) max_seat = max(sorted_seats)
def puzzle2(): masses = (int(mass) for mass in read_lines('day1input.txt')) return sum(get_total_required_fuel(mass) for mass in masses)