mirror of
https://github.com/daylinmorgan/advent-of-code-2023.git
synced 2024-12-21 19:10:43 -06:00
solve day 1
This commit is contained in:
parent
19f37ece40
commit
9d8b5e2d2e
3 changed files with 67 additions and 0 deletions
4
solutions/day01/example.txt
Normal file
4
solutions/day01/example.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
solutions/day01/example2.txt
Normal file
7
solutions/day01/example2.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
56
solutions/day01/solution.nim
Normal file
56
solutions/day01/solution.nim
Normal 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
|
Loading…
Reference in a new issue