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

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

View File

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

View File

@@ -3,7 +3,7 @@
(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))
(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))))
@@ -12,13 +12,13 @@
(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")
(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 (dict-get pp "hcl")]
(let [hcl (dictGet pp "hcl")]
(unless (and
(hcl.startsWith "#")
(= hcl.length 7)
@@ -26,9 +26,9 @@
(for c (.split (hcl.substr 1) "")
(<= 0 (.indexOf (.split "0123456789abcdef" "") c))))))
(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)))
(let [pid (dict-get pp "pid")]
(let [pid (dictGet pp "pid")]
(unless (and (= 9 pid.length) (Std.parseInt pid)) (return false))))
(return true))

View File

@@ -2,8 +2,8 @@
// 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)]
(dictSet numbersMap number (- sum number))
(let [requiredForPair (dictGet numbersMap number)]
(when (numbersMap.exists requiredForPair)
(return [number requiredForPair]))))
null)