#!/bin/bash

# calculate the balls required for a given sold tetradecagon magnetic ball shape


# calculate the balls in a given sheet hexigon of a given pair of side dimensions
# (sides are x,y,x,y,x,y balls wide)
layer () {
local x y t i
x=$1
y=$2
#echo "calculating x=$x, y=$y"
t=0
for ((i=0;i<x;i++)) ; do
  ((a=y+i))
#echo "adding upper $a"
  ((t+=a))
done
for ((i=1;i<y;i++)) ; do
  ((a=y+x-1-i))
#echo "adding lower $a"
  ((t+=a))
done
((tt+=t))
#echo "Layer ($x,$y) requires $t balls, current total is $tt"
echo "Layer ($x,$y) requires $t balls"
}


echo


doorder () {
order=$1
tt=0

# this is the shape's order.  the number of balls wide the outer hexigons and squares are.

((lcount=order*3-2))

echo "Order $order tetradecagon is made of $lcount layers:"

#echo "stage 1"
for ((i=0;i<order-1;i++)) ; do
  layer $order $((order+i))
done

#echo "stage 2"
for ((i=0;i<order/2;i++)) ; do
  layer $((order+i)) $((order*2-i-1))
done

#echo "stage 3"
if [ $((order/2*2)) != $order ] ; then  #  odd order has one unique layer in them middle
  layer $((order*3/2)) $((order*3/2))
 fi

#echo "stage 4"
for ((i=order/2-1;i>=0;i--)) ; do
  layer $((order+i)) $((order*2-i-1))
done

#echo "stage 5"
for ((i=order-2;i>=0;i--)) ; do
  layer $order $((order+i))
done

((sets=(tt+215)/216))
((cost=sets*22))
echo "A total of $tt balls are required ($sets sets of 216, \$$cost)"
echo

}

for ((o=1;o<16;o++)) ; do
  doorder $o
done

echo
echo "I currently have $((231*15)) balls, and will be getting $((216*5)) more soon for a total of $((231*15+216*5)) balls"
echo
