about summary refs log tree commit diff
path: root/aoc/2022/03/part-one.nim
diff options
context:
space:
mode:
authorNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-03 17:00:43 +0900
committerNguyễn Gia Phong <mcsinyx@disroot.org>2022-12-03 19:01:45 +0900
commiteeb9a45346a81ca417cf8dd9f88d3ca1ed88935a (patch)
tree23306c83f348831b33a7807773c14f8527ea17d5 /aoc/2022/03/part-one.nim
parentada3a69b15ffd1f2c76044726cbf05a415c9d8d4 (diff)
downloadcp-eeb9a45346a8.tar.gz
[aoc/2022] Finish day 3
Diffstat (limited to 'aoc/2022/03/part-one.nim')
-rw-r--r--aoc/2022/03/part-one.nim19
1 files changed, 19 insertions, 0 deletions
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