solve day 1

This commit is contained in:
Daylin Morgan 2023-12-01 06:36:49 -06:00
parent 19f37ece40
commit 9d8b5e2d2e
Signed by: daylin
GPG key ID: C1E52E7DD81DF79F
3 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

View file

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

View file

@ -0,0 +1,56 @@
import std/[strutils]
const example* = slurp("example.txt").strip()
const example2* = slurp("example2.txt").strip()
const input* = slurp("input.txt").strip()
proc parseInput(input: string): seq[string] =
input.split("\n")
proc partOne*(input: string): int =
let lines = parseInput(input)
for line in lines:
var digits: seq[char]
for c in line:
case c
of '0'..'9':
digits.add c
else: discard
result += parseInt(digits[0] & digits[^1])
type
Number = enum
zero = 0,
one, two, three, four, five, six, seven, eight, nine
proc partTwo*(input: string): int =
let lines = parseInput(input)
for line in lines:
var digits: seq[char]
var i: int
while i < line.len:
let c = line[i]
case c
of '0'..'9':
digits.add c
inc i
else:
for number in Number.low..Number.high:
if line[i..^1].startsWith($number):
digits.add $number.ord
# some numbers share chars
# let's jump back an extra index to catch them
i += len($number)-2
break
inc i
result += parseInt(digits[0] & digits[^1])
when isMainModule:
import std/unittest
suite "day 1":
test "part one":
check partOne(example) == 142
check partOne(input) == 54597
test "part two":
check partTwo(example2) == 281
check partTwo(input) == 54504