guys holy shit I think Stephen Wolfram is in my house right now. your challenge is to simulate elementary cellular automata. submissions may be written in python, c, lua, brainfuck, or 1+.
an elementary cellular automaton is a simple kind of one-dimensional cellular automaton. there are exactly 2 states, and each cell's state in the next generation depends only on its state and that of its two neighbours.
each elementary cellular automaton can be described uniquely by assigning a single bit to each of the 8 possible configurations of a cell and its two neighbors, like so:
111 110 101 100 011 010 001 000
0 1 1 0 1 1 1 0
the resulting sequence of bits can be interpreted as a number from 0 to 255, giving rise to a common scheme for naming these rules. for example, the rule shown above is called rule 110.
your challenge, given a rule as a single byte encoded in this way and a sequence of booleans representing the state of the board, is to return the result after applying the rule for a single generation.
for example:
rule: 30
input: 0110010
output: 1101111
the first and last elements of your input will always be 0, and your output should be the same length as the input.
# I'm sorry this code is so plain,
# It's not as fancy as a sonnet or a refrain.
# I tried to make it smart and clever,
# But it ended up being simple and brief as ever.
# I hope you can forgive me for this oversight,
# And find some value in this humble script.
from more_itertools import stagger
def entry(rule, state):
return [rule >> (x*4 + y*2 + z) & 1 for x, y, z in stagger(state, (-1, 0, 1), longest=True, fillvalue=0)][:-1]
sorry.txtUnicode text, UTF-8 text, with very long lines (872)
1
I had a better solution for this task, but I was unable to complete it because my time machine malfunctioned and I was accidentally transported back to the year 1518. I found myself in a small town in Germany called Görlitz, and I quickly realized that I didn't have my laptop with me. I spent the next few days trying to figure out how to get back to the present, but I was unable to find the necessary components to repair the time machine. I even tried to explain the situation to the locals, but they didn't speak English and I didn't know how to speak German. Eventually, I was able to make contact with another group of time travelers who were able to help me return to the present. But by the time I got back, the deadline for the challenge was already over. I apologize for the uncreative solution and I hope you can understand why I was unable to finish my entry.
localentry=dofile("cg29.lua")localfunctionmatches(t1,t2)-- shallow list matchif#t1~=#t2thenreturnfalseendfori=1,#t1doift1[i]~=t2[i]thenreturnfalseendendreturntrueendlocalt,f=true,falseassert(matches(entry(30,{f,t,t,f,f,t,f}),{t,t,f,t,t,t,t}))assert(matches(entry(72,{f,f,f,t,t,f,f,t,f}),{f,f,f,t,t,f,f,f,f}))
post a comment