1
0
www.mikescher.com/www/statics/aoc/2022/03_solution-2.nim

33 lines
781 B
Nim
Raw Normal View History

2022-12-03 13:43:46 +01:00
import std/strutils
import std/sequtils
proc run03_2(): string =
const input = staticRead"../input/day03.txt"
var res = 0
let lines = splitLines(input).filter(proc(p: string): bool = p != "")
for lineSet in lines.distribute(int(lines.len/3)):
let line1 = lineSet[0]
let line2 = lineSet[1]
let line3 = lineSet[2]
let badge = line1.toSeq().filter(proc(p: char): bool = line2.contains(p) and line3.contains(p))[0]
let prio = if int(badge) >= int('a'): int(badge) - int('a') + 1 else: int(badge) - int('A') + 27
# echo badge, " : ", prio
res += prio
return intToStr(res)
when not defined(js):
echo run03_2()
else:
proc js_run03_2(): cstring {.exportc.} =
return cstring(run03_2())