aboutsummaryrefslogtreecommitdiffstats
path: root/src/14/solve.py
blob: a6c63a34d73f91e2bdaf4b442a8e193a9666ad89 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys
sys.path.append("../common")
import aoc

data = aoc.data
recipes = [3, 7]

def solve1(args):
    global recipes, data

    end = int(data)
    workers = [i for i in range(2)]
    while len(recipes) < end + 10:
        recipes += [int(c) for c in str(sum(recipes[workers[i]] for i in range(len(workers))))]
        for i in range(len(workers)):
            workers[i] = (workers[i] + recipes[workers[i]]+1) % len(recipes)
    return "".join([str(x) for x in recipes[end:]])

def solve2(args):
    global recipes, data

    ilen = len(data)
    data = [int(c) for c in data]
    workers = [i for i in range(2)]
    stop = False
    counter = 0
    while not stop:
        for v in [int(c) for c in str(sum(recipes[workers[i]] for i in range(len(workers))))]:
            if recipes[-ilen:] == data:
                stop = True
                break
            recipes.append(v)
        for i in range(len(workers)):
            workers[i] = (workers[i] + recipes[workers[i]]+1) % len(recipes)
    return len(recipes) - ilen

aoc.run(solve1, solve2, sols=["6107101544", 20291131])