This commit is contained in:
2021-12-11 15:32:18 +01:00
parent f2ef26fa6e
commit e8d8ecd468
2 changed files with 129 additions and 0 deletions

68
11/02.sh Executable file
View File

@@ -0,0 +1,68 @@
#!/bin/bash
i_max=-1
j_max=0
octopi=()
flashes=()
n_flashes=0
step=1
found_step=-1
while read line; do
if [ $i_max -lt 0 ]; then
i_max=${#line}
fi
octopi+=( $(sed 's/\([0-9]\)/\1 /g' <<< $line) )
j_max=$((j_max+1))
done
for i in $(seq 0 $((i_max-1))); do
for j in $(seq 0 $((j_max-1))); do
flashes+=( 0 )
done
done
check_flash () {
local i=$1
local j=$2
local i_2
local j_2
if [ ${octopi[$((i+j*i_max))]} -gt 9 ] && [ ${flashes[$((i+j*i_max))]} -eq 0 ]; then
n_flashes=$((n_flashes+1))
flashes[$((i+j*i_max))]=1
for i_2 in $(seq $((i-1)) $((i+1))); do
for j_2 in $(seq $((j-1)) $((j+1))); do
if [ $i_2 -ge 0 ] && [ $j_2 -ge 0 ] && [ $i_2 -lt $i_max ] && [ $j_2 -lt $j_max ]; then
octopi[$((i_2+j_2*i_max))]=$((octopi[i_2+j_2*i_max]+1))
check_flash $i_2 $j_2
fi
done
done
fi
}
while [ $found_step -eq -1 ]; do
n_flashes=0
for i in $(seq 0 $((i_max-1))); do
for j in $(seq 0 $((j_max-1))); do
octopi[$((i+j*i_max))]=$((octopi[i+j*i_max]+1))
flashes[$((i+j*i_max))]=0
done
done
for i in $(seq 0 $((i_max-1))); do
for j in $(seq 0 $((j_max-1))); do
check_flash $i $j
done
done
for i in $(seq 0 $((i_max-1))); do
for j in $(seq 0 $((j_max-1))); do
if [ ${octopi[$((i+j*i_max))]} -gt 9 ]; then
octopi[$((i+j*i_max))]=0
fi
done
done
if [ $n_flashes -eq $((i_max*j_max)) ]; then
found_step=$step
fi
step=$((step+1))
done
echo $found_step