about summary refs log tree commit diff
path: root/day12.clj
blob: 15267fb390b869f770adfe6c0e4256e7e2c4abd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(ns org.vuxu.aoc2021.day12
  (:require [clojure.string :as str]))

(def data
  (->> (slurp "day12")
       (str/split-lines)
       (map #(str/split % #"-"))))

(def datamap
  (update-vals
   (group-by first
             (concat
              (map (comp vec reverse) data)
              data))
   #(set (mapv second %))))

(defn dfs [path repeat?]
  (if (= (last path) "end")
    1
    (apply + (for [n (datamap (last path))]
               (cond (not (and (Character/isLowerCase (first n))
                               (some #{n} path)))
                     (dfs (conj path n) repeat?)

                     (and repeat?
                          (= (count (filter #{n} path)) 1)
                          (not= n "start"))
                     (dfs (conj path n) false)

                     :else 0)))))

(def part1
  (dfs ["start"] false))
;; => 4411      

(def part2
  (dfs2 ["start"] true))
;; => 136767