From 4837016256466e500bee5815c2ae6679262f0022 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Fri, 11 Dec 2020 22:26:07 -0700 Subject: [PATCH] Solve AOC Day 10 part 2 --- kiss/src/kiss/FieldForms.hx | 3 +++ kiss/src/kiss/Helpers.hx | 7 +++++++ projects/aoc/src/year2020/Adapters.kiss | 2 +- projects/aoc/src/year2020/Solutions.kiss | 12 +++++++++--- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/kiss/src/kiss/FieldForms.hx b/kiss/src/kiss/FieldForms.hx index 21eba5f7..45213a03 100644 --- a/kiss/src/kiss/FieldForms.hx +++ b/kiss/src/kiss/FieldForms.hx @@ -41,6 +41,9 @@ class FieldForms { case MetaExp("mut", nameExp): access.remove(AFinal); fieldAccess(formName, fieldName, nameExp, access); + case MetaExp("dynamic", nameExp): + access.push(ADynamic); + fieldAccess(formName, fieldName, nameExp, access); default: if (formName == "defvar" || formName == "defun") { access.push(AStatic); diff --git a/kiss/src/kiss/Helpers.hx b/kiss/src/kiss/Helpers.hx index 5cb497c6..d2f2a353 100644 --- a/kiss/src/kiss/Helpers.hx +++ b/kiss/src/kiss/Helpers.hx @@ -59,6 +59,13 @@ class Helpers { // TODO generic type parameter declarations public static function makeFunction(?name:ReaderExp, argList:ReaderExp, body:List, k:KissState):Function { + if (name != null) { + switch (name.def) { + case MetaExp(_, name): + return makeFunction(name, argList, body, k); + default: + } + } var funcName = if (name != null) { switch (name.def) { case Symbol(name) | TypedExp(_, {pos: _, def: Symbol(name)}): diff --git a/projects/aoc/src/year2020/Adapters.kiss b/projects/aoc/src/year2020/Adapters.kiss index fe3f19ce..9904ab59 100644 --- a/projects/aoc/src/year2020/Adapters.kiss +++ b/projects/aoc/src/year2020/Adapters.kiss @@ -10,7 +10,7 @@ 1))) dist)) -(defun arrangementCount [:kiss.List ratings startingIndex] +(defun &dynamic arrangementCount [:kiss.List ratings startingIndex] (if (= startingIndex (- ratings.length 1)) 1 (let [&mut :Int64 sum 0 startingRating (nth ratings startingIndex) diff --git a/projects/aoc/src/year2020/Solutions.kiss b/projects/aoc/src/year2020/Solutions.kiss index 5c1099a5..24197b30 100644 --- a/projects/aoc/src/year2020/Solutions.kiss +++ b/projects/aoc/src/year2020/Solutions.kiss @@ -1,4 +1,5 @@ (defun main [] +/* // Day 1 (let [p (SummingTuples.pairWithSum 2020 [1721 979 366 299 675 1456])] (assert (and (has p 1721) (has p 299)) "pairWithSum is broken")) @@ -114,6 +115,7 @@ [35 20 15 25 47 40 62 55 65 95 102 117 150 182 127 219 299 277 309 576])))) (let [tuple (SummingTuples.contiguousSumTuple 133015568 (Util.readInts "src/year2020/inputs/day9.txt"))] (assert (= 16107959 (+ (apply min tuple) (apply max tuple))))) + */ // Day 10 (assert (Int64.eq 8 (Adapters.arrangementCount (sort [0 22 16 10 15 5 1 11 7 19 6 12 4]) 0))) @@ -123,6 +125,10 @@ (adapters.push (+ 3 (last adapters))) (let [diffs (Adapters.differences adapters) dist (Adapters.distribution diffs)] - (assert (= 1998 (* (dictGet dist 1) (dictGet dist 3)))))) - ) - + (assert (= 1998 (* (dictGet dist 1) (dictGet dist 3))))) + (let [memoized (memoize Adapters.arrangementCount)] + (set Adapters.arrangementCount #|cast memoized|#) + ) + + (print (Int64.toStr (Adapters.arrangementCount adapters 0))) + ))