Move to have day be 2 digit always
[advent-of-code-2020.git] / day06 / get_yes_counts.sh
diff --git a/day06/get_yes_counts.sh b/day06/get_yes_counts.sh
new file mode 100644 (file)
index 0000000..b91205c
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+set -u
+set -e
+
+declare -a group_questions
+
+parse_file() {
+    exec 3<input.txt
+    declare -A questions
+    while read -u 3 line; do
+        #remove new lines
+        line=${line//$'\r'//}
+        line=${line//$'\n'//}
+        # go through each character of line
+        if [ "$line" == "" ]; then
+            question_string=""
+            for key in "${!questions[@]}"; do
+                question_string+="${key}"
+            done
+            group_questions+=($question_string)
+            unset questions
+            declare -A questions
+            continue
+        fi
+        for (( a=0; a<${#line}; a++ )); do
+            question=${line:$a:1}
+            questions["$question"]=1
+        done
+    done
+
+    # last line was hit, so we need to process that too
+    question_string=""
+    for key in "${!questions[@]}"; do
+        question_string+="${key}"
+    done
+    group_questions+=($question_string)
+    unset questions
+}
+
+parse_file
+
+sum_of_groups=0
+for group in "${group_questions[@]}"; do
+    sum_of_groups=$((sum_of_groups+${#group}))
+done
+
+echo "Sum of groups: $sum_of_groups"