aoc

ref: master

2020/src/aoc/day05.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(ns aoc.day05
  (:require [clojure.java.io :as io]))

(def example "FBFBBFFRLR") ;; 44 5

(defn reduce-range [lower upper]
  (fn [coll direction]
    (let [size (/ (count coll) 2)]
    (condp = direction
      lower (take size coll)
      upper (drop size coll)))))

(defn find-row [directions]
  (first
    (reduce
      (reduce-range \F \B)
      (range 0 128)
      directions)))

(defn find-column [directions]
  (first
    (reduce
      (reduce-range \L \R)
      (range 0 8)
      directions)))

(defn find-seat [boarding-pass]
  {:row (find-row (take 7 boarding-pass))
   :column (find-column (drop 7 boarding-pass))})

(defn seat-id [{:keys [row column]}]
  (+ (* row 8)
     column))

(defn read-input []
  (->> (slurp (io/resource "input05.txt"))
       (clojure.string/split-lines)))

(defn compute-1 [input]
  (->> input
       (map find-seat)
       (map seat-id)
       sort
       reverse
       first))

(defn all-seat-ids [input]
  (->> input
       (map find-seat)
       (map seat-id)
       sort))

(defn compute-2 [input]
  (let [seat-ids (all-seat-ids input)
        coll (range (first seat-ids) (inc (last seat-ids)))]
    (clojure.set/difference
     (set coll)
     (set seat-ids))))

(defn main []
  (let [input (read-input)]
    (println (compute-1 input))
    (println (compute-2 input))))

(main)