-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.clj
46 lines (37 loc) · 1.21 KB
/
day02.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
(ns day02
(:require [clojure.string :as str]))
(def raw-input (slurp "./input/day02.txt"))
(def codes (str/split-lines raw-input))
(defn unique-frequencies
[c] (into [] (set (vals (frequencies c)))))
(def answer-part-a
(delay (let [char-counts (map unique-frequencies codes)
sum-counts (frequencies (flatten char-counts))]
(* (get sum-counts 2) (get sum-counts 3)))))
(defn different-by-one?
[a b] (= 1 (get (frequencies (map = a b)) false)))
(defn find-adjacent
([] nil)
([a] nil)
([a b & rest] (if (different-by-one? a b)
[a b]
nil)))
(defn search
"Starting at the beggining of the collection, pass the remander of the list to the provided
function until it returns a non-nil result. Returns the result (if any)"
[f col]
(loop [remaining col]
(if (empty? remaining)
nil
(let [result (apply f remaining)]
(if (nil? result)
(recur (rest remaining))
result)))))
(defn keep-if-same
[a b] (if (= a b) a nil))
(def answer-part-b
(delay
(let [[code-a code-b] (search find-adjacent (sort codes))]
(apply str
(filter char?
(map keep-if-same code-a code-b))))))