aoc2024/day03.sh
Patrick Michl 85ea5d299b
shfmt
2024-12-05 18:25:04 +01:00

42 lines
854 B
Bash
Executable File

#!/usr/bin/env bash
unset PATH
readonly PATH
source ./utils/main.sh
p1() {
local input="$1" result=0
while [[ -n "$input" ]]; do
if [[ "$input" =~ mul\(([0-9]{1,3}),([0-9]{1,3})\) ]]; then
result="$((result + (BASH_REMATCH[1] * BASH_REMATCH[2])))"
input="${input#*"${BASH_REMATCH[0]}"}"
else
input=''
fi
done
printf '%d' "$result"
}
p2() {
local input="$1" result=0 do=true
while [[ -n "$input" ]]; do
if [[ "$input" =~ mul\(([0-9]{1,3}),([0-9]{1,3})\)|do\(\)|don\'t\(\) ]]; then
case "${BASH_REMATCH[0]}" in
do\(\)) do=true ;;
don\'t\(\)) do=false ;;
*) [[ "$do" == true ]] && result="$((result + (BASH_REMATCH[1] * BASH_REMATCH[2])))" ;;
esac
input="${input#*"${BASH_REMATCH[0]}"}"
else
input=''
fi
done
printf '%d' "$result"
}
main "$@"