From: Jack Kinsey Date: Tue, 6 Dec 2022 04:09:36 +0000 (-0500) Subject: Day 3 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=a5d7e2fe3b2e756290c188f40917f9f1632cdd6f;p=adventofcode2022.git Day 3 t: 1:35:??.??? --- diff --git a/src/day03.fnl b/src/day03.fnl new file mode 100644 index 0000000..12de2c3 --- /dev/null +++ b/src/day03.fnl @@ -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")))