X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/blobdiff_plain/1ae258d79ba91cb6215a62a011fca7936c06d9df..0f422cf8b2cf81f77f11dfb69d386cc867e68ef5:/day4/count_valid_passports_2.sh 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