From c12f1778937dbf9af53f217269daba2f242d0f5a Mon Sep 17 00:00:00 2001 From: dave Date: Tue, 11 Dec 2018 22:29:24 -0800 Subject: [PATCH] day 12 --- 12/a.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 12/b.py | 61 ++++++++++++++++++++++++++++++++++++++++++++ 12/input.txt | 34 +++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100755 12/a.py create mode 100755 12/b.py create mode 100644 12/input.txt diff --git a/12/a.py b/12/a.py new file mode 100755 index 0000000..f0da82a --- /dev/null +++ b/12/a.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + + +def printstate(state): + skeys = state.keys() + for key in range(-30, max(skeys) + 1): + try: + print("#" if state[key] else ".", end="") + except KeyError: + print(".", end="") + print() + + +def fmtrule(rule): + return "".join(["#" if c else "." for c in rule[0]]) + + +def main(): + state = {} + rules = [] # list of ((True, False, True, False, True), True) + with open("input.txt") as f: + ogstate = f.readline()[15:-1] + for i, c in enumerate(ogstate): + state[i] = c == "#" + f.readline() + for line in f.readlines(): + tf = [] + for char in line[0:5]: + tf.append(char == "#") + rules.append((tuple(tf), line[-2] == "#")) + + printstate(state) + + generations = 0 + while True: + newstate = {} + statekeys = state.keys() + for scankey in range(min(statekeys) - 2, max(statekeys) + 3): + for ruleid, rule in enumerate(rules): + matches = True + for i, oldgenkey in zip(range(0, 5), range(scankey - 2, scankey + 3)): + if (oldgenkey not in state and rule[0][i]): + matches = False + break + elif (oldgenkey in state and rule[0][i] != state[oldgenkey]): + matches = False + break + # try: + # if state[oldgenkey] != rule[0][i]: + # matches = False + # print("b1") + # break + # except KeyError as e: + # if rule[0][i]: + # print("b2") + # matches = False + # break + if matches and rule[1]: + newstate[scankey] = rule[1] + break + printstate(newstate) + state = newstate + generations += 1 + if generations >= 20: + break + + print(sum(state.keys())) + + +if __name__ == '__main__': + main() diff --git a/12/b.py b/12/b.py new file mode 100755 index 0000000..0b866ad --- /dev/null +++ b/12/b.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 + + +def main(): + state = {} + rules = [] # list of ((True, False, True, False, True), True) + with open("input.txt") as f: + ogstate = f.readline()[15:-1] + for i, c in enumerate(ogstate): + state[i] = c == "#" + f.readline() + for line in f.readlines(): + tf = [] + for char in line[0:5]: + tf.append(char == "#") + + if line[-2] != "#": # turns out these didn't matter...? + continue + rules.append((tuple(tf), line[-2] == "#")) + + generations = 0 + target = 50000000000 + lastvalues = [] + lastvalue = 0 + + while True: + newstate = {} + statekeys = state.keys() + for scankey in range(min(statekeys) - 2, max(statekeys) + 3): + for ruleid, rule in enumerate(rules): + matches = True + for i, oldgenkey in zip(range(0, 5), range(scankey - 2, scankey + 3)): + if (oldgenkey not in state and rule[0][i]): + matches = False + break + elif (oldgenkey in state and rule[0][i] != state[oldgenkey]): + matches = False + break + if matches: + newstate[scankey] = rule[1] + break + state = newstate + + value = sum(state.keys()) + lastvalues.append(value - lastvalue) + lastvalue = value + if len(lastvalues) > 5: + if len(set(lastvalues)) == 1: + print("Convergence on delta", lastvalues[0], "at generation", generations) + remaining = target - generations + print(value + lastvalues[0] * (remaining - 1)) + break + lastvalues.pop(0) + + generations += 1 + if generations >= target: + break + + +if __name__ == '__main__': + main() diff --git a/12/input.txt b/12/input.txt new file mode 100644 index 0000000..0d3ed28 --- /dev/null +++ b/12/input.txt @@ -0,0 +1,34 @@ +initial state: ##.##..#.#....#.##...###.##.#.#..###.#....##.###.#..###...#.##.#...#.#####.###.##..#######.####..# + +.##.. => # +#...# => . +####. => # +##..# => # +..##. => . +.###. => . +..#.# => . +##### => . +##.#. => # +...## => # +.#.#. => . +##.## => # +#.##. => . +#.... => . +#..## => . +..#.. => # +.#..# => # +.#.## => # +...#. => . +.#... => # +###.# => # +#..#. => # +.#### => # +#.### => # +.##.# => # +#.#.. => . +###.. => # +..... => . +##... => . +....# => . +#.#.# => # +..### => #