-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday09.clj
26 lines (23 loc) · 872 Bytes
/
day09.clj
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
(ns clojure-solutions.day09
(:require [clojure.string :as str]
[clojure-aoc-util.util :as aoc]))
(defn- parse []
(->> (slurp "../inputs/day09.txt")
str/split-lines
(mapv (comp (partial mapv read-string) aoc/words))))
(defn- mk-sequences [xs]
(loop [[r & _ :as res] (list xs)]
(if (every? zero? r)
res
(recur (conj res (mapv (fn [a b] (- b a)) r (rest r)))))))
(defn day09 [p]
(let [readings (parse)]
(letfn [(solve [extract gen-val]
(aoc/sum (map #(extract (reduce gen-val (mk-sequences %)))
readings)))]
(case p
:one (solve peek (fn [xs ys]
(let [x (peek xs), y (peek ys)]
(conj ys (+ y x)))))
:two (solve first (fn [[x :as _] [y :as ys]]
(cons (- y x) ys)))))))