Author: Honza Pokorny <me@honza.ca>
Add day 10
resources/input10.txt | 97 ++++++++++++++++++++++++++++++++ src/aoc/day10.clj | 136 +++++++++++++++++++++++++++++++++++++++++++++
diff --git a/resources/input10.txt b/resources/input10.txt new file mode 100644 index 0000000000000000000000000000000000000000..f0028b97b8543c4ebe00aa075668ea6f93769d18 --- /dev/null +++ b/resources/input10.txt @@ -0,0 +1,97 @@ +8 +40 +45 +93 +147 +64 +90 +125 +149 +145 +111 +126 +9 +146 +38 +97 +103 +6 +122 +34 +18 +35 +96 +86 +116 +29 +59 +118 +102 +26 +66 +17 +74 +94 +5 +114 +128 +1 +75 +47 +141 +58 +65 +100 +63 +12 +53 +25 +106 +136 +15 +82 +22 +117 +2 +80 +79 +139 +7 +81 +129 +19 +52 +87 +115 +132 +140 +88 +109 +62 +73 +46 +24 +69 +101 +110 +16 +95 +148 +76 +135 +142 +89 +50 +72 +41 +39 +42 +56 +51 +57 +127 +83 +121 +33 +32 +23 diff --git a/src/aoc/day10.clj b/src/aoc/day10.clj new file mode 100644 index 0000000000000000000000000000000000000000..1fc7c1445bf345c02ee7480e937e6e293e616781 --- /dev/null +++ b/src/aoc/day10.clj @@ -0,0 +1,136 @@ +(ns aoc.day10 + (:require [clojure.java.io :as io]) + (:gen-class)) + +;; (defn read-input [] +;; (line-seq +;; (io/reader +;; (io/resource "input10.txt")))) + +(def example "16 +10 +15 +5 +1 +11 +7 +19 +6 +12 +4") + +(def example2 "28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3") + +(defn read-input [] + (->> + (io/resource "input10.txt") + io/reader + line-seq + + ;; example2 + ;; clojure.string/split-lines + + (map read-string))) + +(defn builtin [ratings] + (->> (apply max ratings) + (+ 3))) + +(defn jolts [input] + (let [device (builtin input) + input (conj input device) + input (sort input)] + (reduce (fn [acc adap] + (let [prev (:i acc) + diff (- adap prev)] + (if (= 3 diff) + (assoc acc :i adap :j3 (inc (:j3 acc))) + (assoc acc :i adap :j1 (inc (:j1 acc)))))) + {:i 0 :j1 0 :j3 0} + input))) + +(defn next-coll [[n & coll]] + (filter #(and + (> % n) + (<= 1 (- % n) 3)) + coll)) + +(declare jp) + +(defn jolt-possibilities [coll] + (let [nexts (next-coll coll)] + (if-not (seq nexts) + coll + (mapcat (fn [next] + (jp + (filter #(>= % next) coll))) nexts)))) + +(def jp (memoize jolt-possibilities)) + +(defn jolt-possibilities-2 [coll] + (loop [acc #{} + current 0] + (let [nexts (next-coll coll)] + ))) + +;; the recursive solution didn't work on the puzzle input because memory :( +;; the `part-2` fn is someone else's + +(defn part-2 [adapters] + (let [device (+ (apply max adapters) 3) + ;; The number of routes to each adapter is the sum of the number of + ;; routes to each of the possible previous adapters (it's like a messy + ;; Pascal's triangle) so we can get the solution in one pass. + routes (reduce + (fn [r a] + (assoc r a + (apply + (map #(get r % 0) (range (- a 3) a))))) + {0 1} + (sort (conj adapters device)))] + (get routes device))) + +(defn compute-1 [input] + (let [{:keys [j1 j3]} (jolts input)] + (* j1 j3))) + +(defn compute-2 [input] + (let [input (cons 0 input) + device (builtin input) + input (conj input device) + input (sort input)] + (count (jp input)))) + +(defn main [] + (let [input (read-input)] + (println (compute-1 input)) + (println (part-2 input))))