From 7be570c4a6e86fb7060f0bc06910ca57003dfe90 Mon Sep 17 00:00:00 2001 From: Orfeas <38209077+0xfea5@users.noreply.github.com> Date: Sat, 8 Jun 2024 13:50:47 +0300 Subject: Update file names and init.sh --- day03/solution.nim | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 day03/solution.nim (limited to 'day03/solution.nim') diff --git a/day03/solution.nim b/day03/solution.nim new file mode 100644 index 0000000..6df88f2 --- /dev/null +++ b/day03/solution.nim @@ -0,0 +1,47 @@ +import std/strutils +import std/sequtils +import std/sets + +proc Points(c: char): int = + if c.isLowerAscii(): + return ord(c) - ord('a') + 1 + else: + return ord(c) - ord('A') + 27 + +proc part1(content: seq[string]): int = + let ruckshacks = map( content, + proc(line: string): tuple[first: string, second: string] = + let pivot = int(line.len()/2) + (line[0 .. pivot-1], line[pivot .. ^1]) + ) + + var score = 0 + for ruckshack in ruckshacks: + let + uniqFirst = toHashSet(ruckshack.first) + uniqSecond = toHashSet(ruckshack.second) + + var c = toSeq(uniqFirst * uniqSecond)[0] + score += Points(c) + + return score + +proc part2(content: seq[string]): int = + assert(content.len() mod 3 == 0) + + let ruckshacks = content + var score = 0 + for i in countup(0, ruckshacks.len()-1, 3): + let + uniqFirst = toHashSet(ruckshacks[i]) + uniqSecond = toHashSet(ruckshacks[i+1]) + uniqThird = toHashSet(ruckshacks[i+2]) + + var c = toSeq(uniqFirst * uniqSecond * uniqThird)[0] + score += Points(c) + + return score + +let content = readFile("./input.txt").strip().split("\n") +echo part1(content) +echo part2(content) -- cgit v1.2.3