-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.clj
44 lines (33 loc) · 1.13 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
(ns advent.day02
(:require [advent.core :as c]
[clojure.math.combinatorics :as combo]
[clojure.pprint :as pp]
[clojure.string :as str]))
(defn read-input [file]
(->> file
(c/read-lines)))
(defn parse-input [input]
(let [[v c s] (str/split input #" ")
chr (first (first (str/split c #":")))
[c1 c2] (map #(Integer/parseInt %) (str/split v #"-"))
freq (frequencies s)
cnt (get freq chr)]
(when cnt
(and (>= cnt c1) (<= cnt c2)))))
(defn part1 [file]
(let [lst (read-input file)]
(count (filter true? (map parse-input lst)))))
(defn parse-input2 [input]
(let [[v c s] (str/split input #" ")
[p1 p2] (map #(Integer/parseInt %) (str/split v #"-"))
chr (first (first (str/split c #":")))
c1 (get s (dec p1))
c2 (get s (dec p2))]
(if (not (= c1 c2))
(and (or (= c1 chr) (= c2 chr))))))
(defn part2 [file]
;; a-b are positions
;; one of which must contain the letter
;; valid if one or the other position has the value
(let [lst (read-input file)]
(count (filter true? (map parse-input2 lst)))))