Added AOC 2022 day 03
This commit is contained in:
32
www/statics/aoc/2022/03_solution-2.nim
Normal file
32
www/statics/aoc/2022/03_solution-2.nim
Normal file
@@ -0,0 +1,32 @@
|
||||
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())
|
||||
Reference in New Issue
Block a user