Skip to content

Commit 2c34d59

Browse files
committed
matrix utils
1 parent 454889b commit 2c34d59

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

notebooks/y2024/d25.clj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns y2024.d25
22
(:require
33
[advent-of-code-clj.input :as input]
4-
[advent-of-code-clj.utils :as utils]))
4+
[advent-of-code-clj.matrix :as mx]))
55

66
(def test-input "#####
77
.####
@@ -54,11 +54,11 @@
5454
(defn parse-key [input]
5555
(map (fn [line]
5656
(dec (count (filter #{\.} line))))
57-
(apply mapv vector (utils/text->matrix input))))
57+
(mx/transpose (mx/text->matrix input))))
5858
(defn parse-lock [input]
5959
(map (fn [line]
6060
(dec (count (filter #{\#} line))))
61-
(apply mapv vector (utils/text->matrix input))))
61+
(mx/transpose (mx/text->matrix input))))
6262

6363
(defn parse [input]
6464
(let [lock-or-key (.split input "\n\n")

src/advent_of_code_clj/matrix.clj

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(ns advent-of-code-clj.matrix)
2+
3+
(defn transpose [xs-of-xses]
4+
(apply mapv vector xs-of-xses))
5+
6+
(defn coord-map-fixed
7+
{:malli/schema [:-> [:sequential [:sequential :any]] [:map-of [:tuple :int :int] :any]]}
8+
[xs-of-xses]
9+
(->> xs-of-xses
10+
(map-indexed (fn [idy xs]
11+
(map-indexed (fn [idx v]
12+
[[idy idx] v])
13+
xs)))
14+
(transduce cat merge)))
15+
16+
(defn text->matrix
17+
{:malli/schema [:-> :string [:vector [:vector char?]]]}
18+
[text]
19+
(mapv vec (.split text "\n")))

src/advent_of_code_clj/utils.clj

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
(ns advent-of-code-clj.utils
2-
(:import [java.util HashSet]))
2+
(:import [java.util HashSet])
3+
(:require
4+
[advent-of-code-clj.matrix :as mx]))
35

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

14-
(defn coord-map-fixed
15-
{:malli/schema [:-> [:sequential [:sequential :any]] [:map-of [:tuple :int :int] :any]]}
16-
[xs-of-xses]
17-
(->> xs-of-xses
18-
(map-indexed (fn [idy xs]
19-
(map-indexed (fn [idx v]
20-
[[idy idx] v])
21-
xs)))
22-
(transduce cat merge)))
16+
(def coord-map-fixed mx/coord-map-fixed)
2317

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

2920
(defn adjacent-hv
3021
"Find adjacent coordinates, without diagonals"

0 commit comments

Comments
 (0)