-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday15.clj
47 lines (40 loc) · 1.77 KB
/
day15.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(ns advent-2022-clojure.day15
(:require [clojure.string :as str]
[advent-2022-clojure.point :as p]))
(defn parse-line [s]
(let [[x1 y1 x2 y2] (map parse-long (re-seq #"-?\d+" s))]
{:sensor [x1 y1] :beacon [x2 y2]}))
(defn parse-input [input]
(->> input str/split-lines (map parse-line)))
(defn blocked-ranges-for-row [target-row readings]
(keep (fn [{:keys [sensor beacon]}]
(let [[sensor-x sensor-y] sensor
total-distance (p/manhattan-distance sensor beacon)
x-distance (- total-distance (abs (- target-row sensor-y)))]
(when-not (neg-int? x-distance)
[(- sensor-x x-distance) (+ sensor-x x-distance)])))
readings))
(defn combine-blocked-ranges [ranges]
(reduce (fn [acc [low' high' :as r]]
(let [[low high] (last acc)]
(cond
(nil? low) [r]
(<= low' (inc high)) (update-in acc [(dec (count acc)) 1] max high')
:else (conj acc r))))
[]
(sort ranges)))
(defn num-beacons-for-row [target-row readings]
(->> readings (map :beacon) (filter #(= target-row (second %))) distinct count))
(defn part1 [row input]
(let [readings (parse-input input)
ranges (combine-blocked-ranges (blocked-ranges-for-row row readings))]
(- (transduce (map (comp inc abs (partial apply -))) + ranges)
(num-beacons-for-row row readings))))
(defn part2 [max-xy input]
(let [readings (parse-input input)]
(first (for [y (range 0 (inc max-xy))
:let [ranges (combine-blocked-ranges (blocked-ranges-for-row y readings))]
:when (= 2 (count ranges))
:let [x (-> ranges first second inc)]
:when (<= 0 x max-xy)]
(+ (* x 4000000) y)))))