Skip to content

Commit

Permalink
day10.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Dec 11, 2023
1 parent 073b03d commit 0849a86
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Day 00: Helper file | [aoc.clj](clojure/aoc.clj) |
[Day 07](http://adventofcode.com/2023/day/7) | [day07.clj](clojure/day07.clj) | Changing card representation is the real MVP.
[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) | -->
[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 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) | -->
Expand Down
56 changes: 56 additions & 0 deletions clojure/day10.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
(ns day10
(:require aoc))


(defn find-start [sketch]
(key (first (aoc/grid->points sketch #(= % \S)))))

(defn delta [[x y] [px py]]
[(- x px) (- y py)])

(defn traverse [sketch start]
(loop [[x y :as curr] (aoc/pt+ start [0 1])
prev start
seen #{}
verticals {}]
(let [seen' (conj seen curr)
[dx dy] (delta curr prev)]
(case ((sketch y) x)
\S {:pipes seen'
:verts verticals}
\J (recur (if (zero? dy) [x (dec y)] [(dec x) y])
curr seen' (update verticals y conj x))
\L (recur (if (zero? dy) [x (dec y)] [(inc x) y])
curr seen' (update verticals y conj x))
\7 (recur (if (zero? dy) [x (inc y)] [(dec x) y])
curr seen' verticals)
\F (recur (if (zero? dy) [x (inc y)] [(inc x) y])
curr seen' verticals)
\| (recur [(+ x dx) (+ y dy)]
curr seen' (update verticals y conj x))
\- (recur [(+ x dx) (+ y dy)]
curr seen' verticals)))))

(defn enclosed [seen verticals h w]
(for [y (range h)
:let [row-verts (verticals y)]
:when row-verts
:let [min-vert (apply min row-verts)
max-vert (apply max row-verts)]
x (range w)
:when (and (< min-vert x max-vert)
(not (seen [x y]))
(odd? (aoc/count-if #(< % x) row-verts)))]
1))

(defn solve [input-file]
(let [sketch (aoc/read-input input-file :chars)
start (find-start sketch)
h (count sketch)
w (count (first sketch))
{:keys [pipes verts]} (traverse sketch start)]
[(/ (count pipes) 2)
(count (enclosed pipes verts h w))]))


(solve 10)
3 changes: 2 additions & 1 deletion clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns solutions-tests
(:require
day01 day02 day03 day04 day05
day06 day07 day08 day09 ; day10
day06 day07 day08 day09 day10
; day11 day12 day13 day14 day15
; day16 day17 day18 day19 day20
; day21 day22 day23 day24 day25
Expand Down Expand Up @@ -33,6 +33,7 @@
(check-day 7 [6440 5905] [253954294 254837398])
(check-day 8 nil [11567 9858474970153])
(check-day 9 [114 2] [1581679977 889])
(check-day 10 [70 8] [6864 349])


(let [summary (run-tests)]
Expand Down
Loading

0 comments on commit 0849a86

Please sign in to comment.