adventofcode-2023/day_01/solution_01b.nim

21 lines
578 B
Nim
Raw Normal View History

import std/[re, strutils]
var res = 0
var valid_digits = "one,two,three,four,five,six,seven,eight,nine".split(",")
var digits_rx = re("\\d|" & valid_digits.join("|"))
proc parseDigit(s: string): int =
try:
result = parseInt(s)
except ValueError:
result = valid_digits.find(s) + 1
for line in readFile("input_01.txt").splitLines:
if line.strip.len > 0:
var digits = findAll(line, digits_rx)
var number = parseDigit(digits[0]) * 10 + parseDigit(digits[^1])
res += number
#echo $digits, $number
echo "Result: ", $res