def main() -> None: groups = aoc.get_str(6).strip().split("\n\n") accumulator = 0 for group in groups: found_characters: Set[str] = set() for character in group: if character in string.ascii_lowercase: found_characters.add(character) accumulator += len(found_characters) print(accumulator) accumulator = 0 for group in groups: lines = group.split("\n") all_characters = [ character for character in lines[0] if character in string.ascii_lowercase ] for line in lines[1:]: for character in list(all_characters): if character not in line: all_characters.remove(character) accumulator += len(all_characters) print(accumulator)
def main() -> None: passports = aoc.get_str(4).rstrip().split("\n\n") fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid", "cid"] valid_passport_count = 0 for passport in passports: remaining_fields = [ field for field in fields if field not in [ field.split(":")[0] for field in passport.replace("\n", " ").split(" ") ] ] if remaining_fields == [] or remaining_fields == ["cid"]: valid_passport_count += 1 print(valid_passport_count) validated_passport_count = 0 for passport in passports: data = { field.split(":")[0]: field.split(":")[1] for field in passport.replace("\n", " ").split(" ") } validated_passport_count += (validate_byr(data) and validate_iyr(data) and validate_eyr(data) and validate_hgt(data) and validate_hcl(data) and validate_ecl(data) and validate_pid(data) and validate_cid(data)) print(validated_passport_count)
def main() -> None: graph = Graph(aoc.get_str(7).strip()) print( len([ rule for rule in graph.rules.values() if rule.contains_shiny_gold ])) print(graph.rules["shiny gold"].contained_bags)
def main() -> None: string = aoc.get_str(17).strip() world = World3D(string) for cycle in range(1, 7): world = world.next() print(world.count) world = World4D(string) for cycle in range(1, 7): world = world.next() print(world.count)
def main() -> None: seats = aoc.get_str(5).strip().split() seat_ids: List[int] = [] for seat in seats: row = int(seat[:7].replace("F", "0").replace("B", "1"), 2) col = int(seat[7:].replace("L", "0").replace("R", "1"), 2) seat_id = 8 * row + col seat_ids.append(seat_id) max_seat_id = max(seat_ids) print(max_seat_id) for seat_id in range(int(0.1 * max_seat_id), int(0.9 * max_seat_id)): if (seat_id - 1 in seat_ids and seat_id not in seat_ids and seat_id + 1 in seat_ids): print(seat_id)
def main() -> None: content = aoc.get_str(19).strip() rule_strings, lines = content.split("\n\n") rules: Dict[int, str] = { int(rule_string.split(": ", 1)[0]): transpile(rule_string.split(": ", 1)[1]) for rule_string in rule_strings.split("\n") } is_fulfilled = False while not is_fulfilled: is_fulfilled = True for rule_key, rule in rules.items(): for key, replacement_rule in rules.items(): rule = rule.replace(rule_key_for_rule_index(key), replacement_rule) rules[rule_key] = rule is_fulfilled = is_fulfilled and "<<" not in rule pattern = re.compile(f"^{rules[0]}$", re.MULTILINE) match_count = len(pattern.findall(lines)) print(match_count) ########################### rule_strings, lines = content.split("\n\n") rules: Dict[int, str] = { int(rule_string.split(": ", 1)[0]): transpile(rule_string.split(": ", 1)[1]) for rule_string in rule_strings.split("\n") } rules[8] = transpile("42 | 42 8") rules[11] = transpile("42 31 | 42 11 31") is_fulfilled = False max_depth = 4 # max(len(line) for line in lines.split("\n")) while not is_fulfilled and max_depth > 0: max_depth -= 1 is_fulfilled = True for rule_key, rule in rules.items(): for key, replacement_rule in rules.items(): rule = rule.replace(rule_key_for_rule_index(key), replacement_rule) rules[rule_key] = rule is_fulfilled = is_fulfilled and "<<" not in rule pattern = re.compile(f"^{rules[0]}$", re.MULTILINE) match_count = len(pattern.findall(lines)) print(match_count)
def main() -> None: starting_numbers = [ int(word) for word in aoc.get_str(15).strip().split(",") ] last_said: Dict[int, int] = {} delta: int for turn in tqdm(range(1, max(RELEVANTS))): number: int if turn <= len(starting_numbers): number = starting_numbers[turn - 1] else: number = delta delta = turn - last_said.get(number, turn) last_said[number] = turn if turn in RELEVANTS: tqdm.write(f"{turn} ==> {number}")
def main() -> None: blocks = aoc.get_str(16).strip().split("\n\n") fields: List[Field] = [] tickets: List[Ticket] = [] for line in blocks[0].split("\n"): field = Field(**parse.parse( "{title}: {from1:d}-{to1:d} or {from2:d}-{to2:d}", line).named) fields.append(field) ticket_error_count = 0 for line in [*blocks[1].split("\n")[1:], *blocks[2].split("\n")[1:]]: ticket = Ticket([int(word) for word in line.split(",")]) if not (errors := ticket.values_not_matching_any(fields)): tickets.append(ticket) else: ticket_error_count += sum(errors)
def main_verbose() -> None: string = aoc.get_str(17).strip() # string = ".#.\n..#\n###" world = World3D(string) print(world) for cycle in range(1, 7): world = world.next() print(f"\n==================== Cycle {cycle}\n") print(world) print(world.count) world = World4D(string) print(world) for cycle in range(1, 7): world = world.next() print(f"\n==================== Cycle {cycle}\n") print(world) print(world.count)
def main() -> None: content = aoc.get_str(20).strip() # content = "Tile 2311:\n..##.#..#.\n##..#.....\n#...##..#.\n####.#...#\n##.##.###.\n##...#.###\n.#.#.#..##\n..#....#..\n###...#.#.\n..###..###\n\nTile 1951:\n#.##...##.\n#.####...#\n.....#..##\n#...######\n.##.#....#\n.###.#####\n###.##.##.\n.###....#.\n..#.#..#.#\n#...##.#..\n\nTile 1171:\n####...##.\n#..##.#..#\n##.#..#.#.\n.###.####.\n..###.####\n.##....##.\n.#...####.\n#.##.####.\n####..#...\n.....##...\n\nTile 1427:\n###.##.#..\n.#..#.##..\n.#.##.#..#\n#.#.#.##.#\n....#...##\n...##..##.\n...#.#####\n.#.####.#.\n..#..###.#\n..##.#..#.\n\nTile 1489:\n##.#.#....\n..##...#..\n.##..##...\n..#...#...\n#####...#.\n#..#.#.#.#\n...#.#.#..\n##.#...##.\n..##.##.##\n###.##.#..\n\nTile 2473:\n#....####.\n#..#.##...\n#.##..#...\n######.#.#\n.#...#.#.#\n.#########\n.###.#..#.\n########.#\n##...##.#.\n..###.#.#.\n\nTile 2971:\n..#.#....#\n#...###...\n#.#.###...\n##.##..#..\n.#####..##\n.#..####.#\n#..#.#..#.\n..####.###\n..#.#.###.\n...#.#.#.#\n\nTile 2729:\n...#.#.#.#\n####.#....\n..#.#.....\n....#..#.#\n.##..##.#.\n.#.####...\n####.#.#..\n##.####...\n##..#.##..\n#.##...##.\n\nTile 3079:\n#.#.#####.\n.#..######\n..#.......\n######....\n####.#..#.\n.#...#.##.\n#.#####.##\n..#.###...\n..#.......\n..#.###..." world = World() tiles = [Tile(world, tile) for tile in content.split("\n\n")] anchor = tiles.pop(0) coordinate = Coordinate(0, 0) world[coordinate] = anchor placed_tiles = [anchor] while placed_tiles: anchor = placed_tiles.pop() for direction in [ Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, ]: if (tile := anchor.find_neighbor(tiles, direction)) is not None: placed_tiles.append(tile)
def main() -> None: content = aoc.get_str(19).strip() # content = '0: 4 1 5\n1: 2 3 | 3 2\n2: 4 4 | 5 5\n3: 4 5 | 5 4\n4: "a"\n5: "b"\n\nababbb\nbababa\nabbbab\naaabbb\naaaabbb' content = """ 42: 9 14 | 10 1 9: 14 27 | 1 26 10: 23 14 | 28 1 1: "a" 11: 42 31 5: 1 14 | 15 1 19: 14 1 | 14 14 12: 24 14 | 19 1 16: 15 1 | 14 14 31: 14 17 | 1 13 6: 14 14 | 1 14 2: 1 24 | 14 4 0: 8 11 13: 14 3 | 1 12 15: 1 | 14 17: 14 2 | 1 7 23: 25 1 | 22 14 28: 16 1 4: 1 1 20: 14 14 | 1 15 3: 5 14 | 16 1 27: 1 6 | 14 18 14: "b" 21: 14 1 | 1 14 25: 1 1 | 1 14 22: 14 14 8: 42 26: 14 22 | 1 20 18: 15 15 7: 14 5 | 1 21 24: 14 1 aaaaabbaabaaaaababaa""".strip() """ abbbbbabbbaaaababbaabbbbabababbbabbbbbbabaaaa bbabbbbaabaabba babbbbaabbbbbabbbbbbaabaaabaaa aaabbbbbbaaaabaababaabababbabaaabbababababaaa bbbbbbbaaaabbbbaaabbabaaa bbbababbbbaaaaaaaabbababaaababaabab ababaaaaaabaaab ababaaaaabbbaba baabbaaaabbaaaababbaababb abbbbabbbbaaaababbbbbbaaaababb aaaaabbaabaaaaababaa aaaabbaaaabbaaa aaaabbaabbaaaaaaabbbabbbaaabbaabaaa babaaabbbaaabaababbaabababaaab aabbbbbaabbbaaaaaabbbbbababaaaaabbaaabba""" content = """ 0: 1 0 1 | 1 1 | 2 1: 3 | 2 0 2: "b" 3: "a" baaa abaaaa """.strip() rule_strings, lines = content.split("\n\n") rules: Rules = {} rules.update({ int(rule_string.split(":", 1)[0]): Rule(rule_string.split(": ", 1)[1], rules) for rule_string in rule_strings.split("\n") }) for message in lines.split("\n"): print(message, rules[0].matches(message)) return print( len([ message for message in lines.split("\n") if rules[0].matches(message) ])) rules[8] = Rule("42 | 42 8", rules) rules[11] = Rule("42 31 | 42 11 31", rules) print( len([ message for message in lines.split("\n") if rules[0].matches(message) ]))
def main() -> None: groups = aoc.get_str(6).strip().split("\n\n") print(sum([len({c for c in g if c != "\n"}) for g in groups])) print(sum(len(set.intersection(*[set(l) for l in g.split("\n")])) for g in groups))