Initial commit (day 01)

Signed-off-by: Christopher Arndt <chris@chrisarndt.de>
This commit is contained in:
Christopher Arndt 2023-12-02 20:58:00 +01:00
commit c3f9012395
3 changed files with 1032 additions and 0 deletions

1000
day_01/input_01.txt Normal file

File diff suppressed because it is too large Load Diff

12
day_01/solution_01.nim Normal file
View File

@ -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

20
day_01/solution_01b.nim Normal file
View File

@ -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