human
I am working on the file "2017/iker/Ferran AoC 2017.ipynb".
The first task is:
Day 15
Generators are an esaier way to make iterators. In the end the result is similar
Can you write Python program for it?
assistant
#Here is the Python program you requested.

class FancyGen(object):
    
    def __init__(self, start, factor):
        self.start = start
        self.factor = factor
        self.q = 2147483647
        
    def __iter__(self):
        self.a = self.start
        return self

    def __next__(self):
        n = (self.a * self.factor) % self.q
        self.a = n
        return n

def compare_lowest_bits(n, m):
    n = n % (2 ** 16)
    m = m % (2 ** 16)
    return n == m

def duel(starta, startb):
    N = 40 * 10 ** 6
    count = 0
    gena = iter(FancyGen(starta, 16807))
    genb = iter(FancyGen(startb, 48271))
    for _ in range(N):
        if compare_lowest_bits(next(gena), next(genb)):
            count += 1
    return count

%%timeit
duel(65, 8921)

def generator(start_value, factor):
    val = start_value
    while True:
        val = val * factor % 2147483647
        yield val

def compare(start_A, start_B, rounds):
    matches = 0
    for i, values in enumerate(zip(generator(start_A, 16807), generator(start_B, 48271))):
        if i >= rounds:
            return matches
        else:
            vA, vB = values
            if vA.to_bytes(100, 'big')[-2:] == vB.to_bytes(100, 'big')[-2:]:
                matches += 1

%%timeit
compare(65, 8921, 40*10**6)