Skip to content

Instantly share code, notes, and snippets.

@izabera
Last active September 8, 2023 01:03
Show Gist options
  • Save izabera/156b519ad6f72595bd4e582ee7775cec to your computer and use it in GitHub Desktop.
Save izabera/156b519ad6f72595bd4e582ee7775cec to your computer and use it in GitHub Desktop.
cellular automata in sed
#!/bin/sed -f
# store rule in hold space
1 { h; d; }
# make borders wrap
s/\(.\)\(.*\)\(.\)/>\3\1\2\3\1|/
# do some mucking around that can probably be done more elegantly by someone who is actually good at sed
x; G; h; s/\n.*//; x
:loop
/>111/ { /^0/ bzero; bone; }
/>110/ { /^.0/ bzero; bone; }
/>101/ { /^..0/ bzero; bone; }
/>100/ { /^...0/ bzero; bone; }
/>011/ { /^....0/ bzero; bone; }
/>010/ { /^.....0/ bzero; bone; }
/>001/ { /^......0/ bzero; bone; }
/>000/ { /^.......0/ bzero; bone; }
bend
# output one character, move cursor forwards, repeat
:zero
s/>\(.\)\(.*\)/\1>\20/; bloop
:one
s/>\(.\)\(.*\)/\1>\21/; bloop
:end
s/.*|//
#!/bin/bash -e
trap 'rm fifo' exit
mkfifo fifo
exec {fifo}<>fifo
read -rep 'rule: ' rule
read -rep 'initial state: ' state
stdbuf -oL sed -f automata.sed < fifo |
{
echo "$rule" >&"$fifo"
echo "$state" >&"$fifo"
echo "$state"
while read -r state; do
sleep 1
echo "$state" >&"$fifo"
echo "$state"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment