Skip to content

Instantly share code, notes, and snippets.

@KennyMonster
Created December 2, 2020 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KennyMonster/eb6ea22eaba1a1a1201820ef1bddc1cf to your computer and use it in GitHub Desktop.
Save KennyMonster/eb6ea22eaba1a1a1201820ef1bddc1cf to your computer and use it in GitHub Desktop.
AOC 2020 day 2
(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