Files
kiss-vscode/projects/aoc/src/year2020/Passports.kiss

39 lines
2.1 KiB
Plaintext

(defun readPassport [:Stream stream &opt :Map<String,String> pp]
(set pp (or pp (new Map<String,String>)))
(when (stream.isEmpty) (return pp))
(let [key (stream.expect "passport key" (lambda [] (stream.takeUntilAndDrop ":")))
value (stream.expect "passport value" (lambda [] (stream.takeUntilOneOf [" " "\n"] true)))]
(dictSet pp key value))
(if (= #|"\n\n"|# (try (stream.expect "paragraph break" (lambda [] (stream.peekChars 2))) (catch [e] "")))
(begin (stream.dropWhitespace) pp)
(begin (stream.dropWhitespace) (readPassport stream pp))))
(defun checkPassport [:Map<String,String> pp strict]
(doFor key ["byr" "iyr" "eyr" "hgt" "hcl" "ecl" "pid"]
(if !(pp.exists key) (return false)))
(when strict
(unless (<= 1920 (Std.parseInt (dictGet pp "byr")) 2002) (return false))
(unless (<= 2010 (Std.parseInt (dictGet pp "iyr")) 2020) (return false))
(unless (<= 2020 (Std.parseInt (dictGet pp "eyr")) 2030) (return false))
(let [hgt (dictGet pp "hgt")
[min max] (cond ((hgt.endsWith "cm") [150 193]) ((hgt.endsWith "in") [59 76]) (true (return false)))]
(unless (<= min (Std.parseInt hgt) max) (return false)))
(let [hcl (dictGet pp "hcl")]
(unless (and
(hcl.startsWith "#")
(= hcl.length 7)
(apply = (.concat [true]
(for c (.split (hcl.substr 1) "")
(<= 0 (.indexOf (.split "0123456789abcdef" "") c))))))
(return false)))
(let [ecl (dictGet pp "ecl")]
(unless (<= 0 (.indexOf (.split "amb blu brn gry grn hzl oth" " ") ecl)) (return false)))
(let [pid (dictGet pp "pid")]
(unless (and (= 9 pid.length) (Std.parseInt pid)) (return false))))
(return true))
(defun countValidPassports [:Stream stream &opt strict c]
(unless c (set c 0))
(if (stream.isEmpty)
c
(countValidPassports stream strict (if (checkPassport (readPassport stream) strict) (+ c 1) c))))