Skip to content

Commit

Permalink
day12.clj
Browse files Browse the repository at this point in the history
  • Loading branch information
narimiran committed Dec 12, 2023
1 parent 73f55ec commit 13498a2
Show file tree
Hide file tree
Showing 5 changed files with 1,064 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Day 00: Helper file | [aoc.clj](clojure/aoc.clj) |
[Day 09](http://adventofcode.com/2023/day/9) | [day09.clj](clojure/day09.clj) | .esreveR
[Day 10](http://adventofcode.com/2023/day/10) | [day10.clj](clojure/day10.clj) | No BFS needed.
[Day 11](http://adventofcode.com/2023/day/11) | [day11.clj](clojure/day11.clj) | Off-by-one trap!
<!-- [Day 12](http://adventofcode.com/2023/day/12) | [day12.clj](clojure/day12.clj) | -->
[Day 12](http://adventofcode.com/2023/day/12) | [day12.clj](clojure/day12.clj) | Immutability, meet my friend `atom`.
<!-- [Day 13](http://adventofcode.com/2023/day/13) | [day13.clj](clojure/day13.clj) | -->
<!-- [Day 14](http://adventofcode.com/2023/day/14) | [day14.clj](clojure/day14.clj) | -->
<!-- [Day 15](http://adventofcode.com/2023/day/15) | [day15.clj](clojure/day15.clj) | -->
Expand Down
55 changes: 55 additions & 0 deletions clojure/day12.clj
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)
3 changes: 2 additions & 1 deletion clojure/tests/solutions_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require
day01 day02 day03 day04 day05
day06 day07 day08 day09 day10
day11 ; day12 day13 day14 day15
day11 day12 ; day13 day14 day15
; day16 day17 day18 day19 day20
; day21 day22 day23 day24 day25
[clojure.test :refer [deftest is run-tests successful?]]))
Expand Down Expand Up @@ -35,6 +35,7 @@
(check-day 9 [114 2] [1581679977 889])
(check-day 10 [70 8] [6864 349])
(check-day 11 [374 82000210] [9724940 569052586852])
(check-day 12 [21 525152] [7286 25470469710341])

(let [summary (run-tests)]
(when-not (successful? summary)
Expand Down
Loading

0 comments on commit 13498a2

Please sign in to comment.