Skip to content

Instantly share code, notes, and snippets.

@reynoldscem
Created May 25, 2018 22:51
Show Gist options
  • Save reynoldscem/5a47f6dde6f2068ccbe1306231528c46 to your computer and use it in GitHub Desktop.
Save reynoldscem/5a47f6dde6f2068ccbe1306231528c46 to your computer and use it in GitHub Desktop.
#!/bin/bash
TREE_SECTION_LENGTH=16
COLS=100
ROWS=63
idx=0
CURRENT_SEC_LENGTH=${TREE_SECTION_LENGTH}
declare -a BRANCH_POSITIONS
BRANCH_POSITIONS[1]=$(expr ${COLS} / 2 - 1)
declare -A GRID
for (( i=0 ; i<${ROWS} ; i++ ))
do
for (( j=0 ; j<${COLS} ; j++ ))
do
GRID[${i},${j}]="_"
done
done
# n from 1 to 5 inclusive.
read n
n_max=$(expr ${n} - 1)
for (( n=0 ; n<=${n_max} ; n++ ))
do
CURRENT_SEC_LENGTH=$(echo "${TREE_SECTION_LENGTH} / 2^${n}" | bc)
for (( i=1 ; i<=${CURRENT_SEC_LENGTH} ; i++ ))
do
for (( j=0 ; j<=${#BRANCH_POSITIONS[*]} ; j++ ))
do
GRID[${idx},${BRANCH_POSITIONS[j]}]=1
done
idx=$((idx + 1))
done
BRANCH_POSITIONS=($(echo ${BRANCH_POSITIONS[*]} ${BRANCH_POSITIONS[*]} | tr " " "\n" | sort -g | xargs))
for (( i=1 ; i<=${CURRENT_SEC_LENGTH} ; i++ ))
do
OFFSET=-1
for (( j=0 ; j<${#BRANCH_POSITIONS[*]} ; j++ ))
do
BRANCH_POSITIONS[j]=$((${BRANCH_POSITIONS[j]} + ${OFFSET}))
OFFSET=$((${OFFSET} * -1))
done
for (( j=0 ; j<${#BRANCH_POSITIONS[*]} ; j++ ))
do
GRID[${idx},${BRANCH_POSITIONS[j]}]=1
done
idx=$((idx+ 1))
done
done
for (( i=$(expr ${ROWS} - 1) ; i>=0 ; i-- ))
do
for (( j=0 ; j<${COLS} ; j++ ))
do
printf ${GRID[$i,$j]}
done
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment