Created
September 2, 2016 17:20
-
-
Save alibitek/d994e05a382892f2cba0060499f2748e to your computer and use it in GitHub Desktop.
Recursive trees - https://www.hackerrank.com/challenges/fractal-trees-all
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
read -r iterations | |
readonly iterations | |
declare -A arr | |
rows=63 | |
cols=100 | |
for ((i = 1; i <= rows; i++)); do | |
for ((j = 1; j <= cols; j++)); do | |
arr[$i,$j]=_ | |
done | |
done | |
function gen() { | |
local x=$1 | |
local y=$2 | |
local d=$3 | |
local h=$4 | |
local hh=$((h/2)) | |
if [[ $d -eq 0 ]]; then | |
return 1 | |
fi | |
for ((i = 1; i <= hh; i++)); do | |
arr[$(($y+$i)),$x]="1" | |
arr[$(($y+$hh+$i)),$(($x-$i))]="1" | |
arr[$(($y+$hh+$i)),$(($x+$i))]="1" | |
done | |
gen $((x-hh)) $((y+h)) $((d-1)) $hh | |
gen $((x+hh)) $((y+h)) $((d-1)) $hh | |
} | |
gen 50 0 $iterations 32 | |
for ((i = rows; i >= 0; i--)); do | |
for ((j = 1; j <= cols; j++)); do | |
echo -n "${arr[$i,$j]}" | |
done | |
echo -e | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment