Day 3
This commit is contained in:
25
2024/day-3/solution-2.sh
Normal file
25
2024/day-3/solution-2.sh
Normal 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
|
||||
}'
|
Reference in New Issue
Block a user