-
Notifications
You must be signed in to change notification settings - Fork 2
/
7.clj
45 lines (34 loc) · 1.14 KB
/
7.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
(ns advent-of-code.2016.7
(:require [clojure.string :as cs]))
(def raw-input (cs/split-lines (slurp "resources/advent/day-7.txt")))
(defn ip-parts [str] (re-seq #"[^\]]\w+[^\[]" str))
(defn ->part-type [str]
(if (cs/starts-with? str "[") :hypernets :supernets))
(defn abba? [s]
(->> (partition 4 1 s)
(some (fn [[a b c d]]
(and (= a d)
(= b c)
(not= a c))))))
(defn tls? [{:keys [supernets hypernets]}]
(and (some abba? supernets)
(not-any? abba? hypernets)))
(defn count-ips [pred]
(->> (map ip-parts raw-input)
(map #(group-by ->part-type %))
(filter pred)
(count)))
(count-ips tls?) ;; solve part 1
(defn aba? [s]
(->> (partition 3 1 s)
(filter (fn [[a b c]] (and (= a c) (not= a b))))
(map #(apply str %))))
(defn ->bab [aba]
(str (second aba) (first aba) (second aba)))
(defn ssl? [{:keys [supernets hypernets]}]
(let [abas (mapcat aba? supernets)
babs (map ->bab abas)
bab? (fn [hn] (some #(cs/includes? hn %) babs))]
(and (not-empty abas)
(some bab? hypernets))))
(count-ips ssl?) ;; solve part 2