9 filename="${1:-small.txt}"
11 step_display="${3:-1}"
18 while read -u 3 line; do
20 for (( a=0; a<$width; a++ )); do
21 octopusses+=(${line:$a:1})
30 echo $((($y*$width)+$x))
35 local x=$((offset%$width))
36 local y=$((offset/$width))
45 local offset=$(get_offset $x $y)
47 if [ "${flashed[$offset]+abc}" ]; then
52 for (( offsety=-1; offsety<=1; offsety++ )); do
53 for (( offsetx=-1; offsetx<=1; offsetx++ )); do
54 new_x=$(($x+$offsetx))
55 new_y=$(($y+$offsety))
56 if [ $new_x -eq $x ] && [ $new_y -eq $y ]; then
59 if [ $new_x -lt 0 ] || [ $new_x -ge $width ] || [ $new_y -lt 0 ] || [ $new_y -ge $height ]; then
62 # add one to the octopus
63 local __offset=$(get_offset $new_x $new_y)
64 ((octopusses[$__offset]+=1))
65 if [ ${octopusses[$__offset]} -gt 9 ]; then
73 for (( y=0; y<$height; y++ )); do
74 for (( x=0; x<$height; x++ )); do
75 if [ "${flashed[$(get_offset $x $y)]+abc}" ]; then
77 echo -n "${octopusses[$(get_offset $x $y)]}"
80 echo -n "${octopusses[$(get_offset $x $y)]}"
98 # first, add 1 to everything
99 for (( y=0; y<$height; y++ )); do
100 for (( x=0; x<$width; x++ )); do
101 offset=$(get_offset $x $y)
102 ((octopusses[$offset]+=1))
105 # now go back through, but only play with the flashes
106 for (( y=0; y<$height; y++ )); do
107 for (( x=0; x<$width; x++ )); do
108 offset=$(get_offset $x $y)
109 if [ ${octopusses[$offset]} -gt 9 ]; then
114 # and a quick second spin, just incase
115 for (( y=0; y<$height; y++ )); do
116 for (( x=0; x<$width; x++ )); do
117 offset=$(get_offset $x $y)
118 if [ ${octopusses[$offset]} -gt 9 ]; then
124 while [ "${#to_flash[@]}" -gt 0 ]; do
125 indexes=("${!to_flash[@]}")
127 flash_offset ${to_flash[$index]}
128 unset to_flash[$index]
131 for offset in "${!flashed[@]}"; do
133 octopusses[$offset]=0
136 if [ $((i % $step_display)) -eq 0 ]; then
142 if [ $(($width*$height)) -eq ${#flashed[@]} ]; then
146 for offset in "${!flashed[@]}"; do
147 unset flashed[$offset]
151 for (( i=1; i<=$iterations; i++ )); do
155 echo "total flashes for $iterations: $total_flashes"
158 while [ $all_flashed -eq -1 ]; do
163 echo "All flashed on step $i"