From 6c967062d9c0b6ad83bf030160520cf44974a517 Mon Sep 17 00:00:00 2001 From: Adam Jeniski Date: Mon, 11 Dec 2023 09:45:58 -0500 Subject: [PATCH] do day 11 --- 2023/src/day11.clj | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 2023/src/day11.clj diff --git a/2023/src/day11.clj b/2023/src/day11.clj new file mode 100644 index 0000000..d894e9a --- /dev/null +++ b/2023/src/day11.clj @@ -0,0 +1,53 @@ +(ns day11 + (:require [core :refer [get-puzzle-input]])) + +(def input (get-puzzle-input 11)) + +(def empty-rows + (->> input + (map-indexed vector) + (filter (fn [[_ line]] + (every? #(= \. %) line))) + (map first))) + +(def empty-cols + (->> input first count range + (filter (fn [n] (every? #(= \. %) + (map #(.charAt % n) input)))))) + +(defn gen-hash-locations [modifier] + (->> input + (map-indexed (fn [row line] + (map-indexed (fn [col c] + [[row col] c]) + line))) + (apply concat) + (filter #(= \# (second %))) + (map first) + (map (fn [[row col]] + [(+ row (* (dec modifier) + (count (filter #(> row %) empty-rows)))) + (+ col (* (dec modifier) + (count (filter #(> col %) empty-cols))))])))) + +;; part 1 +(let [locs (gen-hash-locations 2)] + (->> (into #{} (for [[x1 y1 :as a] locs + [x2 y2 :as b] locs + :when (or (not= x1 x2) + (not= y1 y2))] + (sort [a b]))) + (map (fn [[[x1 y1] [x2 y2]]] + (+ (abs (- x2 x1)) (abs (- y2 y1))))) + (reduce +))) + +;; part 2 +(let [locs (gen-hash-locations 1000000)] + (->> (into #{} (for [[x1 y1 :as a] locs + [x2 y2 :as b] locs + :when (or (not= x1 x2) + (not= y1 y2))] + (sort [a b]))) + (map (fn [[[x1 y1] [x2 y2]]] + (+ (abs (- x2 x1)) (abs (- y2 y1))))) + (reduce +)))