Day 1 Part 2

This commit is contained in:
Abdulkadir Furkan Şanlı 2023-12-01 17:49:17 +01:00
parent 4be283b81e
commit 3395ef819e
No known key found for this signature in database

View File

@ -11,14 +11,14 @@ cd "$(dirname "$0")"
main ()
{
numbers="[[:digit:]]|one|two|three|four|five|six|seven|eight|nine"
numbers_reverse="[[:digit:]]|eno|owt|eerht|ruof|evif|xis|neves|thgie|enin"
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 "${numbers}" <<< "${line}" | head -n 1)
last=$(rev <<< "${line}" | grep -E -o "${numbers_reverse}" | rev | head -n 1)
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}")