-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,064 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
(ns day12 | ||
(:require aoc | ||
[clojure.string :as str])) | ||
|
||
|
||
(def cache (atom {})) | ||
|
||
(def is-operational? #{\. \?}) | ||
(def is-damaged? #{\# \?}) | ||
|
||
|
||
(defn parse-line [line] | ||
(let [[pattern groups] (str/split line #" ")] | ||
[pattern (aoc/integers groups)])) | ||
|
||
(defn unfold [[patts groups]] | ||
[(->> patts | ||
(repeat 5) | ||
(interpose "?") | ||
(apply str)) | ||
(->> groups | ||
(repeat 5) | ||
flatten)]) | ||
|
||
|
||
(defn arrangements [[pattern groups :as pg]] | ||
(if-let [res (@cache pg)] | ||
res | ||
(if (empty? groups) | ||
(if (every? is-operational? pattern) 1 0) | ||
(let [[size & tl] groups | ||
post (+ (reduce + tl) (count tl)) | ||
score (atom 0)] | ||
(doseq [pre (range (inc (- (count pattern) post size))) | ||
:let [[before pattern'] (split-at pre pattern) | ||
[current remaining] (split-at size pattern')] | ||
:while (every? is-operational? before) | ||
:when (every? is-damaged? current)] | ||
(cond | ||
(empty? tl) (when (every? is-operational? remaining) | ||
(swap! score inc)) | ||
(is-operational? (first remaining)) | ||
(swap! score + (arrangements [(rest remaining) tl])))) | ||
(swap! cache assoc pg @score) | ||
@score)))) | ||
|
||
|
||
(defn solve [input-file] | ||
(let [lines (aoc/read-input input-file parse-line) | ||
unfolded (map unfold lines)] | ||
[(reduce + (map arrangements lines)) | ||
(reduce + (pmap arrangements unfolded))])) | ||
|
||
|
||
(solve 12) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.