adventOfCode/challenges/2022/03-rucksackReorganization/nim/challenge.nim
AKP 0f6d020d3c
Add Nim solutions for day 2 and 3
Signed-off-by: AKP <tom@tdpain.net>
2022-12-03 13:01:43 +00:00

39 lines
939 B
Nim

import std/sets
import std/strutils
proc parse(instr: string): seq[string] = instr.strip.splitlines
proc getPriority(ch: char): int =
let co = ch.ord
if ord('a') <= co and co <= ord('z'):
return (co - ord('a')) + 1
return (co - ord('A')) + 27
proc partOne*(instr: string): int =
let inp = parse(instr)
var sigma = 0
for x in inp:
assert x.len mod 2 == 0
let l = x.len div 2
var y = intersection(
toHashSet(x[0..<l]),
toHashSet(x[l..<x.len]),
)
sigma += getPriority(y.pop())
return sigma
proc partTwo*(instr: string): int =
let inp = parse(instr)
assert inp.len mod 3 == 0
var sigma = 0
for i in countup(0, inp.len - 1, 3):
var x = toHashSet(inp[i])
x = intersection(x, toHashSet(inp[i + 1]))
x = intersection(x, toHashSet(inp[i + 2]))
sigma += getPriority(x.pop())
return sigma