Day 3 part 2

main
kirbylife 2024-12-03 10:16:26 -06:00
parent e3435d75e2
commit e740aa4421
1 changed files with 20 additions and 1 deletions

View File

@ -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"