From 709dff930bbc9b3dc9628a2afc94b3619335e763 Mon Sep 17 00:00:00 2001 From: Pierre Jeanjean Date: Thu, 9 Dec 2021 13:26:52 +0100 Subject: [PATCH] Day 9 --- 09/01.sh | 41 +++++++++++++++++++++++++++++++++++ 09/02.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100755 09/01.sh create mode 100755 09/02.sh diff --git a/09/01.sh b/09/01.sh new file mode 100755 index 0000000..135b76e --- /dev/null +++ b/09/01.sh @@ -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 diff --git a/09/02.sh b/09/02.sh new file mode 100755 index 0000000..4e9979d --- /dev/null +++ b/09/02.sh @@ -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]))