From eeb9a45346a81ca417cf8dd9f88d3ca1ed88935a Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sat, 3 Dec 2022 17:00:43 +0900 Subject: [aoc/2022] Finish day 3 --- aoc/2022/03/part-one.nim | 19 +++++++++++++++++++ aoc/2022/03/part-two.nim | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 aoc/2022/03/part-one.nim create mode 100644 aoc/2022/03/part-two.nim diff --git a/aoc/2022/03/part-one.nim b/aoc/2022/03/part-one.nim new file mode 100644 index 0000000..0f16754 --- /dev/null +++ b/aoc/2022/03/part-one.nim @@ -0,0 +1,19 @@ +import math +import sequtils +import sets +import strutils + +func priority(item: int): int = + result = item - 96 + if result < 0: + result += 58 + +func examine(sack: string): int = + let + size = sack.len div 2 + other = to_hash_set sack[0 ..< size] + for c in sack[size .. ^1]: + if c in other: + return priority ord c + +echo sum stdin.read_all.strip.split_lines.map examine diff --git a/aoc/2022/03/part-two.nim b/aoc/2022/03/part-two.nim new file mode 100644 index 0000000..ef27011 --- /dev/null +++ b/aoc/2022/03/part-two.nim @@ -0,0 +1,16 @@ +import math +import sequtils +import sets +import strutils + +func priority(group: seq[string]): int = + let sets = group.map_it it.to_hash_set + var common = sets.foldl a * b + result = common.pop.ord - 96 + if result < 0: + result += 58 + +let + elves = split_lines strip read_all stdin + groups = elves.distribute elves.len div 3 +echo sum groups.map priority -- cgit 1.4.1