From 3395ef819e542b8cae16d853012d43de3b917a64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdulkadir=20Furkan=20=C5=9Eanl=C4=B1?= Date: Fri, 1 Dec 2023 17:49:17 +0100 Subject: [PATCH] Day 1 Part 2 --- 1/2/solution.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/1/2/solution.sh b/1/2/solution.sh index f78c280..efe0753 100755 --- a/1/2/solution.sh +++ b/1/2/solution.sh @@ -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}")