33 lines
964 B
Bash
Executable File
33 lines
964 B
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
if [[ "${TRACE-0}" == "1" ]]; then
|
|
set -o xtrace
|
|
fi
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
main ()
|
|
{
|
|
digit_regex="[[:digit:]]|one|two|three|four|five|six|seven|eight|nine"
|
|
digit_regex_reverse="[[:digit:]]|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin"
|
|
declare -A digits=([one]=1 [two]=2 [three]=3 [four]=4 [five]=5 [six]=6 [seven]=7 [eight]=8 [nine]=9)
|
|
declare -a components
|
|
declare -i result=0
|
|
while read -r line; do
|
|
first=$(grep -E -o "${digit_regex}" <<< "${line}" | head -n 1)
|
|
last=$(rev <<< "${line}" | grep -E -o "${digit_regex_reverse}" | rev | head -n 1)
|
|
first=$([[ ! ${first} =~ ^[0-9]$ ]] && echo "${digits[${first}]}" || echo "${first}")
|
|
last=$([[ ! ${last} =~ ^[0-9]$ ]] && echo "${digits[${last}]}" || echo "${last}")
|
|
components+=("${first}${last}")
|
|
done < input.txt
|
|
for value in "${components[@]}"; do
|
|
(( result += value ))
|
|
done
|
|
echo "${result}"
|
|
}
|
|
|
|
main
|