try: int(val) except: pass else: return True elif field == 'cid': return True return False def part1(lines): passports = [Passport(p) for p in lines.split('\n\n')] return len([p for p in passports if p.valid]) def part2(lines): passports = [Passport(p) for p in lines.strip().split('\n\n')] valid = [ p for p in passports if p.valid and p.valid_fields ] return len(valid) if __name__ == '__main__': DAY = 4 print(part1(aoc_input.read(DAY))) print(part2(aoc_input.read(DAY)))
input: str dims: Tuple[int, int] def count(self, char): return self.input.count(char) def part1(input): """ >>> import aoc_input as inp; part1(inp.read(8)) 1215 """ img = Image(input, (6, 25)) layer = img.fewest_layer('0') return layer.count('1') * layer.count('2') def part2(input): img = Image(input, (6, 25)) return str(img) if __name__ == '__main__': import doctest doctest.testmod() import aoc_input as inp DAY = 8 print(part1(inp.read(DAY))) print(part2(inp.read(DAY)))
from rich import print from bingo import BingoGame def part1(inp): game = BingoGame.from_str(inp) winning_num, winner = next(game.play_game()) print(winner) return winner.total() * winning_num def part2(inp): game = BingoGame.from_str(inp) print(f'{len(game.boards)}') winners = list(game.play_game()) print(f'{len(winners)} winning boards') last_num, last_winner = winners[-1] print(last_num * last_winner.total()) if __name__ == '__main__': from aoc_input import read print(part1(read(4))) print(part2(read(4)))