Day 3 part 2
parent
e3435d75e2
commit
e740aa4421
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue