This commit is contained in:
2024-12-03 15:31:10 +08:00
parent b2f9cf2589
commit d9616826c1
3 changed files with 38 additions and 0 deletions

25
2024/day-3/solution-2.sh Normal file
View File

@ -0,0 +1,25 @@
#!/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
grep -Po 'do\(\)|don(.)?t\(\)|mul\([0-9]+,[0-9]+\)' input |
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
}'