Skip to content

Commit

Permalink
matrix utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Oddsor committed Jan 3, 2025
1 parent 454889b commit 2c34d59
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
6 changes: 3 additions & 3 deletions notebooks/y2024/d25.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns y2024.d25
(:require
[advent-of-code-clj.input :as input]
[advent-of-code-clj.utils :as utils]))
[advent-of-code-clj.matrix :as mx]))

(def test-input "#####
.####
Expand Down Expand Up @@ -54,11 +54,11 @@
(defn parse-key [input]
(map (fn [line]
(dec (count (filter #{\.} line))))
(apply mapv vector (utils/text->matrix input))))
(mx/transpose (mx/text->matrix input))))
(defn parse-lock [input]
(map (fn [line]
(dec (count (filter #{\#} line))))
(apply mapv vector (utils/text->matrix input))))
(mx/transpose (mx/text->matrix input))))

(defn parse [input]
(let [lock-or-key (.split input "\n\n")
Expand Down
19 changes: 19 additions & 0 deletions src/advent_of_code_clj/matrix.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns advent-of-code-clj.matrix)

(defn transpose [xs-of-xses]
(apply mapv vector xs-of-xses))

(defn coord-map-fixed
{:malli/schema [:-> [:sequential [:sequential :any]] [:map-of [:tuple :int :int] :any]]}
[xs-of-xses]
(->> xs-of-xses
(map-indexed (fn [idy xs]
(map-indexed (fn [idx v]
[[idy idx] v])
xs)))
(transduce cat merge)))

(defn text->matrix
{:malli/schema [:-> :string [:vector [:vector char?]]]}
[text]
(mapv vec (.split text "\n")))
19 changes: 5 additions & 14 deletions src/advent_of_code_clj/utils.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns advent-of-code-clj.utils
(:import [java.util HashSet]))
(:import [java.util HashSet])
(:require
[advent-of-code-clj.matrix :as mx]))

(defn coord-map
{:malli/schema [:-> [:sequential [:sequential :any]] [:map-of [:tuple :int :int] :any]]}
Expand All @@ -11,20 +13,9 @@
xs)))
(transduce cat merge)))

(defn coord-map-fixed
{:malli/schema [:-> [:sequential [:sequential :any]] [:map-of [:tuple :int :int] :any]]}
[xs-of-xses]
(->> xs-of-xses
(map-indexed (fn [idy xs]
(map-indexed (fn [idx v]
[[idy idx] v])
xs)))
(transduce cat merge)))
(def coord-map-fixed mx/coord-map-fixed)

(defn text->matrix
{:malli/schema [:-> :string [:vector [:vector char?]]]}
[text]
(mapv vec (.split text "\n")))
(def text->matrix mx/text->matrix)

(defn adjacent-hv
"Find adjacent coordinates, without diagonals"
Expand Down

0 comments on commit 2c34d59

Please sign in to comment.