]> git.sommitrealweird.co.uk Git - advent-of-code-2020.git/commitdiff
Add bash versions of day 1 for shits and giggles
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Fri, 4 Dec 2020 13:37:16 +0000 (13:37 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Fri, 4 Dec 2020 13:37:16 +0000 (13:37 +0000)
day1/find_2020_factors.sh [new file with mode: 0644]
day1/find_2020_factors_2.sh [new file with mode: 0644]

diff --git a/day1/find_2020_factors.sh b/day1/find_2020_factors.sh
new file mode 100644 (file)
index 0000000..fa3a5d5
--- /dev/null
@@ -0,0 +1,17 @@
+#!/bin/bash
+
+# read the file straight in for gits and shiggles
+declare -a data
+read -a data -d $'\0' < input.txt
+
+echo "Got ${#data[*]} records"
+
+for a in $(seq 0 $((${#data[*]}-2))); do
+    for b in $(seq $((a+1)) $((${#data[*]}-1))); do
+        if [ $((${data[$a]} + ${data[$b]})) -eq 2020 ]; then
+            echo "Match found: ${data[$a]} + ${data[$b]} = 2020"
+            echo "Multiplied: $((${data[$a]} * ${data[$b]}))"
+            exit 0
+        fi
+    done
+done
diff --git a/day1/find_2020_factors_2.sh b/day1/find_2020_factors_2.sh
new file mode 100644 (file)
index 0000000..04af85d
--- /dev/null
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+# read the file straight in for gits and shiggles
+declare -a data
+read -a data -d $'\0' < input.txt
+
+echo "Got ${#data[*]} records"
+
+for a in $(seq 0 $((${#data[*]}-3))); do
+    progress=$(($((a * 100)) / ${#data[*]}))
+    echo -ne "\r${progress}% through"
+    for b in $(seq $((a+1)) $((${#data[*]}-2))); do
+        for c in $(seq $((a+2)) $((${#data[*]}-1))); do
+            if [ $((${data[$a]} + ${data[$b]} + ${data[$c]})) -eq 2020 ]; then
+                echo -e "\r100% done          "
+                echo "Match found: ${data[$a]} + ${data[$b]} + ${data[$c]} = 2020"
+                echo "Multiplied: $((${data[$a]} * ${data[$b]} * ${data[$c]}))"
+                exit 0
+            fi
+        done
+    done
+done