about summary refs log tree commit diff
path: root/day09.clj
blob: 5502b5b1084426fc4d8cc60d54adfa688cdd88e4 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
(ns org.vuxu.aoc2021.day09
  (:require [clojure.string :as str]))

(defn parse-digit [char]
  (Character/digit char 10))

(def data
  (->> (slurp "day09")
       (str/split-lines)
       (mapv (partial mapv parse-digit))))

(def datamap
  (zipmap (for [x (range (count data))
                y (range (count (data x)))]
            [x y])
          (flatten data)))

(def low-points 
  (filter (fn [[x y]]
            (< (get datamap [x y])
               (apply min (for [[dx dy] [[-1 0] [1 0] [0 -1] [0 1]]]
                            (get datamap [(+ x dx) (+ y dy)] 999)))))
          (keys datamap)))

(def part1
  (->> low-points
       (map datamap)
       (map inc)
       (apply +)))
;; => 512

(defn grow [[x y]]
  (->> (for [[dx dy] [[-1 0] [1 0] [0 -1] [0 1]]]
         [(+ x dx) (+ y dy)])
       (filter (fn [[xx yy]]
                 (not= 9 (get datamap [xx yy] 9))))))

(defn fix [f x]
  (loop [x x]
    (let [fx (f x)]
      (if (= x fx) x (recur fx)))))

(defn measure [p]
  (count (fix (fn [ps] (apply conj ps (mapcat grow ps)))
              (set [p]))))

(def part2
  (->> low-points
       (map measure)
       sort
       (take-last 3)
       (apply *)))
;; => 1600104