about summary refs log tree commit diff
path: root/day16.clj
blob: fce1aa34829b4e2e37e48860ae299cf5bd6477c9 (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
54
55
56
57
58
59
60
61
62
63
(let [data (slurp "day16")
      [a b c] (clojure.string/split data #"\n\n")]
  (def ranges a)
  (def mine b)
  (def nearby c))

(def line-ranges
  (->> ranges
       clojure.string/split-lines
       (map (fn [line]
              (->> line
                   (re-seq #"(\d+)-(\d+)")
                   (map (fn [[_ f t]]
                          (range (Integer/parseInt f)
                                 (inc (Integer/parseInt t)))))
                   flatten)))))

(def all-in-ranges
  (->> line-ranges
       flatten
       set))

(def nearby-tickets
  (->> nearby
       clojure.string/split-lines
       rest
       (map (fn [s] (clojure.string/split s #",")))
       (map (partial map #(Integer/parseInt %)))))

(reduce + (remove all-in-range (flatten nearby-tickets))) ; => 24021

(def my-ticket
  (->> mine
       clojure.string/split-lines
       second
       (#(clojure.string/split % #","))
       (map #(Integer/parseInt %))
       vec))

(def valid-tickets
  (->> nearby-tickets
       (filter (fn [t] (empty? (remove all-in-range t) )))))

(defn valid-positions [line-range]
  (let [s (set line-range)]
    (filter (fn [i]
              (every? (fn [ticket]
                        (s (nth ticket i)))
                      valid-tickets))
            (range (count line-ranges)))))

(->> line-ranges
     (map valid-positions)
     (sort-by count)
     (cons ())
     (partition 2 1) 
     (map (fn [[x y]] (first (remove (set x) y))))
    
     (drop 6)  ;; ???
     (take 6)

     (map my-ticket)
     (apply *)) ; => 1289178686687