2023: day 1!
This commit is contained in:
parent
b0086a2360
commit
4b49e6b86c
3 changed files with 42 additions and 0 deletions
1
2023/.gitignore
vendored
Normal file
1
2023/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
input
|
12
2023/day01/d01_1.nim
Normal file
12
2023/day01/d01_1.nim
Normal file
|
@ -0,0 +1,12 @@
|
|||
import std/[math, sequtils, strutils]
|
||||
|
||||
proc calibrationValue*(line: string): int =
|
||||
var digits: seq[char]
|
||||
for ch in line:
|
||||
if ch.isDigit():
|
||||
digits.add(ch)
|
||||
result = parseInt(digits[0] & digits[^1])
|
||||
|
||||
when isMainModule:
|
||||
var input = stdin.readAll().strip().splitLines()
|
||||
echo input.map(calibrationValue).sum()
|
29
2023/day01/d01_2.nim
Normal file
29
2023/day01/d01_2.nim
Normal file
|
@ -0,0 +1,29 @@
|
|||
import std/[math, sequtils, strutils]
|
||||
import d01_1
|
||||
|
||||
func calcDigitLookup(): seq[(string, string)] =
|
||||
let digits = {
|
||||
"one": "1",
|
||||
"two": "2",
|
||||
"three": "3",
|
||||
"four": "4",
|
||||
"five": "5",
|
||||
"six": "6",
|
||||
"seven": "7",
|
||||
"eight": "8",
|
||||
"nine": "9"
|
||||
}
|
||||
for (k1, v1) in digits:
|
||||
for (k2, v2) in digits:
|
||||
if k1[^1] == k2[0]:
|
||||
result.add((k1[0..^2] & k2, v1 & v2))
|
||||
result & @digits
|
||||
|
||||
const digits = calcDigitLookup()
|
||||
|
||||
when isMainModule:
|
||||
let
|
||||
input = stdin.readAll().strip()
|
||||
realInput = input.multiReplace(digits)
|
||||
lines = realInput.splitLines()
|
||||
echo lines.map(calibrationValue).sum()
|
Loading…
Add table
Reference in a new issue