-
Notifications
You must be signed in to change notification settings - Fork 2
/
challenge.nim
39 lines (30 loc) · 939 Bytes
/
challenge.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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