Skip to content

Instantly share code, notes, and snippets.

@stuartstein777
Created December 4, 2020 15:57
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 stuartstein777/358d08828f25a64223cbd51e822f0256 to your computer and use it in GitHub Desktop.
Save stuartstein777/358d08828f25a64223cbd51e822f0256 to your computer and use it in GitHub Desktop.
aoc2020-day2
;; part 1
(defn valid? [{:keys [min max c pwd]}]
(<= min ((frequencies pwd) c 0) max))
;; part 2
(defn xor [a b]
(or (and a (not b))
(and b (not a))))
(defn valid? [{:keys [min max c pwd]}]
(let [p1 (nth pwd (dec min) \_)
p2 (nth pwd (dec max) \_)]
(xor (= c p1) (= c p2))))
;; shared
(defn parse-entry [entry]
(let [[min max c pwd] (rest (re-find #"(\d+)-(\d+) (.): (.+)" entry))]
{:min (Integer/parseInt min)
:max (Integer/parseInt max)
:c (first c)
:pwd pwd}))
(->> (slurp "input.txt")
(str/split-lines)
(map parse-entry)
(filter valid?)
(count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment