-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathday02.clj
More file actions
25 lines (20 loc) · 760 Bytes
/
Copy pathday02.clj
File metadata and controls
25 lines (20 loc) · 760 Bytes
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
(ns advent-2020-clojure.day02
(:require [clojure.string :as str])
(:use [advent-2020-clojure.utils :only [xor]]))
(defn parse-line [line]
(let [[_ min max letter word] (re-matches #"(\d+)-(\d+) (\w): (\w+)" line)]
[(Integer/parseInt min) (Integer/parseInt max) (first letter) word]))
(defn sled-password? [[min max c word]]
(let [matches (->> (filter (partial = c) word)
count)]
(<= min matches max)))
(defn toboggan-password? [[min max c word]]
(xor #(= c (get word (dec %)))
[min max]))
(defn solve [input rule]
(->> (str/split-lines input)
(map parse-line)
(filter rule)
count))
(defn part1 [input] (solve input sled-password?))
(defn part2 [input] (solve input toboggan-password?))