]> localhost Git - adventofcode2022.git/commitdiff
Day 3
authorJack Kinsey <j.jameskinsey@gmail.com>
Tue, 6 Dec 2022 04:09:36 +0000 (23:09 -0500)
committerJack Kinsey <j.jameskinsey@gmail.com>
Tue, 6 Dec 2022 04:09:36 +0000 (23:09 -0500)
t: 1:35:??.???

src/day03.fnl [new file with mode: 0644]

diff --git a/src/day03.fnl b/src/day03.fnl
new file mode 100644 (file)
index 0000000..12de2c3
--- /dev/null
@@ -0,0 +1,61 @@
+(module net.journcy.aoc2022.day03
+  {autoload {c net.journcy.aoc2022.common}})
+
+; a-z (97-122) -> 1-26
+; A-Z (65-90) -> 27-52
+(defn item->priority [item]
+  (let [ord (- (string.byte item) 64)]
+    (if (> ord 26)
+      (- ord 32)
+      (+ ord 26))))
+
+(defn errorcheck [rucksack]
+  (let [len (# rucksack)
+        half (/ len 2)
+        first (string.sub rucksack 1 half)
+        second (string.sub rucksack (- half))
+        same (c.head (c.intersect (c.split first "") (c.split second "")))]
+    (item->priority same)))
+
+(defn badgecheck [sacks]
+  (->> sacks
+       (c.map #(c.split $1 ""))
+       (c.reduce c.intersect)  
+       c.head
+       item->priority))
+
+(comment
+  (c.intersect ["v" "J" "r" "w" "p" "W" "t" "w" "J" "g" "W" "r"]
+               ["h" "c" "s" "F" "M" "M" "f" "F" "F" "h" "F" "p"])
+  (c.map errorcheck 
+         ["vJrwpWtwJgWrhcsFMMfFFhFp"
+          "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"
+          "PmmdzqPrVvPwwTWBwg"
+          "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn"
+          "ttgJtRGJQctTZtZT"
+          "CrZsJsPPZsGzwwsLwLmpwMDw"])
+  (badgecheck ["vJrwpWtwJgWrhcsFMMfFFhFp"
+               "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"
+               "PmmdzqPrVvPwwTWBwg"])
+  (c.map badgecheck
+         (c.partition 
+           ["vJrwpWtwJgWrhcsFMMfFFhFp"
+            "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL"
+            "PmmdzqPrVvPwwTWBwg"
+            "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn"
+            "ttgJtRGJQctTZtZT"
+            "CrZsJsPPZsGzwwsLwLmpwMDw"]
+           3)))
+
+(defn part1 [lines]
+  (->> lines
+       (c.map errorcheck)
+       c.sum))
+
+(defn part2 [lines]
+  (let [parts (c.partition lines 3)]
+    (c.sum (c.map badgecheck parts))))
+
+(comment
+  (part1 (c.lines "input/day03.txt"))
+  (part2 (c.lines "input/day03.txt")))