]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/blob - day6/get_yes_counts.sh
b91205cb91b09f013c1461390176cca4aabc8b45
[advent-of-code-2020.git] / day6 / get_yes_counts.sh
1 #!/bin/bash
2
3 set -u
4 set -e
5
6 declare -a group_questions
7
8 parse_file() {
9     exec 3<input.txt
10     declare -A questions
11     while read -u 3 line; do
12         #remove new lines
13         line=${line//$'\r'//}
14         line=${line//$'\n'//}
15         # go through each character of line
16         if [ "$line" == "" ]; then
17             question_string=""
18             for key in "${!questions[@]}"; do
19                 question_string+="${key}"
20             done
21             group_questions+=($question_string)
22             unset questions
23             declare -A questions
24             continue
25         fi
26         for (( a=0; a<${#line}; a++ )); do
27             question=${line:$a:1}
28             questions["$question"]=1
29         done
30     done
31
32     # last line was hit, so we need to process that too
33     question_string=""
34     for key in "${!questions[@]}"; do
35         question_string+="${key}"
36     done
37     group_questions+=($question_string)
38     unset questions
39 }
40
41 parse_file
42
43 sum_of_groups=0
44 for group in "${group_questions[@]}"; do
45     sum_of_groups=$((sum_of_groups+${#group}))
46 done
47
48 echo "Sum of groups: $sum_of_groups"