Files
advent-of-code-2021/09/01.sh
2021-12-09 13:26:52 +01:00

42 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
i_max=-1
j_max=0
locations=()
marked_locations=()
while read line; do
if [ $i_max -lt 0 ]; then
i_max=${#line}
fi
locations+=( $(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
marked_locations+=( 0 )
done
done
for i in $(seq 0 $((i_max-1))); do
for j in $(seq 0 $((j_max-1))); do
curr_location=${locations[$((i+j*i_max))]}
if { [ $i -gt 0 ] && [ $curr_location -ge ${locations[$((i-1+j*i_max))]} ] ;} \
|| { [ $i -lt $((i_max-1)) ] && [ $curr_location -ge ${locations[$((i+1+j*i_max))]} ] ;} \
|| { [ $j -gt 0 ] && [ $curr_location -ge ${locations[$((i+(j-1)*i_max))]} ] ;} \
|| { [ $j -lt $((j_max-1)) ] && [ $curr_location -ge ${locations[$((i+(j+1)*i_max))]} ] ;}; then
marked_locations[$((i+j*i_max))]=1
fi
done
done
sum=0
for i in $(seq 0 $((i_max-1))); do
for j in $(seq 0 $((j_max-1))); do
curr_location=${marked_locations[$((i+j*i_max))]}
if [ $curr_location -eq 0 ]; then
sum=$((sum+1+locations[i+j*i_max]))
fi
done
done
echo $sum