52 lines
1.1 KiB
Nim
52 lines
1.1 KiB
Nim
import lib/loader
|
|
|
|
|
|
proc toSet(s: string): set[char] =
|
|
for c in s:
|
|
result = result + {c}
|
|
|
|
|
|
proc answersIncl(data: seq[string]): seq[set[char]] =
|
|
var s: set[char]
|
|
for line in data:
|
|
if line.len > 0:
|
|
s = s + line.toSet()
|
|
else:
|
|
result.add(s)
|
|
s = {}
|
|
if s.len > 0: # in case of trailing newline
|
|
result.add(s)
|
|
|
|
|
|
proc answersExcl(data: seq[string]): seq[set[char]] =
|
|
var s: set[char]
|
|
var first = true
|
|
for line in data:
|
|
if first:
|
|
s = line.toSet()
|
|
first = false
|
|
elif line.len > 0:
|
|
s = s * line.toSet()
|
|
else:
|
|
result.add(s)
|
|
s = {}
|
|
first = true
|
|
if s.len > 0:
|
|
result.add(s)
|
|
|
|
|
|
proc partOne(data: seq[string]): int =
|
|
for group in answersIncl(data):
|
|
result += group.len
|
|
|
|
|
|
proc partTwo(data: seq[string]): int =
|
|
for group in answersExcl(data):
|
|
result += group.len
|
|
|
|
|
|
when isMainModule:
|
|
let data = loadStrings(6)
|
|
echo "One: ", partOne(data)
|
|
echo "Two: ", partTwo(data)
|