]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blob - day4/count_valid_passports.sh
17c7ffc77aa6aabac36c2098efaf90e2b886ee2b
[advent-of-code-2020.git] / day4 / count_valid_passports.sh
1 #!/bin/bash
2
3 declare -a data
4 cur_data=""
5 seperator=""
6 valid_count=0
7
8 required_fields=(byr iyr eyr hgt hcl ecl pid)
9 exec 3<input.txt
10
11 while read -u 3 line; do
12     if [ "$line" = "" ]; then
13         data+=("$cur_data")
14         seperator=""
15         cur_data=""
16         continue
17     fi
18     cur_data="${cur_data}${seperator}$line"
19     seperator=" "
20 done
21
22 # on the last line, if cur_data isn't empty, add that to the array
23 if [ "$cur_data" != "" ]; then
24     data+=("$cur_data")
25 fi
26
27 for (( i=0; i<${#data[@]}; i++ )); do
28     record="${data[$i]}"
29     declare -A kvp
30     for field in $record; do
31         key=${field%:*}
32         value=${field#*:}
33         kvp[$key]=$value
34     done
35     for field in ${required_fields[@]}; do
36         if ! [ ${kvp[$field]+a} ]; then
37             continue 2
38         fi
39     done
40     valid_count=$((valid_count+1))
41     unset kvp
42 done
43
44 echo "$valid_count valid entries"