26 lines
687 B
Bash
26 lines
687 B
Bash
#!/bin/bash
|
|
|
|
# Find valid calls of mul
|
|
valid_mull_calls=$(cat input.txt | grep 'mul([[:digit:]]\+,[[:digit:]]\+)' -o)
|
|
|
|
# Remove everything except the numbers with delimiting commas.
|
|
mul_args_only=$(echo "$valid_mull_calls" | sed 's/mul(//g' | sed 's/)//g')
|
|
|
|
mul () {
|
|
# Takes a string of number1,number2 and multiplies them.
|
|
local multiplicand=$(echo $1 | cut -d , -f 1)
|
|
local multiplier=$(echo $1 | cut -d , -f 2)
|
|
echo $((multiplicand * multiplier))
|
|
|
|
}
|
|
|
|
total=0
|
|
# Loop over multiplicand,multiplier pairs and aggregate the sum of their products.
|
|
for args in $( echo $mul_args_only | xargs ); do
|
|
product=$(mul $args)
|
|
total=$((total + product))
|
|
done
|
|
|
|
echo $total
|
|
|