Interpreting the Wolfram Code

-2

0

Introduction

An elementary cellular automaton is a cellular automaton that is 1-dimensional and has 2 states, 1 and 0. These cellular automata are categorized based on a simple code: the Wolfram code, invented by Stephen Wolfram (yes, the Wolfram | Alpha guy). It works like this:

  • First, you the set of all possible states for a cell and its 2 neighbors; this gives you 8 (2^3) states. You then arrange them in order (in this case) from left to right, so you have 000, 001, 010, 011, 100, 101, 110, and 111.
  • Then, for each state, you take the resulting state of the center cell, either a 0 or 1. Then, you are left with 8 bits.
  • You can guess what you do next: you combine the 8 bits into a single 1-byte number. This gives you 255 possible cellular automatons.

Challenge

Given the Wolfram code number of a cellular automaton and a 3-bit state, you must write a program that will output the state of the center cell for that state.

  • The two integers may be taken from any input method that is convenient (ie. variable, stdin, command line arguments, function arguments) Input can be taken as any type, as long as that type is capable of representing integers. For example, you could take input as an array of digits, a string, or an integer. Input can be in any base you want.
  • The output may also be in any convenient method (ie. variable, stdout, exit code, return value) The only restriction is that the use must be able to see the output (no outputting to /dev/null). Output can be given as any type as well.

Example I/O

Number: 255, State: 101
States: 1, 1, 1, 1, 1, 1, 1, 1
Output: 1


Number: 129, State: 110
States: 1, 0, 0, 0, 0, 0, 0, 1
Output: 0


Number: 170, State: 001
States: 1, 0, 1, 0, 1, 0, 1, 0
Output: 0

Rules

This is , so shortest answer in bytes wins!

sugarfi

Posted 2019-12-10T12:51:19.800

Reputation: 1 239

1Now our task is to search for a duplicate of indexing. – None – 2019-12-10T13:36:12.680

2

You've had a number of questions that have been unclear, duplicates or turned out to be extremely trivial like this one. Have you considered posting your questions in the Sandbox for a couple of days in order to iron out some of the kinks beforehand?

– Jo King – 2019-12-12T23:54:12.997

Answers

7

Haskell, 4 bytes

The number is taken as a binary string and the state is taken as a (decimal) integer. This let us use the state as an index to the number, which immediately results in the desired output. So this answer just consists of an indexing function !!.

(!!)

Try it online!

flawr

Posted 2019-12-10T12:51:19.800

Reputation: 40 560

1

05AB1E, 1 byte

Based on @flawr's previous MATL answer. Remember to give them an upvote! (Note: this takes input in a reversed order.)

è

Try it online!

user85052

Posted 2019-12-10T12:51:19.800

Reputation:

Your TIO input is incorrect. It should be this instead. Right now you're indexing the 5 into the string "\"10000001\"" (coincidentally resulting in 0 as well). Here perhaps a better example to illustrate the current issue. Although I'm not sure why having those quotes around the first input somehow reverses the input-order (weird bug).. The first input should index into the second. So simply removing the quotes would use 10000001 to index into 5, which of course results in 5.

– Kevin Cruijssen – 2019-12-10T13:47:10.083

1

APL (Dyalog Classic), 8 bytes

I don't know how to set this to a callable function or turn it into a full program. (Based on @flawr's Haskell answer, remember to upvote them!)

Format: 'state' {⍺[⍵+1]} number

{⍺[⍵+1]}

Try it online!

user85052

Posted 2019-12-10T12:51:19.800

Reputation:

APL has a function for indexing, . You may also want to consider setting indexing to 0-based to better fit with the question – Jo King – 2019-12-11T11:16:53.737

1

Keg, 5 SBCS bytes

-2 bytes from Jono 2906 via the -hr flag

?(¿|_

Try it online!

user85052

Posted 2019-12-10T12:51:19.800

Reputation:

1

GolfScript, 2 bytes

~=

Try it online!

Explanation

~  # Dump the input onto the stack
 = # Index

user85052

Posted 2019-12-10T12:51:19.800

Reputation:

0

Wren, 17 bytes

Based on @flawr's previous JavaScript answer. Remember to give them an upvote!

Fn.new{|a,b|a[b]}

Try it online!

user85052

Posted 2019-12-10T12:51:19.800

Reputation:

0

JavaScript (Node.js), 34 bytes

(n)=>(s)=>n.toString(2)[+('0b'+s)]

Try it online!

Pretty sure the toString(2) call can be golfed better.

G0BLiN

Posted 2019-12-10T12:51:19.800

Reputation: 270