-
-
Save KennyMonster/eb6ea22eaba1a1a1201820ef1bddc1cf to your computer and use it in GitHub Desktop.
AOC 2020 day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns day-2 | |
(:require [clojure.string :as str])) | |
(def data (-> "input/day_2.txt" | |
(slurp) | |
(str/split-lines))) | |
(defn parse [s] | |
(let [[_ pmin pmax pc password] | |
(re-matches #"(\d+)-(\d+) ([a-z]): ([a-z]+)" s)] | |
{:min (Integer/parseInt pmin) | |
:max (Integer/parseInt pmax) | |
:char (.charAt pc 0) | |
:password password})) | |
(defn valid-pw? [m] | |
(<= (:min m) | |
(get (frequencies (:password m)) (:char m) 0) | |
(:max m))) | |
(defn solve-1 [] | |
(count (filter valid-pw? (map parse data)))) | |
(defn valid-pw-2? [m] | |
(let [c1 (.charAt (:password m) (dec (:min m))) | |
c2 (.charAt (:password m) (dec (:max m)))] | |
(= (set (map #(= (:char m) %) [c1 c2])) | |
#{true false}))) | |
(defn solve-2 [] | |
(count (filter valid-pw-2? (map parse data)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment