-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.clj
27 lines (21 loc) · 989 Bytes
/
day04.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(ns advent-2024-clojure.day04
(:require [abyala.advent-utils-clojure.core :as c]
[abyala.advent-utils-clojure.point :as p]))
(def diag-directions [[1 1] [1 -1] [-1 -1] [-1 1]])
(def all-directions (concat p/cardinal-directions diag-directions))
(def x-mas-neighbors #{[\M \M \S \S] [\M \S \S \M] [\S \S \M \M] [\S \M \M \S]})
(defn four-paths [coord]
(map (fn [dir] (take 4 (iterate #(p/move % dir) coord))) all-directions))
(defn num-xmas-paths [points point]
(if (= (points point) \X)
(c/count-when #(= [\X \M \A \S] (map points %)) (four-paths point))
0))
(defn x-mas? [points point]
(and (= (points point) \A)
(x-mas-neighbors (map (comp points #(p/move point %)) diag-directions))))
(defn part1 [input]
(let [points (p/parse-to-char-coords-map input)]
(c/sum (partial num-xmas-paths points) (keys points))))
(defn part2 [input]
(let [points (p/parse-to-char-coords-map input)]
(c/count-when (partial x-mas? points) (keys points))))