Get the hyphens out of dict specforms

This commit is contained in:
2020-12-10 18:43:24 -07:00
parent 1b58b38806
commit 8795595b1b
7 changed files with 28 additions and 28 deletions

View File

@@ -154,12 +154,12 @@ class Macros {
exps[2] exps[2]
]).withPosOf(wholeExp); ]).withPosOf(wholeExp);
} }
macros["set-nth"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> { macros["setNth"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(3, 3, "(set-nth [list] [index] [value])"); wholeExp.checkNumArgs(3, 3, "(setNth [list] [index] [value])");
arraySet(wholeExp, exps, k); arraySet(wholeExp, exps, k);
}; };
macros["dict-set"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> { macros["dictSet"] = (wholeExp:ReaderExp, exps:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(3, 3, "(dict-set [dict] [key] [value])"); wholeExp.checkNumArgs(3, 3, "(dictSet [dict] [key] [value])");
arraySet(wholeExp, exps, k); arraySet(wholeExp, exps, k);
}; };

View File

@@ -32,8 +32,8 @@ class SpecialForms {
wholeExp.checkNumArgs(2, 2, "(nth [list] [idx])"); wholeExp.checkNumArgs(2, 2, "(nth [list] [idx])");
arrayAccess(wholeExp, args, k); arrayAccess(wholeExp, args, k);
}; };
map["dict-get"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> { map["dictGet"] = (wholeExp:ReaderExp, args:Array<ReaderExp>, k:KissState) -> {
wholeExp.checkNumArgs(2, 2, "(dict-get [dict] [key])"); wholeExp.checkNumArgs(2, 2, "(dictGet [dict] [key])");
arrayAccess(wholeExp, args, k); arrayAccess(wholeExp, args, k);
}; };
@@ -147,7 +147,7 @@ class SpecialForms {
for (nameExp in nameExps) for (nameExp in nameExps)
toVar(nameExp, switch (nameExp.def) { toVar(nameExp, switch (nameExp.def) {
case KeyValueExp(keyExp, nameExp): case KeyValueExp(keyExp, nameExp):
CallExp(Symbol("dict-get").withPosOf(valueExp), [uniqueVarSymbol, keyExp]).withPosOf(valueExp); CallExp(Symbol("dictGet").withPosOf(valueExp), [uniqueVarSymbol, keyExp]).withPosOf(valueExp);
default: default:
CallExp(Symbol("nth").withPosOf(valueExp), CallExp(Symbol("nth").withPosOf(valueExp),
[uniqueVarSymbol, Symbol(Std.string(idx++)).withPosOf(valueExp)]).withPosOf(valueExp); [uniqueVarSymbol, Symbol(Std.string(idx++)).withPosOf(valueExp)]).withPosOf(valueExp);

View File

@@ -332,8 +332,8 @@
(defun _testMaps [] (defun _testMaps []
(deflocal :Map<String,String> myMap [=>"hey" "you" (deflocal :Map<String,String> myMap [=>"hey" "you"
=>"found" "me"]) =>"found" "me"])
(Assert.equals "you" (dict-get myMap "hey")) (Assert.equals "you" (dictGet myMap "hey"))
(Assert.equals "me" (dict-get myMap "found")) (Assert.equals "me" (dictGet myMap "found"))
(doFor =>key value myMap (doFor =>key value myMap
(Assert.isTrue (<= 0 (.indexOf ["hey" "found"] key))) (Assert.isTrue (<= 0 (.indexOf ["hey" "found"] key)))
(Assert.isTrue (<= 0 (.indexOf ["you" "me"] value)))) (Assert.isTrue (<= 0 (.indexOf ["you" "me"] value))))

View File

@@ -10,22 +10,22 @@
(let [parts (str.split " ") (let [parts (str.split " ")
quantity (Std.parseInt (parts.shift)) quantity (Std.parseInt (parts.shift))
colorStr (parts.join " ")] colorStr (parts.join " ")]
(dict-set innerMap (bagColor colorStr) quantity) (dictSet innerMap (bagColor colorStr) quantity)
(unless (childMap.exists (bagColor colorStr)) (dict-set childMap (bagColor colorStr) [])) (unless (childMap.exists (bagColor colorStr)) (dictSet childMap (bagColor colorStr) []))
(.push (dict-get childMap (bagColor colorStr)) (bagColor containerStr)))) (.push (dictGet childMap (bagColor colorStr)) (bagColor containerStr))))
(dict-set parentMap (bagColor containerStr) innerMap)))) (dictSet parentMap (bagColor containerStr) innerMap))))
(defun findIndirectContainers [color :ChildMap childMap :Map<String,Bool> outMap] (defun findIndirectContainers [color :ChildMap childMap :Map<String,Bool> outMap]
(when (childMap.exists color) (when (childMap.exists color)
(doFor parentColor (dict-get childMap color) (doFor parentColor (dictGet childMap color)
(dict-set outMap parentColor true) (dictSet outMap parentColor true)
(findIndirectContainers parentColor childMap outMap)))) (findIndirectContainers parentColor childMap outMap))))
(defun totalChildBags [bag :ParentMap parentMap] (defun totalChildBags [bag :ParentMap parentMap]
(if (parentMap.exists bag) (if (parentMap.exists bag)
(begin (begin
(deflocal &mut sum 0) (deflocal &mut sum 0)
(doFor =>childColor quantity (dict-get parentMap bag) (doFor =>childColor quantity (dictGet parentMap bag)
(set sum (+ sum quantity (* quantity (totalChildBags childColor parentMap))))) (set sum (+ sum quantity (* quantity (totalChildBags childColor parentMap)))))
sum) sum)
0)) 0))

View File

@@ -8,7 +8,7 @@
(let [yesDict (new Map<String,Int>)] (let [yesDict (new Map<String,Int>)]
(doFor person group (doFor person group
(doFor question (person.split "") (doFor question (person.split "")
(dict-set yesDict question (dictSet yesDict question
(+ 1 (+ 1
(if (yesDict.exists question) (dict-get yesDict question) 0))))) (if (yesDict.exists question) (dictGet yesDict question) 0)))))
(count yesDict predicate))) (count yesDict predicate)))

View File

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

View File

@@ -2,8 +2,8 @@
// 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
(deflocal :Map<Int,Int> numbersMap (new Map)) (deflocal :Map<Int,Int> numbersMap (new Map))
(doFor number numbers (doFor number numbers
(dict-set numbersMap number (- sum number)) (dictSet numbersMap number (- sum number))
(let [requiredForPair (dict-get numbersMap number)] (let [requiredForPair (dictGet numbersMap number)]
(when (numbersMap.exists requiredForPair) (when (numbersMap.exists requiredForPair)
(return [number requiredForPair])))) (return [number requiredForPair]))))
null) null)