This commit is contained in:
dave 2018-12-11 22:29:24 -08:00
parent 3684b721d5
commit c12f177893
3 changed files with 166 additions and 0 deletions

71
12/a.py Executable file
View File

@ -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()

61
12/b.py Executable file
View File

@ -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()

34
12/input.txt Normal file
View File

@ -0,0 +1,34 @@
initial state: ##.##..#.#....#.##...###.##.#.#..###.#....##.###.#..###...#.##.#...#.#####.###.##..#######.####..#
.##.. => #
#...# => .
####. => #
##..# => #
..##. => .
.###. => .
..#.# => .
##### => .
##.#. => #
...## => #
.#.#. => .
##.## => #
#.##. => .
#.... => .
#..## => .
..#.. => #
.#..# => #
.#.## => #
...#. => .
.#... => #
###.# => #
#..#. => #
.#### => #
#.### => #
.##.# => #
#.#.. => .
###.. => #
..... => .
##... => .
....# => .
#.#.# => #
..### => #