55 lines
1.2 KiB
Clojure
55 lines
1.2 KiB
Clojure
(ns aoc2024.2
|
|
(:require
|
|
[aoc.utils :refer [read-input]]))
|
|
|
|
|
|
;; Each row is a report
|
|
;; Each number in the report is a level
|
|
(def tdata '((7 6 4 2 1)
|
|
(1 2 7 8 9)
|
|
(9 7 6 2 1)
|
|
(1 3 2 4 5)
|
|
(8 6 4 4 1)
|
|
(1 3 6 7 9)))
|
|
|
|
;; A report is safe if:
|
|
;; - The levels are either strictly increasing or strictly decreasing
|
|
;; - Any two adjacent levels differ by at:
|
|
;; - at least 1
|
|
;; - at most 3
|
|
|
|
;; The goal is to determine how many reports are safe
|
|
|
|
(defn report-is-safe
|
|
;; unknown if increasing or decreasing
|
|
([report]
|
|
(or (report-is-safe report -)
|
|
(report-is-safe report +)))
|
|
|
|
;; we know the direction
|
|
([report dir]
|
|
(let [[this next :as levels] report]
|
|
(or
|
|
(nil? next)
|
|
(and
|
|
(<= 1 (dir (- next this)) 3)
|
|
(report-is-safe (rest levels) dir))))))
|
|
|
|
(defn without-each
|
|
[v]
|
|
(map-indexed
|
|
(fn [idx _]
|
|
(concat
|
|
(take idx v)
|
|
(drop (inc idx) v)))
|
|
v))
|
|
|
|
(defn report-is-safe-with-allowance
|
|
[report]
|
|
(boolean (some true? (map report-is-safe (cons report (without-each report))))))
|
|
|
|
(map report-is-safe-with-allowance tdata)
|
|
|
|
(let [input (read-input "inputs/input2.txt")]
|
|
(count (filter report-is-safe-with-allowance input)))
|