From: Brett Parker Date: Sat, 5 Dec 2020 04:08:09 +0000 (+0000) Subject: Fix small breakages in bash versions X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/commitdiff_plain/0f422cf8b2cf81f77f11dfb69d386cc867e68ef5?ds=sidebyside Fix small breakages in bash versions --- diff --git a/day4/count_valid_passports.sh b/day4/count_valid_passports.sh index 17c7ffc..4a447ee 100644 --- a/day4/count_valid_passports.sh +++ b/day4/count_valid_passports.sh @@ -24,6 +24,18 @@ if [ "$cur_data" != "" ]; then data+=("$cur_data") fi +verify_data() { + local -n record=$1 + + for field in ${required_fields[@]}; do + if ! [ ${kvp[$field]+a} ]; then + exit 1 + fi + done + + exit 0 +} + for (( i=0; i<${#data[@]}; i++ )); do record="${data[$i]}" declare -A kvp @@ -32,12 +44,9 @@ for (( i=0; i<${#data[@]}; i++ )); do value=${field#*:} kvp[$key]=$value done - for field in ${required_fields[@]}; do - if ! [ ${kvp[$field]+a} ]; then - continue 2 - fi - done - valid_count=$((valid_count+1)) + if ( verify_data kvp ); then + valid_count=$((valid_count+1)) + fi unset kvp done diff --git a/day4/count_valid_passports_2.sh b/day4/count_valid_passports_2.sh index 6eb2b08..88fc560 100644 --- a/day4/count_valid_passports_2.sh +++ b/day4/count_valid_passports_2.sh @@ -128,6 +128,17 @@ while read -u 3 line; do seperator=" " done +check_fields() { + local -n record=$1 + for field in ${required_fields[@]}; do + if ! [ ${record[$field]+a} ]; then + exit 1 + fi + done + + exit 0 +} + # on the last line, if cur_data isn't empty, add that to the array if [ "$cur_data" != "" ]; then data+=("$cur_data") @@ -141,13 +152,10 @@ for (( i=0; i<${#data[@]}; i++ )); do value=${field#*:} kvp[$key]=$value done - for field in ${required_fields[@]}; do - if ! [ ${kvp[$field]+a} ]; then - continue 2 + if ( check_fields kvp ); then + if ( check_data kvp ); then + valid_count=$((valid_count+1)) fi - done - if ( check_data kvp ); then - valid_count=$((valid_count+1)) fi unset kvp done