From 8993238612199ac328e336be36348f0152aa20ad Mon Sep 17 00:00:00 2001 From: Aleh Suprunovich Date: Thu, 4 Dec 2025 14:00:17 +0300 Subject: [PATCH] day 4 --- src/day4.clj | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/day4.clj diff --git a/src/day4.clj b/src/day4.clj new file mode 100644 index 0000000..7dce8e1 --- /dev/null +++ b/src/day4.clj @@ -0,0 +1,67 @@ +(ns day4 + (:require [clojure.string :as string] + [helpers :refer [get-input]])) + +(def example +"..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@.") + +(def input (mapv (partial apply vector) (string/split-lines example))) +(def input (mapv (partial apply vector) (string/split-lines (get-input 4)))) +(def line-len (count input)) +(def column-len (count (first input))) + +(defn adjacent [[line col]] + (let [first-col (dec col) + last-col (inc col) + line-up (map #(vector (dec line) %) (range first-col (inc last-col))) + line-down (map #(vector (inc line) %) (range first-col (inc last-col)))] + (->> (concat line-up [[line first-col] [line last-col]] line-down) + (remove (fn [[line col]] (or (neg? line) (neg? col)))) + (filter (fn [[line col]] (and (< line line-len) (< col column-len))))))) + +(def coords + (->> + (range line-len) + (mapcat + (fn [l] + (map (fn [c] [l c]) + (range column-len)))))) + +(defn roll? [m c] + (= \@ (get-in m c))) + +(defn adjacent-rolls [m c] + (filter (partial roll? m) + (adjacent c))) + +(defn accessible? [m c] + (and + (roll? m c) + (> 4 (count (adjacent-rolls m c))))) + +(defn accessible-rolls [m] + (filter (partial accessible? m) coords)) + +(defn remove-rolls [m rolls] + (reduce (fn [m c] (assoc-in m c \x)) + m rolls)) + +(println "Result:" (count (accessible-rolls input))) + +(println "Total:" + (loop [m input + a (accessible-rolls input) + r 0] + (if (empty? a) + r + (let [m' (remove-rolls m a)] + (recur m' (accessible-rolls m') (+ r (count a)))))))