]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/commitdiff
Fix small breakages in bash versions
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Sat, 5 Dec 2020 04:08:09 +0000 (04:08 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Sat, 5 Dec 2020 04:08:09 +0000 (04:08 +0000)
day4/count_valid_passports.sh
day4/count_valid_passports_2.sh

index 17c7ffc77aa6aabac36c2098efaf90e2b886ee2b..4a447ee652b10a4dfa91009853f8cad44df1324e 100644 (file)
@@ -24,6 +24,18 @@ if [ "$cur_data" != "" ]; then
     data+=("$cur_data")
 fi
 
     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
 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
         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
 
     unset kvp
 done
 
index 6eb2b08e46e5e5530f42beb0576e0bec33b8be63..88fc560b0e0c54ed2b7f3fdfe7b3357f18ca4d74 100644 (file)
@@ -128,6 +128,17 @@ while read -u 3 line; do
     seperator=" "
 done
 
     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")
 # 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
         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
         fi
-    done
-    if ( check_data kvp ); then
-        valid_count=$((valid_count+1))
     fi
     unset kvp
 done
     fi
     unset kvp
 done