diff options
Diffstat (limited to '2023')
-rw-r--r-- | 2023/day01.awk | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/2023/day01.awk b/2023/day01.awk index 3127556..519b414 100644 --- a/2023/day01.awk +++ b/2023/day01.awk @@ -1,7 +1,39 @@ { - gsub("[^[:digit:]]", ""); + first = 0; + for (i = 0; i <= length; i++) { + digit = 0; + s = substr($0, i); + if (s ~ /^one/) { + digit = 1; + } else if (s ~ /^two/) { + digit = 2; + } else if (s ~ /^three/) { + digit = 3; + } else if (s ~ /^four/) { + digit = 4; + } else if (s ~ /^five/) { + digit = 5; + } else if (s ~ /^six/) { + digit = 6; + } else if (s ~ /^seven/) { + digit = 7; + } else if (s ~ /^eight/) { + digit = 8; + } else if (s ~ /^nine/) { + digit = 9; + } else if (s ~ /^[[:digit:]]/) { + digit = substr(s, 1, 1); + } + if (digit) { + if (!first) first = digit; + last = digit; + } + } + gsub(/[^[:digit:]]/, ""); part1 += substr($0, 1, 1) substr($0, length, 1); + part2 += first last; } END { print part1; + print part2; } |