2
2
(:require [clojure.string :as str]))
3
3
4
4
(defn split-seat [seat] (split-at 7 seat))
5
+ (defn low-instruction? [c] (#{\F \L} c))
5
6
6
- (defn get-row [front]
7
- (first (reduce (fn [[min max] dir]
8
- (let [midpoint (+ min (/ (- max min) 2 ))]
9
- (if (= \F dir) [min midpoint]
10
- [midpoint max])))
11
- [0 128 ]
12
- front)))
13
-
14
- (defn get-column [back]
15
- (first (reduce (fn [[min max] dir]
16
- (let [midpoint (+ min (/ (- max min) 2 ))]
17
- (if (= \L dir) [min midpoint]
18
- [midpoint max])))
19
- [0 8 ]
20
- back)))
7
+ (defn midpoint [low high]
8
+ (-> (- high low) (/ 2 ) (+ low)))
9
+
10
+ (defn binary-space-partition [num-vals instructions]
11
+ (->> (reduce (fn [[low high] dir]
12
+ (let [midpoint (midpoint low high)]
13
+ (if (low-instruction? dir) [low midpoint] [midpoint high])))
14
+ [0 num-vals]
15
+ instructions)
16
+ first))
17
+
18
+ (defn find-row [instructions]
19
+ (binary-space-partition 128 instructions))
20
+
21
+ (defn find-column [instructions]
22
+ (binary-space-partition 8 instructions))
21
23
22
24
(defn seat-id [seat]
23
- (let [[front back ] (split-seat seat)]
24
- (-> (* (get -row front ) 8 )
25
- (+ (get -column back )))))
25
+ (let [[r c ] (split-seat seat)]
26
+ (-> (* (find -row r ) 8 )
27
+ (+ (find -column c )))))
26
28
27
- (defn part1 [input]
29
+ (defn missing-within-collection [coll]
30
+ (reduce (fn [prev v]
31
+ (let [target (inc prev)]
32
+ (if (= v target)
33
+ v
34
+ (reduced target))))
35
+ (sort coll)))
36
+
37
+ (defn apply-to-seat-ids [input f]
28
38
(->> (str/split-lines input)
29
39
(map seat-id)
30
- (apply max)))
40
+ f))
41
+
42
+ (defn part1 [input]
43
+ (apply-to-seat-ids input (partial apply max)))
44
+
45
+ (defn part2 [input]
46
+ (apply-to-seat-ids input missing-within-collection))
0 commit comments