about summary refs log tree commit diff
path: root/day20.clj
blob: 017d8294d532b5d341a8136c891346728fee6737 (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
(def tiles
  (->> (clojure.string/split (slurp "day20") #"\n\n")
       (map (fn [x]
              (let [[name & data] (clojure.string/split-lines x)]
                {(read-string (first (re-seq #"\d+" name)))
                 (vec data)}
                )))
       (into {})))

;; clockwise
(defn borders [tile]
  [(first tile)
   (apply str (reverse (first tile)))

   (apply str (map last tile))
   (apply str (reverse (map last tile)))

   (last tile)
   (apply str (reverse (last tile)))

   (apply str (map first tile))
   (apply str (reverse (map first tile)))
   ])

(def tile-borders
  (into {} (for [[k v] tiles] [k (borders v)])))

(def corners
  (filter
   (fn [x]
     (= 2 (count
           (remove empty?
                   (for [y (keys tile-borders) :when (not= x y)]
                     (clojure.set/intersection (set (tile-borders x))
                                               (set (tile-borders y))))))))
   (keys tile-borders)))

(apply * corners)  ; => 29584525501199

;; too lazy to do part2, guess it instead:

(defn count-hashes [tile]
  (->> (rest (butlast tile))
       (map (comp rest butlast))
       flatten
       (filter #{\#})
       count))

(apply + (map count-hashes (vals tiles))) ;; => 2175

;; grep '#\.\.\.\.##' day20 | wc -l
;; 41

(- 2175 (* 15 34)) ;; => 1665