From eec741a56ec8939d749e313976eb8bc8826fadf5 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Tue, 3 Dec 2024 21:23:50 -0600 Subject: [PATCH] day 3 part 1 --- solutions/day03/example.txt | 1 + solutions/day03/solution.nim | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 solutions/day03/example.txt create mode 100644 solutions/day03/solution.nim diff --git a/solutions/day03/example.txt b/solutions/day03/example.txt new file mode 100644 index 0000000..f274bda --- /dev/null +++ b/solutions/day03/example.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) diff --git a/solutions/day03/solution.nim b/solutions/day03/solution.nim new file mode 100644 index 0000000..d491a22 --- /dev/null +++ b/solutions/day03/solution.nim @@ -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