import sys sys.path.append("../common") import aoc lines = aoc.data.split("\n") istate = lines[0].split(": ")[1] rules = [l.split(" => ") for l in lines[2:] if l != ""] rules = [r[0] for r in rules if r[1] == "#"] def next_gen(pots): pmin = min(pots) pmax = max(pots) npots = list() for i in range(pmin-4, pmax+1): for r in rules: match = True for j in range(5): if (r[j] == "#") != ((i+j) in pots): match = False break if match: npots.append(i+2) break return npots def get_pots(): pots = list() for i in range(len(istate)): if istate[i] == "#": pots.append(i) return pots def solve1(args): pots = get_pots() for i in range(20): pots = next_gen(pots) return sum(pots) def solve2(args): pots = get_pots() psum = sum(pots) pdif = None i = 0 while (True): pots = next_gen(pots) i += 1 csum = sum(pots) if pdif == csum - psum: return csum + pdif * (50000000000 - 164) pdif = csum - psum psum = csum aoc.run(solve1, solve2, sols=[1987, 1150000000358])