advent-of-code-2024/day-3/script.sh

33 lines
763 B
Bash

#!/bin/bash
regex="mul\(([0-9]{1,3}),([0-9]{0,3}))"
acc=0
while read line; do
if [[ $line =~ $regex ]] then
n1=${BASH_REMATCH[1]}
n2=${BASH_REMATCH[2]}
acc=$(expr $n1 \* $n2 + $acc)
fi
done <<< $(grep -Eo $regex input.txt)
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"