Skip to content

Instantly share code, notes, and snippets.

@KennyMonster
Last active December 5, 2020 00:48
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/a51c03c53403e76413aa2fe4d61d57a4 to your computer and use it in GitHub Desktop.
Save KennyMonster/a51c03c53403e76413aa2fe4d61d57a4 to your computer and use it in GitHub Desktop.
AOC 2020 Day 4
(ns day-4
(:require [clojure.string :as str]
[clojure.set :refer [difference]]
[clojure.spec.alpha :as s]))
(def data (slurp "input/day_4.txt"))
(defn parse-passport-chunk [s]
"key/val pairs separated by whitespace into a map"
(into {} (map (fn [[_ key val]] [(keyword key) val])
(re-seq #"(\w+):(\S+)" s))))
(defn input->passports [s]
"Raw input -> map of passwords w/ keyword keys"
(->> (str/split s #"\n\n")
(map parse-passport-chunk)))
(def expected-passport-keys #{:ecl :byr :iyr :hgt :pid :hcl :eyr})
(defn passport-valid? [p]
(empty? (difference expected-passport-keys (set (keys p)))))
(comment
(count (filter true? (map passport-valid? (input->passports data)))))
;; part 2
(s/def ::byr #(<= 1920 (Integer/parseInt %) 2002))
(s/def ::iyr #(<= 2010 (Integer/parseInt %) 2020))
(s/def ::eyr #(<= 2020 (Integer/parseInt %) 2030))
(s/def ::hgt (fn [s]
(let [[_ n u] (re-matches #"(\d+)(cm|in)" s)]
(case u
"cm" (<= 150 (Integer/parseInt n) 193)
"in" (<= 59 (Integer/parseInt n) 76)
false))))
(s/def ::hcl #(re-matches #"#[0-9a-f]{6}" %))
(s/def ::ecl #{"amb" "blu" "brn" "gry" "grn" "hzl" "oth"})
(s/def ::pid #(re-matches #"^\d{9}$" %))
(s/def ::passport (s/keys :req-un [::byr ::iyr ::eyr ::hgt ::hcl ::ecl ::pid]))
(comment
(count (filter true? (map #(s/valid? ::passport %) (input->passports data)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment