Solve AOC Day 2.2

This commit is contained in:
2020-12-02 19:25:28 -07:00
parent 0d0502bb1d
commit 7b41a17046
3 changed files with 27 additions and 12 deletions

View File

@@ -1,12 +1,19 @@
(defun countChar [char str] (defun countChar [char str]
(count (str.split "") (lambda [c] ?(= c char)))) (count (str.split "") (lambda [c] ?(= c char))))
(defun parsePasswordCheck [:String ruleStr] (defun parsePasswordCheck1 [:String ruleStr]
(let [[min max letter] (let [[min max letter]
(.split (ruleStr.replace " " "-") "-")] (.split (ruleStr.replace " " "-") "-")]
(lambda [password] (<= (Std.parseInt min) (countChar letter password) (Std.parseInt max))))) (lambda [password] (<= (Std.parseInt min) (countChar letter password) (Std.parseInt max)))))
(defun validateInputLine [:String line] (defun parsePasswordCheck2 [:String ruleStr]
(let [[a b letter]
(.split (ruleStr.replace " " "-") "-")
aIdx (- (Std.parseInt a) 1)
bIdx (- (Std.parseInt b) 1)]
(lambda [password] (= 1 (countChar letter (+ (.charAt password aIdx) (.charAt password bIdx)))))))
(defun validateInputLine [:String line ruleParser]
(let [[rule password] (let [[rule password]
(line.split ": ")] (line.split ": ")]
((parsePasswordCheck rule) password))) ((ruleParser rule) password)))

View File

@@ -14,14 +14,22 @@
(throw "trioWithSum is broken"))) (throw "trioWithSum is broken")))
// Day 2 // Day 2
(unless (Passwords.validateInputLine "1-3 a: abcde") (unless (Passwords.validateInputLine "1-3 a: abcde" Passwords.parsePasswordCheck1)
(throw "validateInputLine is broken")) (throw "parsePasswordCheck1 is broken"))
(when (Passwords.validateInputLine "1-3 b: cdefg") (when (Passwords.validateInputLine "1-3 b: cdefg" Passwords.parsePasswordCheck1)
(throw "validateInputLine is broken")) (throw "parsePasswordCheck1 is broken"))
(unless (Passwords.validateInputLine "2-9 c: ccccccccc") (unless (Passwords.validateInputLine "2-9 c: ccccccccc" Passwords.parsePasswordCheck1)
(throw "validateInputLine is broken")) (throw "parsePasswordCheck1 is broken"))
(print (map (Util.readLines "src/year2020/inputs/day2-1.txt") Passwords.validateInputLine)) (unless (= 655 (count (map (Util.readLines "src/year2020/inputs/day2-1.txt") (.bind Passwords.validateInputLine _ Passwords.parsePasswordCheck1)) (lambda [v] v)))
) (throw "parsePasswordCheck1 is broken"))
(unless (Passwords.validateInputLine "1-3 a: abcde" Passwords.parsePasswordCheck2)
(throw "parsePasswordCheck2 is broken"))
(when (Passwords.validateInputLine "1-3 b: cdefg" Passwords.parsePasswordCheck2)
(throw "parsePasswordCheck2 is broken"))
(when (Passwords.validateInputLine "2-9 c: ccccccccc" Passwords.parsePasswordCheck2)
(throw "parsePasswordCheck2 is broken"))
(unless (= 673 (count (map (Util.readLines "src/year2020/inputs/day2-1.txt") (.bind Passwords.validateInputLine _ Passwords.parsePasswordCheck2)) (lambda [v] v)))
(throw "parsePasswordCheck2 is broken")))
(defun :kiss.List<Int> pairWithSum [sum :kiss.List<Int> numbers] (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 // Put the numbers in a map for random access. This gives an O(n) solution

View File

@@ -50,7 +50,7 @@ class Kiss {
// Helpful aliases // Helpful aliases
k.defAlias("print", Symbol("Prelude.print")); k.defAlias("print", Symbol("Prelude.print"));
k.defAlias("map", Symbol("Lambda.map")); k.defAlias("map", Symbol("Lambda.map"));
k.defAlias("filter", Symbol("Lambda.filter")); k.defAlias("filter", Symbol("Lambda.filter")); // TODO use truthy as the default filter function
k.defAlias("has", Symbol("Lambda.has")); k.defAlias("has", Symbol("Lambda.has"));
k.defAlias("count", Symbol("Lambda.count")); k.defAlias("count", Symbol("Lambda.count"));