day03
This commit is contained in:
parent
3794911eef
commit
937b633d8a
40
day03.sh
Executable file
40
day03.sh
Executable 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 "$@"
|
Loading…
x
Reference in New Issue
Block a user