day 3 part 1

This commit is contained in:
Daylin Morgan 2024-12-03 21:23:50 -06:00
parent b41dfec4f2
commit eec741a56e
Signed by: daylin
GPG key ID: 950D13E9719334AD
2 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))

View file

@ -0,0 +1,55 @@
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