aoc-2018-python

Advent of Code 2018 Solutions in Python
git clone https://git.sinitax.com/sinitax/aoc-2018-python
Log | Files | Refs | README | sfeed.txt

solve.py (1127B)


      1import sys
      2sys.path.append("../common")
      3import aoc
      4
      5data = aoc.data
      6recipes = [3, 7]
      7
      8def solve1(args):
      9    global recipes, data
     10
     11    end = int(data)
     12    workers = [i for i in range(2)]
     13    while len(recipes) < end + 10:
     14        recipes += [int(c) for c in str(sum(recipes[workers[i]] for i in range(len(workers))))]
     15        for i in range(len(workers)):
     16            workers[i] = (workers[i] + recipes[workers[i]]+1) % len(recipes)
     17    return "".join([str(x) for x in recipes[end:]])
     18
     19def solve2(args):
     20    global recipes, data
     21
     22    ilen = len(data)
     23    data = [int(c) for c in data]
     24    workers = [i for i in range(2)]
     25    stop = False
     26    counter = 0
     27    while not stop:
     28        for v in [int(c) for c in str(sum(recipes[workers[i]] for i in range(len(workers))))]:
     29            if recipes[-ilen:] == data:
     30                stop = True
     31                break
     32            recipes.append(v)
     33        for i in range(len(workers)):
     34            workers[i] = (workers[i] + recipes[workers[i]]+1) % len(recipes)
     35    return len(recipes) - ilen
     36
     37aoc.run(solve1, solve2, sols=["6107101544", 20291131])