Files
advent-of-code/2024/day-3/solution-2.sh

26 lines
539 B
Bash
Raw Permalink Normal View History

2024-12-03 15:31:10 +08:00
#!/usr/bin/env bash
# Filter out do(), don't(), and mul()
# then strip out tails to get do and don
# strip out mul() to get numbers
2024-12-04 22:15:25 +08:00
grep -Po 'do\(\)|don.t\(\)|mul\([0-9]+,[0-9]+\)' input |
2024-12-03 15:31:10 +08:00
grep -Po 'don|do|mul\([0-9]+,[0-9]+\)' |
sed -E 's/^mul\(([0-9]+),([0-9]+)\)/\1 \2/g' |
awk '
BEGIN {
total = 0
todo = 1
}
{
if ($1 == "don") {
todo = 0
} else if ($1 == "do") {
todo = 1
} else if (todo == 1){
total += $1 * $2
}
}
END {
print total
}'