diff --git a/README.md b/README.md index 10e6a89..5026326 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Day 00: Helper file | [aoc.clj](clojure/aoc.clj) | [Day 08](http://adventofcode.com/2023/day/8) | [day08.clj](clojure/day08.clj) | Underwhelming to see _that_ to be solution for part 2. [Day 09](http://adventofcode.com/2023/day/9) | [day09.clj](clojure/day09.clj) | .esreveR [Day 10](http://adventofcode.com/2023/day/10) | [day10.clj](clojure/day10.clj) | No BFS needed. - +[Day 11](http://adventofcode.com/2023/day/11) | [day11.clj](clojure/day11.clj) | Off-by-one trap! diff --git a/clojure/aoc.clj b/clojure/aoc.clj index 1128b3e..8f3019f 100644 --- a/clojure/aoc.clj +++ b/clojure/aoc.clj @@ -114,7 +114,7 @@ false (recur (dec idx) acc))))) -(defn count-if [pred xs] +(defn count-if ^long [pred xs] (reduce (fn [^long acc x] (if (pred x) (inc acc) acc)) diff --git a/clojure/day11.clj b/clojure/day11.clj new file mode 100644 index 0000000..829ff56 --- /dev/null +++ b/clojure/day11.clj @@ -0,0 +1,43 @@ +(ns day11 + (:require aoc)) + + +(def ^:const multi (dec 1000000)) + + +(defn empty-lines [lines] + (keep + (fn [[i line]] + (when (every? #(= % \.) line) i)) + (map-indexed vector lines))) + +(defn expand [universe] + (let [empty-rows (empty-lines universe) + empty-cols (empty-lines (aoc/transpose universe))] + (for [[^long y line] (map-indexed vector universe) + [^long x chr] (map-indexed vector line) + :when (= chr \#) + :let [move-right (aoc/count-if #(< ^long % x) empty-cols) + move-down (aoc/count-if #(< ^long % y) empty-rows)]] + [[(+ x move-right) + (+ y move-down)] + [(+ x (* multi move-right)) + (+ y (* multi move-down))]]))) + +(defn distances [coords] + (for [[^long x1 ^long y1] coords + [^long x2 ^long y2] coords + :while (not (and (= x1 x2) (= y1 y2)))] + (+ (- x1 x2) (abs (- y1 y2))))) + +(defn solve [input-file] + (let [universe (->> (aoc/read-input input-file) + expand + sort) + p1-galaxies (map first universe) + p2-galaxies (map second universe)] + [(reduce + (distances p1-galaxies)) + (reduce + (distances p2-galaxies))])) + + +(solve 11) diff --git a/clojure/tests/solutions_tests.clj b/clojure/tests/solutions_tests.clj index 624efa5..58635de 100644 --- a/clojure/tests/solutions_tests.clj +++ b/clojure/tests/solutions_tests.clj @@ -2,7 +2,7 @@ (:require day01 day02 day03 day04 day05 day06 day07 day08 day09 day10 - ; day11 day12 day13 day14 day15 + day11 ; day12 day13 day14 day15 ; day16 day17 day18 day19 day20 ; day21 day22 day23 day24 day25 [clojure.test :refer [deftest is run-tests successful?]])) @@ -34,7 +34,7 @@ (check-day 8 nil [11567 9858474970153]) (check-day 9 [114 2] [1581679977 889]) (check-day 10 [70 8] [6864 349]) - +(check-day 11 [374 82000210] [9724940 569052586852]) (let [summary (run-tests)] (when-not (successful? summary) diff --git a/inputs/11.txt b/inputs/11.txt new file mode 100644 index 0000000..232a501 --- /dev/null +++ b/inputs/11.txt @@ -0,0 +1,140 @@ +.............................................................................................#.............................................. +...#....................#............................#......................#........................#...........#................#......... +................#.....................................................#..............#.....................#................................ +................................................................#.......................................................#................... +.......#........................#.........................#..............................#.....#............................................ +............#............................#.................................................................................................. +......................#........................#......................................................#..................................... +....................................#.....................................#.................#..........................................#.... +................................................................................................................#..............#............ +...................#.................................................#............................#......................................... +............................................#.........#...............................#.................#................................... +.....#.....................#..................................................#............................................................. +........................................................................#.................#................................................. +...............................................................#.....................................................#............#......... +..............#.......................#...........................................#......................................................... +.......................#.......................#...........................................................#................................ +.........#........................................................#......................................................................... +.........................................................................#................................................................#. +............................#..........................#........................#.....#...............................#..................... +.....................................#..................................................................#....................#.............. +#........................................................................................................................................... +..........#......................#...........#............#............#.................................................................... +....................#.........................................................#.................................#........................... +.................................................#.............#.....................#..........#......................#..........#......... +....................................#....................................................................................................... +..........................................#..............................................................................................#.. +......................#.....................................................................................#.....#.........#............... +...........#................#.............................#.............#.....................#............................................. +#.................#...............................................#...........#............................................................. +.......................................#.................................................#.................................................. +.....#.......................................#.......................................................#..............................#......# +..............#.................#.................#..................#..............#..............................#........................ +...........................................................................#................................................................ +.........................#............................#....................................................#................#............... +.#..............................................................................#........................................................... +.......................................#................................#......................#............................................ +................#...........#...................#.......................................................................................#... +..............................................................#..........................................................#.................. +.......#......................................................................#.........#.............#..................................... +.......................................................#.................................................................................... +......................#................................................#....................................#.............................#. +#.........................................#......................................................................#.......................... +..................................#................#............................................#.......#................................... +.....#.....................#.............................#.............................#.................................................... +................#...............................................#........................................................................... +...............................#................................................#....................#..........................#........... +......................................................#....................#................#................#.............................. +..#..........................................#......................................................................#....................... +.............#.............................................................................................................................. +.......................................................................................................................................#.... +.......................#..............#...........................#............................#...............#............................ +.........#..................#...............................................#...........#................................................... +......................................................................#........................................................#............ +..............................................................#....................#...............#......#........#........................ +..............#...................#.............#.....................................................................................#..... +............................................................................................................................................ +.....................#.....#............#...........#......................................#................................................ +..........#...................................................................................................................#...........#. +...#......................................................................#.....................#................#.......................... +.............................................#...........................................................................#.................. +......................................................#...........#................#................#.................................#..... +.................................#.....................................#..................#...............#......................#.......... +............................................................................................................................................ +.......#..............................................................................................................#..................... +.....................#..............#......#.................................................#.............................................. +..............................#..................................#..............#............................#.............................. +..........#...............................................#............................#..........................................#......... +#.................................................#...................#...........................#......................................... +.......................................#.................................................................................................... +.......#...........................................................................#........................................................ +................#............................................#............................................#.........#....................#.. +.......................#........#.........#...............................................................................#................. +............................................................................................................................................ +..#................#........................................................................................................................ +..........#...........................................#......................................#.............................................. +............................................................................................................................................ +.....#.....................................#...................#....................................................................#....... +.........................#...........#............#.................................................................#....................#.. +........................................................#...............#................................................................... +............#.................#.................................................#......#..............#..................................... +.............................................................................................#................#................#............ +........................................#.......................#..........................................................................# +..#.................#..........................#......................#.............#..............#...................#.............#...... +............................#............................#................................#......................#.......................... +.............#.................................................................................#............................................ +.................................#.........................................#................................................................ +.........................#.........................#.................................................#........................#............. +.......#......................................................#............................................................................. +............................................................................................................................................ +............................................................................................................................................ +.......................#.........................................................................................#.......................... +...#...........................#..................................................................#......#................#................. +...............................................#................#........................................................................... +....................#.................................#.................#.......#..........#........................................#....... +...........#.................................................................................................#.............................# +.#..................................................................................................#....................................... +.....................................#.................................................................................#.................... +...............#.........#...........................................................#...................................................... +................................#........................#........#......................................................................... +......#..........................................#.............................................................................#............ +...............................................................................................#.......#.................................... +...............................................................................................................#............................ +..............................#...........#............................#.................................................#............#..... +...................#...........................#............................................................................................ +...............................................................#.....................#.....#................................................ +........#.................#.......................................................................................#...........#............. +...............#............................#.........#.......................#...........................#................................. +.#...................................#...................................#................................................................#. +.........................................................................................#...........................................#...... +........................#................................................................................................................... +....................................................................................................................#....................... +........................................#.....#.........#.........#......................................................................... +.......................................................................................#............#....................................... +.#..............................#.............................................................#........................#.....#.....#........ +............#...............................................................#.................................#.........................#... +..............................................................#.........................................#................................... +........................#..............................#...............#........#.........#................................................. +...................#........................#.......................................................................#....................... +.......................................#...................................................................#................................ +..............#..................#..............#...................................................................................#....... +........#......................................................#..............#.................#.........................................#. +....................................................................#.......................................................#............... +.............................#..........................................................................#................................... +.................#..................................................................#....................................................... +............................................#..........................#............................................#..............#........ +......#..............#..........................................#...........#..............................#................................ +............#.....................#.................................................................#....................................... +...........................#.............#...............#..................................................................#............... +#....................................................................................................................................#...... +.............................................................#.....................#........................................................ +.......#..........................................#.................#....................................#........#.......................#. +..........................................................................................#................................................. +..........................#..............................................#......................#........................................... +...............................#.....................#..........................#.............................#............................. +....#......#................................................#..........................#.......................................#............ +..................#.............................#........................................................................................... +...........................................#..................................................#............................................. +.......................#...........................................#...................................................#...................# +.......#......#.............#.........................#..................#..............................#...........................#....... +..#..............................................................................................................#.......................... \ No newline at end of file diff --git a/inputs/11_test.txt b/inputs/11_test.txt new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/inputs/11_test.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file