Day 2.1 solution (doesn't work because of Kiss bug)
This commit is contained in:
10
projects/advent-of-code/src/year2020/Passwords.hx
Normal file
10
projects/advent-of-code/src/year2020/Passwords.hx
Normal file
@@ -0,0 +1,10 @@
|
||||
package year2020;
|
||||
|
||||
import kiss.Prelude;
|
||||
import kiss.Operand;
|
||||
import year2020.Util;
|
||||
|
||||
using StringTools;
|
||||
|
||||
@:build(kiss.Kiss.build("src/year2020/Passwords.kiss"))
|
||||
class Passwords {}
|
12
projects/advent-of-code/src/year2020/Passwords.kiss
Normal file
12
projects/advent-of-code/src/year2020/Passwords.kiss
Normal file
@@ -0,0 +1,12 @@
|
||||
(defun countChar [char str]
|
||||
(count (str.split "") (lambda [c] ?(= c char))))
|
||||
|
||||
(defun parsePasswordCheck [:String ruleStr]
|
||||
(let [[min max letter]
|
||||
(.split (ruleStr.replace " " "-") "-")]
|
||||
(lambda [password] (<= (Std.parseInt min) (countChar letter password) (Std.parseInt max)))))
|
||||
|
||||
(defun validateInputLine [:String line]
|
||||
(let [[rule password]
|
||||
(line.split ": ")]
|
||||
((parsePasswordCheck rule) password)))
|
@@ -1,9 +1,10 @@
|
||||
package year2020;
|
||||
|
||||
import haxe.ds.Map;
|
||||
import sys.io.File;
|
||||
import StringTools;
|
||||
import kiss.Prelude;
|
||||
import year2020.Util;
|
||||
import year2020.Passwords;
|
||||
|
||||
@:build(kiss.Kiss.build("src/year2020/Solutions.kiss"))
|
||||
class Solutions {}
|
||||
|
@@ -3,25 +3,25 @@
|
||||
(let [p (pairWithSum 2020 [1721 979 366 299 675 1456])]
|
||||
(unless (and (has p 1721) (has p 299))
|
||||
(throw "pairWithSum is broken")))
|
||||
(let [[a b] (pairWithSum 2020 (readInts "src/year2020/inputs/day1-1.txt"))]
|
||||
(let [[a b] (pairWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))]
|
||||
(unless (= 545379 (* a b))
|
||||
(throw "pairWithSum is broken")))
|
||||
(let [t (trioWithSum 2020 [1721 979 366 299 675 1456])]
|
||||
(unless (and (has t 675) (has t 366) (has t 979))
|
||||
(throw "trioWithSum is broken")))
|
||||
(let [[a b c] (trioWithSum 2020 (readInts "src/year2020/inputs/day1-1.txt"))]
|
||||
(let [[a b c] (trioWithSum 2020 (Util.readInts "src/year2020/inputs/day1-1.txt"))]
|
||||
(unless (= 257778836 (* a b c))
|
||||
(throw "trioWithSum is broken"))))
|
||||
|
||||
(defun readLines [file]
|
||||
(.filter
|
||||
(.map
|
||||
// TODO implement escape sequences in kiss string literals
|
||||
(.split (File.getContent file) #|"\n"|#)
|
||||
StringTools.trim)
|
||||
(lambda [l] (< 0 l.length))))
|
||||
|
||||
(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt)))
|
||||
(throw "trioWithSum is broken")))
|
||||
|
||||
// Day 2
|
||||
(unless (Passwords.validateInputLine "1-3 a: abcde")
|
||||
(throw "validateInputLine is broken"))
|
||||
(when (Passwords.validateInputLine "1-3 b: cdefg")
|
||||
(throw "validateInputLine is broken"))
|
||||
(unless (Passwords.validateInputLine "2-9 c: ccccccccc")
|
||||
(throw "validateInputLine is broken"))
|
||||
(print (map (Util.readLines "src/year2020/inputs/day2-1.txt") Passwords.validateInputLine))
|
||||
)
|
||||
|
||||
(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
|
||||
@@ -34,7 +34,6 @@
|
||||
null)
|
||||
|
||||
(defun :kiss.List<Int> trioWithSum [sum :kiss.List<Int> numbers]
|
||||
(deflocal &mut trio null)
|
||||
(doFor number numbers
|
||||
(let [requiredForTrio (- sum number)
|
||||
pairThatSatisfies (pairWithSum requiredForTrio numbers)]
|
||||
|
7
projects/advent-of-code/src/year2020/Util.hx
Normal file
7
projects/advent-of-code/src/year2020/Util.hx
Normal file
@@ -0,0 +1,7 @@
|
||||
package year2020;
|
||||
|
||||
import sys.io.File;
|
||||
import kiss.Prelude;
|
||||
|
||||
@:build(kiss.Kiss.build("src/year2020/Util.kiss"))
|
||||
class Util {}
|
9
projects/advent-of-code/src/year2020/Util.kiss
Normal file
9
projects/advent-of-code/src/year2020/Util.kiss
Normal file
@@ -0,0 +1,9 @@
|
||||
(defun readLines [file]
|
||||
(.filter
|
||||
(.map
|
||||
// TODO implement escape sequences in kiss string literals
|
||||
(.split (File.getContent file) #|"\n"|#)
|
||||
StringTools.trim)
|
||||
(lambda [l] (< 0 l.length))))
|
||||
|
||||
(defun readInts [file] (let [lines (readLines file)] (lines.map Std.parseInt)))
|
1000
projects/advent-of-code/src/year2020/inputs/day2-1.txt
Normal file
1000
projects/advent-of-code/src/year2020/inputs/day2-1.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -52,6 +52,7 @@ class Kiss {
|
||||
k.defAlias("map", Symbol("Lambda.map"));
|
||||
k.defAlias("filter", Symbol("Lambda.filter"));
|
||||
k.defAlias("has", Symbol("Lambda.has"));
|
||||
k.defAlias("count", Symbol("Lambda.count"));
|
||||
|
||||
while (true) {
|
||||
stream.dropWhitespace();
|
||||
|
Reference in New Issue
Block a user