Skip to content

Instantly share code, notes, and snippets.

@MarcoNicolodi
Created December 4, 2020 02:56
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 MarcoNicolodi/1ade7abe8782a063884b240bd7a18835 to your computer and use it in GitHub Desktop.
Save MarcoNicolodi/1ade7abe8782a063884b240bd7a18835 to your computer and use it in GitHub Desktop.
(ns advent-of-code-2020.day2
(:require [clojure.string :as str]))
;;part 1
(def path "./resources/day2-input")
(defn wire->policy+password [path]
(->> path
slurp
str/split-lines
(map (comp (fn [m] (-> m
(update :min #(Integer/parseInt %))
(update :max #(Integer/parseInt %))))
(partial apply hash-map)
(partial interleave [:min :max :letter :password])
rest
(partial re-find #"(\d+)-(\d+)\s(\w):\s(\w+)")))))
(defn parse-passwords [policy+password]
(let [freqs (-> policy+password :password (str/split #"") frequencies)]
(and (>= (get freqs (:letter policy+password) 0) (:min policy+password))
(<= (get freqs (:letter policy+password) 0) (:max policy+password)))))
(defn result [path]
(->> path
wire->policy+password
(map parse-passwords)
(filter true?)
count))
;;part 2
(defn wire->policy+password [path]
(->> path
slurp
str/split-lines
(map (comp (fn [m] (-> m
(update :pos1 #(Integer/parseInt %))
(update :pos2 #(Integer/parseInt %))))
(partial apply hash-map)
(partial interleave [:pos1 :pos2 :letter :password])
rest
(partial re-find #"(\d+)-(\d+)\s(\w):\s(\w+)")))))
(defn parse-passwords [policy+password]
(= 1 (count (filter (partial = (:letter policy+password))
((juxt (fn [letters] (str (nth letters (dec (:pos1 policy+password)))))
(fn [letters] (str (nth letters (dec (:pos2 policy+password))))))
(:password policy+password))))))
(defn result [path]
(->> path
wire->policy+password
(map parse-passwords)
(filter true?)
count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment