blob: 6df88f29b9c5482bd183411b2f3446d640d72c7b (
plain) (
blame)
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
40
41
42
43
44
45
46
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)
|