From e740aa4421d648c590c3ebb5808fc70aa4430e89 Mon Sep 17 00:00:00 2001 From: kirbylife Date: Tue, 3 Dec 2024 10:16:26 -0600 Subject: [PATCH] Day 3 part 2 --- day-3/script.sh | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/day-3/script.sh b/day-3/script.sh index 899a8b9..310cbe3 100644 --- a/day-3/script.sh +++ b/day-3/script.sh @@ -10,4 +10,23 @@ while read line; do acc=$(expr $n1 \* $n2 + $acc) fi done <<< $(grep -Eo $regex input.txt) -echo $acc +echo "Part 1: $acc" + +# Part 2 +regex="(mul\(([0-9]{1,3}),([0-9]{0,3})\)|do\(\)|don't\(\))" +acc=0 +enabled=1 +while read line; do + if [[ $line =~ $regex ]] then + if [[ $line == "do()" ]] then + enabled=1 + elif [[ $line == "don't()" ]] then + enabled=0 + else + n1=${BASH_REMATCH[2]} + n2=${BASH_REMATCH[3]} + [ $enabled -eq 1 ] && acc=$(expr $n1 \* $n2 + $acc) + fi + fi +done <<< $(grep -Eo $regex input.txt) +echo "Part 2: $acc"