# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:adventofcode]
#     language: python
#     name: conda-env-adventofcode-py
# ---

# # Challenge 15

myinput = '/home/fmuinos/projects/adventofcode/2016/ferran/inputs/input15.txt'
myinput_test = '/home/fmuinos/projects/adventofcode/2016/ferran/inputs/input15-1.txt'

# ## Solution 15.1

# +
import math
import re

def setup(myinput):
    dd = {}
    p = re.compile('has \d+')
    q = re.compile('\d+\.')
    with open(myinput, 'rt') as f:
        i = 1
        for line in f:
            total = int(p.findall(line)[0].split()[1])
            t0 = int(q.findall(line)[0].rstrip('.'))
            dd[i] = (total, -(t0 + i) % total)
            i += 1
    return dd

def lcm(args):
    x = args.pop()
    if args == []:
        return x
    else:
        y = lcm(args)
        return x * y // math.gcd(x, y)

def sieve(dd):
    N, X = list(zip(*[dd[i] for i in sorted(dd)]))
    l = lcm(list(N))
    X = list(X)
    N = list(N)
    for i in range(len(dd)-1):        
        a = X[i] + N[i]
        while((a - X[i+1]) % N[i+1] != 0):
            a += N[i]
        X[i+1] = a
        N[i+1] *= N[i]
    while(a > 0):
        a -= l
    return a + l


# -

# ## Test

dd = setup(myinput_test)
print(dd)

sieve(dd)

# ## Result

dd = setup(myinput)
print(dd)

sieve(dd)

# ## Solution 15.2

disc = 7
total = 11
t0 = 0
dd.update({disc: (total, -(t0 + disc) % total)})
print(dd)

# ## Result

sieve(dd)

StarcoderdataJupyterScriptsDedupFiltered