2017-12-13 - Packet Scanners
(original .ipynb)test_input_str = """0: 3 1: 2 4: 4 6: 4""" puzzle_input_str = open("puzzle_input/day13.txt").read() def parse_input(input_str): scanners = {} for line in input_str.split("\n"): r_str, d_str = line.split(": ") yield (int(r_str), int(d_str)) def caught(d, picoseconds): return (picoseconds == 0) or (picoseconds % (2*(d-1))) == 0 def total_severity(scanners, wait): return sum(r*d for r,d in scanners if caught(d, r+wait)) def part_one(input_str): scanners = parse_input(input_str) return total_severity(scanners, 0) assert 24 == part_one(test_input_str) print("part one:", part_one(puzzle_input_str))
part one: 1476
def part_two(input_str): scanners = list(parse_input(input_str)) wait = 0 while True: if all(not caught(d, r+wait) for r,d in scanners): return wait wait += 1 assert 10 == part_two(test_input_str) print("part two:", part_two(puzzle_input_str))
part two: 3937334