Day 9
This commit is contained in:
41
09/01.sh
Executable file
41
09/01.sh
Executable file
@@ -0,0 +1,41 @@
|
||||
#!/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
|
||||
65
09/02.sh
Executable file
65
09/02.sh
Executable file
@@ -0,0 +1,65 @@
|
||||
#!/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 [ $curr_location -ne 9 ]; then
|
||||
marked_locations[$((i+j*i_max))]=$((marked_locations[i+j*i_max]+1))
|
||||
curr_i=$i
|
||||
curr_j=$j
|
||||
curr_min=$curr_location
|
||||
min_found=0
|
||||
while [ $min_found -eq 0 ]; do
|
||||
if [ $curr_i -gt 0 ] && [ ${locations[$((curr_i-1+curr_j*i_max))]} -lt $curr_min ]; then
|
||||
marked_locations[$((curr_i-1+curr_j*i_max))]=$((marked_locations[curr_i+curr_j*i_max]+marked_locations[curr_i-1+curr_j*i_max]))
|
||||
marked_locations[$((curr_i+curr_j*i_max))]=0
|
||||
curr_min=${locations[$((curr_i-1+curr_j*i_max))]}
|
||||
curr_i=$((curr_i-1))
|
||||
elif [ $curr_j -gt 0 ] && [ ${locations[$((curr_i+(curr_j-1)*i_max))]} -lt $curr_min ]; then
|
||||
marked_locations[$((curr_i+(curr_j-1)*i_max))]=$((marked_locations[curr_i+curr_j*i_max]+marked_locations[curr_i+(curr_j-1)*i_max]))
|
||||
marked_locations[$((curr_i+curr_j*i_max))]=0
|
||||
curr_min=${locations[$((curr_i+(curr_j-1)*i_max))]}
|
||||
curr_j=$((curr_j-1))
|
||||
elif [ $curr_i -lt $((i_max-1)) ] && [ ${locations[$((curr_i+1+curr_j*i_max))]} -lt $curr_min ]; then
|
||||
marked_locations[$((curr_i+1+curr_j*i_max))]=$((marked_locations[curr_i+curr_j*i_max]+marked_locations[curr_i+1+curr_j*i_max]))
|
||||
marked_locations[$((curr_i+curr_j*i_max))]=0
|
||||
curr_min=${locations[$((curr_i+1+curr_j*i_max))]}
|
||||
curr_i=$((curr_i+1))
|
||||
elif [ $curr_j -lt $((j_max-1)) ] && [ ${locations[$((curr_i+(curr_j+1)*i_max))]} -lt $curr_min ]; then
|
||||
marked_locations[$((curr_i+(curr_j+1)*i_max))]=$((marked_locations[curr_i+curr_j*i_max]+marked_locations[curr_i+(curr_j+1)*i_max]))
|
||||
marked_locations[$((curr_i+curr_j*i_max))]=0
|
||||
curr_min=${locations[$((curr_i+(curr_j+1)*i_max))]}
|
||||
curr_j=$((curr_j+1))
|
||||
else
|
||||
min_found=1
|
||||
fi
|
||||
done
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
sizes=()
|
||||
for marked_location in ${marked_locations[@]}; do
|
||||
if [ $marked_location -gt 0 ]; then
|
||||
sizes+=( $marked_location )
|
||||
fi
|
||||
done
|
||||
sizes=( $(printf '%s\n' "${sizes[@]}" | sort -nr) )
|
||||
echo $((sizes[0]*sizes[1]*sizes[2]))
|
||||
Reference in New Issue
Block a user