55 lines
1.1 KiB
Nim
55 lines
1.1 KiB
Nim
import std/[strutils, sequtils, math]
|
|
import aoc
|
|
|
|
proc parseInstruction(i: string): (int, int) =
|
|
let s = i.split(',')
|
|
echo s
|
|
assert s.len == 2
|
|
result = (parseInt(s[0]), parseInt(s[1]))
|
|
|
|
|
|
|
|
proc parseInput(input: string): seq[(int, int)] =
|
|
var i: int
|
|
while i < input.len:
|
|
case input[i]
|
|
of 'm':
|
|
# ul(x,y)
|
|
# not enough space so bail
|
|
if i > (input.len - 6): return
|
|
else:
|
|
var instruction: string
|
|
if input[i .. i+3] == "mul(":
|
|
i += 4
|
|
while true:
|
|
let c = input[i]
|
|
case c:
|
|
of '0'..'9':
|
|
instruction.add c; inc i
|
|
of ')':
|
|
result.add parseInstruction instruction
|
|
break
|
|
of ',':
|
|
instruction.add c; inc i
|
|
else: break
|
|
inc i
|
|
else: inc i
|
|
|
|
|
|
# var xy: (int, int)
|
|
|
|
|
|
proc partOne*(input: string): int =
|
|
let instructions = parseInput(input)
|
|
instructions.mapIt(it[0] * it[1]).sum()
|
|
|
|
|
|
# proc partTwo*(input: string): int = 0
|
|
#
|
|
solve:
|
|
"example.txt":
|
|
partOne: 161
|
|
# partTwo: 0
|
|
"input.txt":
|
|
partOne: 0
|
|
# partTwo: 0
|