Initial commit (day 01)
Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
commit
c3f9012395
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,12 @@
|
|||
import std/[re, strutils]
|
||||
|
||||
var res = 0
|
||||
|
||||
for line in readFile("input_01.txt").splitLines:
|
||||
if line.strip.len > 0:
|
||||
var digits = findAll(line, re"\d")
|
||||
var number = parseInt(digits[0] & digits[^1])
|
||||
res += number
|
||||
#echo $digits, $number
|
||||
|
||||
echo "Result: ", $res
|
|
@ -0,0 +1,20 @@
|
|||
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
|
Loading…
Reference in New Issue