-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractal.sh
76 lines (71 loc) · 4.37 KB
/
fractal.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#https://www.hackerrank.com/challenges/fractal-trees-all
read count
declare -A matrix
NCOLS=100
NROWS=63
NMAX=5
for rows in $(seq 0 $(( $NROWS - 1 )) )
do
for cols in $(seq 0 $(( $NCOLS - 1 )) )
do
matrix[$rows,$cols]="_"
done
done
MAX=$NROWS
mid_point=`echo "scale=0;$NCOLS/2 - 1"|bc -l`
init=1
function update() {
mp_array=$@
if [ $init -gt $count ]
then
return
fi
for mp in $mp_array
do
NLEN=`echo "scale=0;2 ^ ( $NMAX - $init )"|bc -l`
start=`echo "scale=0;$mp - $NLEN"|bc`
end=`echo "scale=0;$mp + $NLEN"|bc`
limit=`echo "2 * $NLEN "|bc -l`
while [ $limit -le $MAX ]
do
matrix[$limit,$start]=1
matrix[$limit,$end]=1
if [ $start -ne $end ]
then
start=`echo "scale=0;$start + 1"|bc -l`
end=`echo "scale=0;$end - 1"|bc -l`
fi
limit=`echo "$limit + 1"|bc -l`
done
done
MAX=`echo "2 * $NLEN"|bc -l`
init=`echo "scale=0;$init + 1"|bc -l`
if [ $init -le $count ]
then
if [ $init -eq 2 ]
then
update `echo "scale=0;$mid_point - ( 2 ^ ( $NMAX - 1 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) )"|bc -l`
fi
if [ $init -eq 3 ]
then
update `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 ))"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 ))"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 ))"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 ))"|bc -l`
fi
if [ $init -eq 4 ]
then
update `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) )"|bc -l`
fi
if [ $init -eq 5 ]
then
update `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX - 4 ))"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX - 4 ))"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0; $mid_point - ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX -4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX -4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX -4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) - ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX -4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) - ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) - ( 2 ^ ( $NMAX - 4 ) )"|bc -l` `echo "scale=0;$mid_point + ( 2 ^ ( $NMAX - 1 ) ) + ( 2 ^ ( $NMAX -2 )) + ( 2 ^ ( $NMAX -3 ) ) + ( 2 ^ ( $NMAX - 4 ) )"|bc -l`
fi
fi
}
update $mid_point
for nrows in $(seq 0 $(( $NROWS - 1 )))
do
for ncols in $(seq 0 $(( $NCOLS - 1 )))
do
echo -n "${matrix[$nrows,$ncols]}"
done
echo ""
done