init day 7

This commit is contained in:
2024-12-07 12:00:25 -05:00
parent 6767b9456b
commit 376d37c805
3 changed files with 906 additions and 1 deletions
+16 -1
View File
@@ -36,6 +36,21 @@
(if condition 1 0))
(defn mmap
"map map"
"map map f coll"
[f coll]
(map (partial map f) coll))
(defn mmapv
"mapv mapv f coll"
[f coll]
(mapv (partial mapv f) coll))
(defn mmmap
"map map map f coll"
[f coll]
(map (partial map (partial map f)) coll))
(defn mmmapv
"mapv mapv mapv f coll"
[f coll]
(mapv (partial mapv (partial mapv f)) coll))
+40
View File
@@ -0,0 +1,40 @@
(ns day07
(:require
[core :as c]
[input-manager :refer [get-input]]
[clojure.string :as str]))
(def input (->> (get-input 7)
(map #(str/split % #": "))
(c/mmap c/split-whitespace)
(c/mmmapv parse-long)
(map #(update % 0 first))
(into {})))
(defn valid-row? [target acc [v & vs]]
(if v
(or (valid-row? target (+ acc v) vs)
(valid-row? target (* acc v) vs))
(= target acc)))
;; part 1
(->> input
(filter #(valid-row? (first %) 0 (second %)))
(map first)
(reduce +))
(defn valid-row-2?
([target [v & vs]] (valid-row-2? target v vs))
([target acc [v & vs]]
(if v
(or (valid-row-2? target (+ acc v) vs)
(valid-row-2? target (* acc v) vs)
(valid-row-2? target (parse-long (str acc v)) vs))
(= target acc))))
;; part 2
(->> input
(filter #(apply valid-row-2? %))
(map first)
(reduce +))