-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.clj
46 lines (36 loc) · 1.09 KB
/
day06.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 advent.day06
(:require [advent.core :as c]
[clojure.java.io :as io]
[clojure.pprint :as pp]
[clojure.set :as set]
[clojure.string :as str]))
;; Each group is separated by a blank line
;; Each person's answers are on a single line
;; The answers are letters to identify the questions they answered yes to [a-z]
(defn read-input
"Read input into groups by blank lines"
[file]
(-> file
(io/resource)
slurp
(str/split #"\n\n")))
(defn part1
"For each group remove blank lines and then count unique answers"
[file]
(->> file
read-input
(map #(into #{} (str/replace % #"\n" "")))
(map count)
(apply +)))
(defn unique-to-all-groups
"For each group reduce their answers to the set they all agree on"
[s]
(reduce set/intersection (map #(into #{} %) (str/split s #"\n"))))
(defn part2
"For each group count answers if eeveryone in the group answered the same questions with a yes"
[file]
(->> file
read-input
(map #(unique-to-all-groups %))
(map count)
(apply +)))