Solve AOC day 4

This commit is contained in:
2020-12-04 15:28:20 -07:00
parent 4504ef37ef
commit 2cacce20d8
6 changed files with 1190 additions and 19 deletions

View File

@@ -0,0 +1,9 @@
package year2020;
import kiss.Prelude;
import kiss.Stream;
using StringTools;
@:build(kiss.Kiss.build("src/year2020/Passports.kiss"))
class Passports {}

View File

@@ -0,0 +1,39 @@
(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"|#])))]
(dict-set 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 (dict-get pp "byr")) 2002) (return false))
(unless (<= 2010 (Std.parseInt (dict-get pp "iyr")) 2020) (return false))
(unless (<= 2020 (Std.parseInt (dict-get pp "eyr")) 2030) (return false))
(let [hgt (dict-get 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 (dict-get 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 (dict-get pp "ecl")]
(unless (<= 0 (.indexOf (.split "amb blu brn gry grn hzl oth" " ") ecl)) (return false)))
(let [pid (dict-get 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))))

View File

@@ -3,10 +3,12 @@ package year2020;
import haxe.ds.Map;
import StringTools;
import kiss.Prelude;
import kiss.Stream;
import year2020.Util;
import year2020.SummingTuples;
import year2020.Passwords;
import year2020.Toboggan;
import year2020.Passports;
@:build(kiss.Kiss.build("src/year2020/Solutions.kiss"))
class Solutions {}

View File

@@ -41,22 +41,9 @@
(assert (= 2 (Toboggan.pathTrees exampleHillTile 0 0 1 2)))
(assert (= 289 (Util.countChar "#" (Toboggan.pathString (Util.readLines "src/year2020/inputs/day3-1.txt") 0 0 3 1))))
(assert (= 5522401584 (let [hillTile (Util.readLines "src/year2020/inputs/day3-1.txt")]
(apply * (for args [[0 0 1 1] [0 0 3 1] [0 0 5 1] [0 0 7 1] [0 0 1 2]] (apply (Toboggan.pathTrees.bind hillTile) args)))))))
(defun :kiss.List<Int> pairWithSum [sum :kiss.List<Int> numbers]
// Put the numbers in a map for random access. This gives an O(n) solution
(deflocal :Map<Int,Int> numbersMap (new Map))
(doFor number numbers
(dict-set numbersMap number (- sum number))
(let [requiredForPair (dict-get numbersMap number)]
(when (numbersMap.exists requiredForPair)
(return [number requiredForPair]))))
null)
(defun :kiss.List<Int> trioWithSum [sum :kiss.List<Int> numbers]
(doFor number numbers
(let [requiredForTrio (- sum number)
pairThatSatisfies (pairWithSum requiredForTrio numbers)]
(when pairThatSatisfies
(return [number (nth pairThatSatisfies 0) (nth pairThatSatisfies 1)]))))
null)
(apply * (for args [[0 0 1 1] [0 0 3 1] [0 0 5 1] [0 0 7 1] [0 0 1 2]] (apply (Toboggan.pathTrees.bind hillTile) args))))))
// Day 4
(assert (= 2 (Passports.countValidPassports (new Stream "src/year2020/inputs/day4-example.txt"))))
(assert (= 250 (Passports.countValidPassports (new Stream "src/year2020/inputs/day4-1.txt"))))
(assert (= 158 (Passports.countValidPassports (new Stream "src/year2020/inputs/day4-1.txt") "strict"))))

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in