From: Jack Kinsey Date: Fri, 2 Dec 2022 18:30:55 +0000 (-0500) Subject: Day 1 X-Git-Url: http://git.jkinsey.net/?a=commitdiff_plain;h=652d3f8420ad48eb91bf227d7277b11bf2d05130;p=adventofcode2022.git Day 1 --- 652d3f8420ad48eb91bf227d7277b11bf2d05130 diff --git a/src/day01.fnl b/src/day01.fnl new file mode 100644 index 0000000..22dfe6f --- /dev/null +++ b/src/day01.fnl @@ -0,0 +1,62 @@ +(module net.journcy.aoc2022.day01) + +(defn inc [n] (+ n 1)) +(defn dec [n] (- n 1)) +(defn lines [file] (icollect [v (io.lines file)] v)) +(defn numbers [file] (icollect [v (io.lines file)] (tonumber v))) +(defn head [tbl] (. tbl 1)) +(defn last [tbl] (let [n (# tbl)] (. tbl n))) +(defn map [f tbl] (icollect [_ v (ipairs tbl)] (f v))) +(defn add [a b] (+ a b)) +(defn gt [a b] (> a b)) +(defn accnum [f tbl] + (accumulate [acc 0 + _ v (ipairs tbl)] + (f acc v))) +(defn take [n tbl] + (accumulate [t [] + i v (ipairs tbl) + &until (> i n)] + (doto t (table.insert v)))) +(defn partition [tbl brk] + (accumulate [parts [[]] + _ v (ipairs tbl)] + (do + (if (= v brk) + (table.insert parts []) + (table.insert (last parts) v)) + parts))) +(defn sum [tbl] (accnum add tbl)) +(defn sort [tbl f] (do (table.sort tbl f) tbl)) + +(comment + (partition [1 2 3 "" 4 5 6 "" 7 8 9] "") + (map sum [[1000 2000 3000] + [4000] + [5000 6000] + [7000 8000 9000] + [10000]]) + (take 3 [[1000 2000 3000] + [4000] + [5000 6000] + [7000 8000 9000] + [10000]]) + ) + + +(defn part1 [lines] + (->> (partition lines "") + (map (partial map tonumber)) + (map sum) + (accnum math.max))) + +(defn part2 [lines] + (->> (partition lines "") + (map (partial map tonumber)) + (map sum) + (#(sort $1 gt)) + (take 3) + sum)) + +(part1 (lines "input/day01.txt")) +(part2 (lines "input/day01.txt"))