Skip to content

Instantly share code, notes, and snippets.

@fxn
Last active December 3, 2022 22:01
Show Gist options
  • Save fxn/f22848fc091a5caa9aacd2bd339cf668 to your computer and use it in GitHub Desktop.
Save fxn/f22848fc091a5caa9aacd2bd339cf668 to your computer and use it in GitHub Desktop.
import std/[os,sequtils,sets,sugar]
type
Rucksack = object
compA: HashSet[char]
compB: HashSet[char]
func toRucksack(rucksackStr: string): Rucksack =
let items = distribute(rucksackStr.toSeq, 2)
result.compA = items[0].toHashSet
result.compB = items[1].toHashSet
func commonItem(self: Rucksack): char =
var commonItems = self.compA*self.compB
commonItems.pop
func priority(c: char): int =
case c:
of 'a'..'z': int(c) - 96
of 'A'..'Z': int(c) - 38
else: raise
var commonItems = collect:
for rucksackStr in paramStr(1).lines:
rucksackStr.toRucksack.commonItem
echo foldl(commonItems, a + priority(b), 0)
import std/[os,sequtils,sets,sugar]
func priority(c: char): int =
case c:
of 'a'..'z': int(c) - 96
of 'A'..'Z': int(c) - 38
else: raise
func groupBadge(groupItems: seq[HashSet[char]]): char =
var commonItems = foldl(groupItems[1 .. ^1], a*b, groupItems[0])
commonItems.pop
let lines = paramStr(1).lines.toSeq
var badges = collect:
for i in countup(0, high(lines), 3):
let groupItems = lines[i .. (i + 2)].mapIt(it.toHashSet)
groupBadge(groupItems)
echo foldl(badges, a + priority(b), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment