do day 12
This commit is contained in:
parent
52e2bf8202
commit
7057247936
84
2025/src/day12.clj
Normal file
84
2025/src/day12.clj
Normal file
@ -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)))))
|
||||
Loading…
x
Reference in New Issue
Block a user