From 5020fc18ce4a56af3702771d201c4566e3fd2261 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Sun, 1 Dec 2024 12:39:48 -0600 Subject: [PATCH] solution 1 --- solutions/day01/example.txt | 6 ++++++ solutions/day01/solution.nim | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 solutions/day01/example.txt create mode 100644 solutions/day01/solution.nim diff --git a/solutions/day01/example.txt b/solutions/day01/example.txt new file mode 100644 index 0000000..b8af9ad --- /dev/null +++ b/solutions/day01/example.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/solutions/day01/solution.nim b/solutions/day01/solution.nim new file mode 100644 index 0000000..78795f5 --- /dev/null +++ b/solutions/day01/solution.nim @@ -0,0 +1,32 @@ +import std/[strutils, algorithm, sequtils, tables] +import aoc + +proc parseInput(input: string): (seq[int], seq[int]) = + for line in input.splitLines(): + let s = line.splitWhitespace() + assert s.len == 2 + result[0].add parseInt(s[0]) + result[1].add parseInt(s[1]) + +proc partOne*(input: string): int = + var ids = parseInput(input) + ids[0].sort + ids[1].sort + for (l, r) in zip(ids[0],ids[1]): + result += abs(l-r) + + +proc partTwo*(input: string): int = + var ids = parseInput(input) + let counts = toCountTable ids[1] + for id in ids[0]: + if id in counts: + result += id * counts[id] + +solve: + "example.txt": + partOne: 11 + partTwo: 31 + "input.txt": + partOne: 1590491 + partTwo: 22588371