summaryrefslogtreecommitdiff
path: root/02.sh
diff options
context:
space:
mode:
authorChristian Segundo2023-12-02 13:27:14 +0100
committerChristian Segundo2023-12-02 13:27:14 +0100
commit73353e0e5e01f0702c0a8aa99397474c40af2a4c (patch)
tree15a3415650e1ec6d6d51afa040c4965ec2fd9926 /02.sh
downloadadvent-of-dotslash-2023-73353e0e5e01f0702c0a8aa99397474c40af2a4c.tar.gz
Add day 1 & 2
Diffstat (limited to '02.sh')
-rwxr-xr-x02.sh71
1 files changed, 71 insertions, 0 deletions
diff --git a/02.sh b/02.sh
new file mode 100755
index 0000000..9c011d5
--- /dev/null
+++ b/02.sh
@@ -0,0 +1,71 @@
+#!/usr/bin/env bash
+set -eufCo pipefail
+
+function play_one() (
+ local game="$1"
+ local id=0
+ id=$(echo "$game" | cut -d' ' -f2 | cut -d':' -f1)
+ while read -r set; do
+ local red=12
+ local green=13
+ local blue=14
+ while read -r item; do
+ local n=0
+ n=$(echo "$item" | cut -d' ' -f1)
+ if [[ "$item" =~ "red" ]]; then
+ red=$((red - n))
+ elif [[ "$item" =~ "green" ]]; then
+ green=$((green - n))
+ elif [[ "$item" =~ "blue" ]]; then
+ blue=$((blue - n))
+ fi
+ done < <(echo "$set" | tr ',' '\n')
+ if [[ "$red" -lt 0 || "$green" -lt 0 || "$blue" -lt 0 ]]; then
+ echo 0; return 0;
+ fi
+ done < <(echo "$game" | cut -d':' -f2 | cut -d' ' -f2- | tr ';' '\n')
+ echo "$id"
+)
+
+function play_two() (
+ local game="$1"
+ local id=0
+ id=$(echo "$game" | cut -d' ' -f2 | cut -d':' -f1)
+ local colors=()
+ while read -r set; do
+ local red=0
+ local green=0
+ local blue=0
+ while read -r item; do
+ local n=0
+ n=$(echo "$item" | cut -d' ' -f1)
+ if [[ "$item" =~ "red" ]]; then
+ red=$((red + n))
+ elif [[ "$item" =~ "green" ]]; then
+ green=$((green + n))
+ elif [[ "$item" =~ "blue" ]]; then
+ blue=$((blue + n))
+ fi
+ done < <(echo "$set" | tr ',' '\n')
+ colors+=("$red $green $blue")
+ done < <(echo "$game" | cut -d':' -f2 | cut -d' ' -f2- | tr ';' '\n')
+ local red=0
+ local green=0
+ local blue=0
+ for i in "${colors[@]}"; do
+ rbg=($i)
+ [[ "${rbg[0]}" -gt "$red" ]] && red="${rbg[0]}"
+ [[ "${rbg[1]}" -gt "$green" ]] && green="${rbg[1]}"
+ [[ "${rbg[2]}" -gt "$blue" ]] && blue="${rbg[2]}"
+ done
+ echo "$((red * green * blue))"
+)
+
+sum_one=0
+sum_two=0
+while read -r game; do
+ sum_one=$((sum_one + $(play_one "$game")))
+ sum_two=$((sum_two + $(play_two "$game")))
+done < <(cat 02.txt)
+echo "$sum_one"
+echo "$sum_two"