-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.clj
141 lines (124 loc) · 3.97 KB
/
core.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(ns _2020.sixteen.core
(:require [util :refer [input]]
[clojure.string :as string]))
(defn parse-rules [rules]
(->> rules
(map (fn [line]
(let [res (re-seq #"([\w| ]+): (\d+)-(\d+) or (\d+)-(\d+)"
line)
[_ label & range-vals] (first res)
[a b x y] (->> range-vals (map read-string))
]
{:label label
:ranges [[a b] [x y]]})))))
(defn parse [f]
(->> (input f)
(partition-by #{""})
(remove (comp #{""} first))
((fn [[rules your nearby]]
{:rules (parse-rules rules)
:your-ticket (-> your second
(string/split #",")
(->> (map read-string)))
:nearby-tickets (->> nearby
rest
(map (fn [line]
(->> (string/split line #",")
(map read-string)))))}))))
(comment
(parse "example.txt")
(parse "input.txt"))
(defn val-in-any-range? [ranges v]
(when v
(->> ranges
(filter
(fn [[a b]]
(<= a v b)))
first)))
(defn error-rate [f]
(let [parsed (parse f)
rules (:rules parsed)
ranges (->> rules (mapcat :ranges))
tickets (:nearby-tickets parsed)
values (->> tickets (mapcat (fn [x] x)))]
(->> values
(remove (partial val-in-any-range? ranges))
(apply +))))
(comment
(error-rate "example.txt")
(error-rate "input.txt")
)
;; part 2
(defn valid-tickets [f]
(let [parsed (parse f)
ranges (->> parsed :rules (mapcat :ranges))
tickets (:nearby-tickets parsed)
]
(->> tickets
(remove
(fn [ticket]
(->> ticket
(remove (partial val-in-any-range? ranges))
first))))))
(comment
(:rules (parse "example-two.txt"))
(valid-tickets "example.txt")
(valid-tickets "example-two.txt"))
(defn possible-indexes [{:keys [ranges]} tickets]
(->> (range 0 (count tickets))
(filter
(fn [i]
(->> tickets
(map
(fn [ticket]
(if (val-in-any-range? ranges (nth ticket i nil))
true
false)))
(every? true?))))
(into #{})))
(defn rules-with-possible-indexes [f]
(let [rules (:rules (parse f))
tickets (valid-tickets f)]
(->> rules
(map (fn [rule]
(assoc rule :ixs (possible-indexes rule tickets)))))))
(defn rules-with-index [f]
(->>
(rules-with-possible-indexes f)
(sort-by (comp count :ixs))
((fn [rules]
(loop [with-index []
rules rules]
(if-let [rule (some-> rules first)]
;; assumes there is only one of these, which happens to be the case
(let [ix (first (:ixs rule))]
(when (> (count (:ixs rule)) 1)
(println "DERP"))
(recur
(conj with-index (assoc rule :ix ix))
(->> rules rest (map (fn [rule]
(update rule :ixs disj ix))))))
with-index))))))
(comment
(rules-with-possible-indexes "example-two.txt")
(rules-with-index "example-two.txt")
)
(defn ticket-with-labels [f]
(let [rules (rules-with-index f)
rule-map (->> rules (map (fn [r] [(:ix r) r])) (into {}))
your-ticket (:your-ticket (parse f))]
(->> your-ticket
(map-indexed
(fn [i v]
[(:label (rule-map i)) v]))
(into {}))))
(comment
(ticket-with-labels "example-two.txt")
(ticket-with-labels "input.txt")
(->>
(ticket-with-labels "input.txt")
(filter (fn [[k _v]]
(string/starts-with? k "departure")))
(map second)
(apply *))
)