This commit is contained in:
Patrick Michl 2024-12-03 06:52:57 +01:00
parent 3794911eef
commit 937b633d8a
No known key found for this signature in database
GPG Key ID: BFE0ACEE21CD5EB0

40
day03.sh Executable file
View File

@ -0,0 +1,40 @@
#!/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 "$@"