From 70572479367833b9074e6079f0d19de221b99609 Mon Sep 17 00:00:00 2001 From: ajet Date: Wed, 31 Dec 2025 19:37:09 -1000 Subject: [PATCH] do day 12 --- 2025/src/day12.clj | 84 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 2025/src/day12.clj diff --git a/2025/src/day12.clj b/2025/src/day12.clj new file mode 100644 index 0000000..ced4b92 --- /dev/null +++ b/2025/src/day12.clj @@ -0,0 +1,84 @@ +(ns day12 + (:require input-manager + core + [clojure.string :as str] + clojure.set)) + +(def requirements (->> (input-manager/get-input-raw 2025 12) + (core/split-on-double-newlines) + (map str/split-lines) + last + (map (comp (fn [[x y & cnts]] + [[x y] (into {} (map-indexed vector cnts))]) + (partial map parse-long) + (partial re-seq #"\d+"))))) + +(defn fits-trivially? + "is it trivially possible to fit all the pieces if they were 3x3 blocks instead of jigsaws" + [[dimensions cnts]] + (let [area (apply * dimensions) + guess (->> cnts vals (reduce +) (* 9))] + (>= area guess))) + +;; part 1 +(->> requirements + (filter fits-trivially?) + count) + +(comment + ;; NP Hard... general solution is not fast enough + (defn flip [grid] + (vec (reverse grid))) + (defn rotations [grid] + (->> [(for [x (range 0 3)] + (for [y (range 0 3)] + (.charAt (grid x) y))) + (for [y (range 2 -1 -1)] + (for [x (range 0 3)] + (.charAt (grid x) y))) + (for [x (range 2 -1 -1)] + (for [y (range 2 -1 -1)] + (.charAt (grid x) y))) + (for [y (range 2 -1 -1)] + (for [x (range 3)] + (.charAt (grid x) y)))] + (map (partial map-indexed (fn [row-idx row] + (map-indexed (fn [col-idx cell] + (when (= cell \#) + [row-idx col-idx])) + row)))) + (map (partial mapcat (partial filter identity))) + (mapv (partial mapv vec)) + set)) + + #_(def jigsaws (->> (drop-last (->> (input-manager/get-input-raw 2025 12) + (core/split-on-double-newlines) + (map str/split-lines))) + (map (fn [[idx & puzzle-lines]] + [(parse-long (apply str (drop-last idx))) + (clojure.set/union (rotations (vec puzzle-lines)) (rotations (flip puzzle-lines)))])) + (into {}))) + (defn placeable? [jigsaw filled] + (every? #(not (contains? filled %)) jigsaw)) + + (defn offset [loc jigsaw] + (into #{} (map #(mapv + % loc) jigsaw))) + + (defn fits? [[[rows cols :as dimensions] cnts] jigsaws filled] + (cond + (every? (partial = 0) (vals cnts)) true + :else + (let [[id] (first (filter (fn [[_ cnt]] (> cnt 0)) cnts)) + orientations (jigsaws id) + locs (for [row (range (- rows 2)) + col (range (- cols 2))] + [row col]) + cnts' (update cnts id dec)] + (some (fn [loc] + (->> orientations + (map #(offset loc %)) + (filter #(placeable? % filled)) + (map #(fits? [dimensions cnts'] jigsaws (clojure.set/union filled %))) + (filter identity) + first)) + locs)))))