Skip to content

Commit

Permalink
day11.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Dec 11, 2023
1 parent 0849a86 commit 5966c64
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | -->
[Day 11](http://adventofcode.com/2023/day/11) | [day11.clj](clojure/day11.clj) | Off-by-one trap!
<!-- [Day 12](http://adventofcode.com/2023/day/12) | [day12.clj](clojure/day12.clj) | -->
<!-- [Day 13](http://adventofcode.com/2023/day/13) | [day13.clj](clojure/day13.clj) | -->
<!-- [Day 14](http://adventofcode.com/2023/day/14) | [day14.clj](clojure/day14.clj) | -->
Expand Down
2 changes: 1 addition & 1 deletion clojure/aoc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
43 changes: 43 additions & 0 deletions clojure/day11.clj
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 2 additions & 2 deletions clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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?]]))
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 5966c64

Please sign in to comment.