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 (1291B)


      1import sys
      2sys.path.append("../common")
      3import aoc
      4
      5lines = aoc.data.split("\n")
      6istate = lines[0].split(": ")[1]
      7rules = [l.split(" => ") for l in lines[2:] if l != ""]
      8rules = [r[0] for r in rules if r[1] == "#"]
      9
     10def next_gen(pots):
     11    pmin = min(pots)
     12    pmax = max(pots)
     13    npots = list()
     14    for i in range(pmin-4, pmax+1):
     15        for r in rules:
     16            match = True
     17            for j in range(5):
     18                if (r[j] == "#") != ((i+j) in pots):
     19                    match = False
     20                    break
     21            if match:
     22                npots.append(i+2)
     23                break
     24    return npots
     25
     26def get_pots():
     27    pots = list()
     28    for i in range(len(istate)):
     29        if istate[i] == "#":
     30            pots.append(i)
     31    return pots
     32
     33def solve1(args):
     34    pots = get_pots()
     35    for i in range(20):
     36        pots = next_gen(pots)
     37    return sum(pots)
     38
     39def solve2(args):
     40    pots = get_pots()
     41    psum = sum(pots)
     42    pdif = None
     43    i = 0
     44    while (True):
     45        pots = next_gen(pots)
     46        i += 1
     47        csum = sum(pots)
     48        if pdif == csum - psum:
     49            return csum + pdif * (50000000000 - 164)
     50        pdif = csum - psum
     51        psum = csum
     52
     53aoc.run(solve1, solve2, sols=[1987, 1150000000358])