From b25e12b3a228122bac62d4e3c9ff94dc5a073cd8 Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Wed, 4 Dec 2024 23:10:24 -0500 Subject: [PATCH] golf --- 2024/src/core.clj | 15 +++++++++ 2024/src/day04.clj | 79 ++++++++++++++++++++-------------------------- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/2024/src/core.clj b/2024/src/core.clj index 56847a6..98b1182 100644 --- a/2024/src/core.clj +++ b/2024/src/core.clj @@ -17,3 +17,18 @@ (if (.find m) (recur m (assoc res (.start m) (.group m))) res))) + +(defn dbg [x] + (println x) + x) + +(defn get-coords + "returns a lazy seq representing list of list x, y tuples" + [list-of-lists] + (->> list-of-lists count range + (map #(->> % (get list-of-lists) count range)) + (map-indexed (fn [row cols] + (map #(list row %) cols))))) + +(defn bool->binary [condition] + (if condition 1 0)) diff --git a/2024/src/day04.clj b/2024/src/day04.clj index fe6c472..4590f48 100644 --- a/2024/src/day04.clj +++ b/2024/src/day04.clj @@ -1,5 +1,7 @@ (ns day04 - (:require [input-manager :refer [get-input]])) + (:require [input-manager :refer [get-input]] + [core :as c] + [clojure.string :as str])) (def input (get-input 4)) @@ -8,51 +10,38 @@ ;; part 1 (->> input - (map (partial map-indexed vector)) - (map-indexed vector) - (reduce (fn [acc [row s]] - (reduce (fn [acc2 [col _c]] - (+ acc2 (->> (for [offset (range 4)] - (map #(apply get-char %) - [[row (+ col offset)] - [row (- col offset)] - [(+ row offset) col] - [(- row offset) col] - [(- row offset) (- col offset)] - [(- row offset) (+ col offset)] - [(+ row offset) (- col offset)] - [(+ row offset) (+ col offset)]])) - (apply map vector) - (filter #(= % (seq "XMAS"))) - count))) - acc - s)) - 0)) + c/get-coords + (map (partial reduce (fn [acc [row col]] + (+ acc + (->> (for [offset (range 4)] + (map #(apply get-char %) + [[row (+ col offset)] + [row (- col offset)] + [(+ row offset) col] + [(- row offset) col] + [(- row offset) (- col offset)] + [(- row offset) (+ col offset)] + [(+ row offset) (- col offset)] + [(+ row offset) (+ col offset)]])) + (apply map vector) + (filter #(= % (seq "XMAS"))) + count))) + 0)) + (reduce +)) ;; part 2 (->> input - (map reverse) - (map (partial map-indexed vector)) - (map-indexed vector) - (reduce (fn [acc [row s]] - (reduce (fn [acc2 [col _c]] - (+ acc2 (if (= (get-char row col) \A) - (let [corners [[(dec row) (dec col)] - [(inc row) (dec col)] - [(dec row) (inc col)] - [(inc row) (inc col)]] - chars (mapv #(apply get-char %) corners)] - (if (and (= 2 - (count (filter #(= % \S) chars)) - (count (filter #(= % \M) chars))) - (not= (get chars 0) - (get chars 3))) - 1 - 0)) - 0))) - acc - s)) - 0) - ; - ) + c/get-coords + (map (partial reduce (fn [acc [row col]] + (+ acc + (if (= (get-char row col) \A) + (->> [[[(dec row) (dec col)] [(inc row) (inc col)]] + [[(inc row) (dec col)] [(dec row) (inc col)]]] + (map (partial map #(apply get-char %))) + (map set) + (apply = #{\M \S}) + c/bool->binary) + 0))) + 0)) + (reduce +))