Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created April 26, 2023 03:11
Show Gist options
  • Save pmarreck/4182f4937fe0ba70fb1687e411504392 to your computer and use it in GitHub Desktop.
Save pmarreck/4182f4937fe0ba70fb1687e411504392 to your computer and use it in GitHub Desktop.
Stephen Wolfram's Rule 30 output in Bash, generated (mostly) via ChatGPT4
#!/usr/bin/env bash
# Get the terminal width
width=$(tput cols)
# Set number of output lines
LINES=${LINES:-10000}
# Initialize the first row with a single block character in the middle
row=$(printf "%-$((width / 2))s" " ")
row="${row}█${row}"
row=${row::width}
# Rule 30 in Bash!
rule30() {
local prev="$1"
local cur="$2"
local next="$3"
if [[ ( $prev == "█" && $cur == "█" && $next == "█" ) ||
( $prev == "█" && $cur == "█" && $next == " " ) ||
( $prev == "█" && $cur == " " && $next == "█" ) ||
( $prev == " " && $cur == " " && $next == " " ) ]]; then
echo " "
else
echo "█"
fi
}
# Print the cellular automaton
echo "$row"
for _ in $(seq $((LINES - 1))); do
new_row=""
for i in $(seq 0 $((width - 1))); do
prev=${row:$(( (i - 1 + width) % width )):1}
cur=${row:$i:1}
next=${row:$(( (i + 1) % width )):1}
new_row+=$(rule30 "$prev" "$cur" "$next")
done
row="$new_row"
echo "$row"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment