2017-12-15 - Dueling Generators
(original .ipynb)
from math import remainder
global_modulo = 2147483647
def create_generator(factor, value, multiple=1):
while True:
value = (value * factor) % global_modulo
if value % multiple == 0:
yield value
def l16_match(val_a, val_b):
return 0xffff & val_a == 0xffff & val_b
def part_one(start_a, start_b, iterations):
a = create_generator(16807, start_a)
b = create_generator(48271, start_b)
total = 0
while iterations > 0:
if l16_match(next(a), next(b)):
total += 1
iterations -= 1
return total
assert 1 == part_one(65, 8921, 5)
# assert 588 == part_one(65, 8921) #sloooow, but it works
# my puzzle input is:
# Generator A starts with 591
# Generator B starts with 393
print("part one:", part_one(591, 393, 40_000_000))
part one: 619
def part_two(start_a, start_b, iterations):
a = create_generator(16807, start_a, 4)
b = create_generator(48271, start_b, 8)
total = 0
while iterations > 0:
if l16_match(next(a), next(b)):
total += 1
iterations -= 1
return total
assert 0 == part_two(65, 8921, 5)
assert 1 == part_two(65, 8921, 1056)
print("part_two:", part_two(591, 393, 5_000_000))
part_two: 290