previndexinfonext

code guessing, round #29 (completed)

started at ; stage 2 at ; ended at

specification

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.

APIs:

results

  1. 👑 farishafizhan +3 -0 = 3
    1. LyricLy
    2. olive
    3. IFcoltransG (was soup girl)
    4. soup girl (was IFcoltransG)
    5. Palaiologos
  2. soup girl +3 -0 = 3
    1. LyricLy
    2. olive
    3. IFcoltransG (was farishafizhan)
    4. farishafizhan (was IFcoltransG)
    5. Palaiologos
  3. LyricLy +3 -3 = 0
    1. olive
    2. farishafizhan (was soup girl)
    3. soup girl (was farishafizhan)
    4. IFcoltransG
    5. Palaiologos
  4. Palaiologos +2 -3 = -1
    1. LyricLy
    2. soup girl (was olive)
    3. farishafizhan (was soup girl)
    4. olive (was farishafizhan)
    5. IFcoltransG
  5. olive +1 -3 = -2
    1. farishafizhan (was LyricLy)
    2. Palaiologos (was soup girl)
    3. LyricLy (was farishafizhan)
    4. IFcoltransG
    5. soup girl (was Palaiologos)
  6. IFcoltransG +0 -3 = -3

    entries

    you can download all the entries

    entry #1

    written by LyricLy
    submitted at
    1 like

    guesses
    comments 0

    post a comment


    code.python ASCII text
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    # 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.txt Unicode 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.
    

    entry #2

    written by olive
    submitted at
    0 likes

    guesses
    comments 0

    post a comment


    cg29.lua ASCII text, with CRLF line terminators
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    -- n: u
    -- bit: u - 0-based index, starting at LSB
    --> bool  - true/false if bit is 1/0
    local function getbit(n, bit)
    	return 1 == (n/2^bit)%2 - (n/2^bit)%1
    end
    
    
    -- rule: u8 - Wolfram Code
    --> { [bool]={ [bool]={ [bool]=bool } } } - rule lookup, starting at left
    local function wolframrules(rule)
    	return {
    		[true]  = {
    			[true]  = {
    				[true]  = getbit(rule, 7),
    				[false] = getbit(rule, 6),
    			},
    			[false] = {
    				[true]  = getbit(rule, 5),
    				[false] = getbit(rule, 4),
    			},
    		},
    		[false] = {
    			[true]  = {
    				[true]  = getbit(rule, 3),
    				[false] = getbit(rule, 2),
    			},
    			[false] = {
    				[true]  = getbit(rule, 1),
    				[false] = getbit(rule, 0),
    			},
    		},
    	}
    end
    
    
    -- rule: u8             - Wolfram Code
    -- state: { bool, ... } - World state
    --> { bool, ... }       - Next state, same length as input
    return function(rule, state)
    	local newstate = {}
    	local rules = wolframrules(rule)
    	for i = 1,#state do
    		newstate[i] = rules
    			[state[i-1] or false]
    			[state[i]]
    			[state[i+1] or false]
    	end
    	return newstate
    end
    
    cg29.test.lua ASCII text, with CRLF line terminators
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    local entry = dofile("cg29.lua")
    
    local function matches(t1, t2) -- shallow list match
    	if #t1 ~= #t2 then return false end
    	for i = 1, #t1 do
    		if t1[i] ~= t2[i] then return false end
    	end
    	return true
    end
    
    local t,f = true,false
    assert(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}))
    

    entry #3

    written by soup girl
    submitted at
    1 like

    guesses
    comments 0

    post a comment


    steve.b ASCII text

    entry #4

    written by farishafizhan
    submitted at
    0 likes

    guesses
    comments 0

    post a comment


    round29.py ASCII text, with CRLF line terminators
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    def entry(rule, state):
        rule_dict = {
            '111': 0,
            '110': 0,
            '101': 0,
            '100': 0,
            '011': 0,
            '010': 0,
            '001': 0,
            '000': 0
        }
        rule = f'{rule:08b}'[::-1]
    
        for i in range(8):
            rule_dict[f'{i:03b}'] = bool(int(rule[i]))
        
        print(rule_dict)
    
        state = '0' + ''.join(['1' if i else '0' for i in state]) + '0'
    
        new_state = []
    
        for i in zip(state, state[1:], state[2:]):
            i = ''.join(i)
            new_state.append(rule_dict[i])
    
        return new_state
    

    entry #5

    written by IFcoltransG
    submitted at
    1 like

    guesses
    comments 0

    post a comment


    entry.py ASCII text
      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    from collections.abc import Set, Hashable
    
    
    def entry(rule, state):
        state = [False] + state + [False]
        state = [Mintoff.bool(x) for x in state]
        rule = Mintoff.int(rule)
        return [check(rule, *state[i-1:i+2]) for i in Mintoff.range(1, Mintoff.len(state) - 1)]
    
    
    def check(rule, l, c, r):
        index = r + (c << 1) + (l << 2)
        return bool((rule >> index) & 1)
    
    
    class Mintoff(Set, Hashable):
        def __init__(self, iterable=frozenset()):
            self.__inner = frozenset(iterable)
    
        @classmethod
        def int(cls, n):
            n = n.__index__()
            if n < 0:
                raise ValueError("< 0")
            x = cls()
            for _ in range(n):
                x |= {x}
            return x
    
        @classmethod
        def bool(cls, x):
            if x:
                return cls({cls()})
            return cls()
    
        @classmethod
        def pair(cls, left, right):
            return cls({cls({left}), cls({left, right})})
    
        @classmethod
        def len(cls, x):
            return cls.int(len(x))
    
        @classmethod
        def range(cls, start, stop=None, step=None):
            if stop is None:
                stop = start
                start = 0
            if step is None:
                return Mintoff.int(stop) - Mintoff.int(start)
            raise NotImplementedError
    
        def __repr__(self):
            try:
                return "Mintoff " + str(self.__index__())
            except ValueError:
                pass
            try:
                if len(self) == 2:
                    [left] = min(self)
                    [right] = max(self) - {left}
                    return "Mintoff " + str((left, right))
                if len(self) == 1:
                    [left] = min(self)
                    [right] = max(self)
                    return "Mintoff " + str((left, right))
            except (TypeError, ValueError):
                pass
            return "Mintoff " + str({*self})
    
        def __contains__(self, x):
            return x in self.__inner
    
        def __iter__(self):
            return iter(sorted(self.__inner))
    
        def __len__(self):
            return len(self.__inner)
    
        def __hash__(self):
            return hash(self.__inner)
    
        def __eq__(self, other):
            if isinstance(other, Set):
                return self.__inner == frozenset(other)
            return super().__eq__(other)
    
        def __add__(self, other):
            if isinstance(other, (int, Mintoff)):
                other = other.__index__()
                if other < 0:
                    return self - (-other)
                for _ in range(other):
                    self |= {self}
                return self
            return NotImplemented
    
        def __sub__(self, other):
            if isinstance(other, int):
                if other < 0:
                    return self + (-other)
                return self.int(self.__index__() - other)
            return super().__sub__(other)
    
        def __and__(self, other):
            if isinstance(other, int):
                return self.int(self.__index__() & other)
            return super().__and__(other)
    
        def __lshift__(self, other):
            if isinstance(other, (int, Mintoff)):
                other = other.__index__()
                for _ in self.range(other):
                    self += self.__index__()
                return self
            return NotImplemented
    
        def __rshift__(self, other):
            if isinstance(other, (int, Mintoff)):
                other = other.__index__()
                for _ in self.range(other):
                    self = self.int(self.__index__() // 2)
                return self
            return NotImplemented
    
        def __index__(self):
            n = 0
            while self != frozenset():
                decr = max(self)
                if decr != self - frozenset({decr}):
                    raise ValueError("NaN")
                self = decr
                n += 1
            return n
    
        __slots__ = '__inner'
    

    entry #6

    written by Palaiologos
    submitted at
    0 likes

    guesses
    comments 0

    post a comment


    wico.c ASCII text, with CRLF line terminators
    1
    2
    3
    4
    5
    6
    7
    8
    #include <stdio.h>
    
    int g(){
        int c=getchar();if(c=='0'||c=='1')return c-'0';if(c<0)exit(0);return g();}
    
    int main(void){
        int r;scanf("%d",&r);int tab[]={r&1,r&2,r&4,r&8,r&16,r&32,r&64,r&128};
        int a=g(),b=g(),c=g();for(;;){putchar('0'+!!tab[a*4+b*2+c]);a=b;b=c;c=g();}}