From 79f385417ab110d6687016fc47f6da0a68e66307 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 12 Dec 2021 19:23:36 -0700 Subject: [PATCH] Solve AOC day 10 --- projects/aoc/src/year2021/Day10.hx | 6 ++++ projects/aoc/src/year2021/Solutions2021.hx | 3 ++ projects/aoc/src/year2021/Solutions2021.kiss | 6 +++- projects/aoc/src/year2021/day10.kiss | 36 ++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 projects/aoc/src/year2021/Day10.hx create mode 100644 projects/aoc/src/year2021/day10.kiss diff --git a/projects/aoc/src/year2021/Day10.hx b/projects/aoc/src/year2021/Day10.hx new file mode 100644 index 00000000..105e79b3 --- /dev/null +++ b/projects/aoc/src/year2021/Day10.hx @@ -0,0 +1,6 @@ +package year2021; + +enum LineType { + Incomplete(expected:Array); + Corrupt(char:String); +} diff --git a/projects/aoc/src/year2021/Solutions2021.hx b/projects/aoc/src/year2021/Solutions2021.hx index ee91930c..2c8847e0 100644 --- a/projects/aoc/src/year2021/Solutions2021.hx +++ b/projects/aoc/src/year2021/Solutions2021.hx @@ -12,6 +12,9 @@ import year2021.Day4; #if day5 import year2021.Day5; #end +#if day10 +import year2021.Day10; +#end using hx.strings.Strings; diff --git a/projects/aoc/src/year2021/Solutions2021.kiss b/projects/aoc/src/year2021/Solutions2021.kiss index e59e796b..8b66729b 100644 --- a/projects/aoc/src/year2021/Solutions2021.kiss +++ b/projects/aoc/src/year2021/Solutions2021.kiss @@ -64,7 +64,11 @@ basinSizes (for b realBasins (count b))] (assert (= 480 (apply + (for =>_ height realLowPoints (+ 1 height))))) (assert (= 1045660 (apply * (.slice (reversed (sort basinSizes)) 0 3)))))) - (dayTodo 10) + (day 10 + (load "day10.kiss") + (assert (= 462693 (apply + (map (map (Util.readLines "src/year2021/inputs/day10.txt") getLineType) score)))) + (let [completionScores (sort (filter (map (map (Util.readLines "src/year2021/inputs/day10.txt") getLineType) completionScore)))] + (assert (= 3094671161 (nth completionScores (Math.floor (/ completionScores.length 2))))))) (dayTodo 11) (day 12 (load "day12.kiss") diff --git a/projects/aoc/src/year2021/day10.kiss b/projects/aoc/src/year2021/day10.kiss new file mode 100644 index 00000000..6675e51b --- /dev/null +++ b/projects/aoc/src/year2021/day10.kiss @@ -0,0 +1,36 @@ +(function :LineType getLineType [:String line] + (let [expected []] + (doFor char (line.split "") + (case char + ("(" (expected.push ")")) + ("[" (expected.push "]")) + ("{" (expected.push "}")) + ("<" (expected.push ">")) + ((when (= (last expected) c) c) + (expected.pop)) + (otherwise + (return (Corrupt char))))) + (Incomplete expected))) + +(function score [:LineType line] + (case line + ((Corrupt ")") 3) + ((Corrupt "]") 57) + ((Corrupt "}") 1197) + ((Corrupt ">") 25137) + (otherwise 0))) + +(function completionScore [:LineType line] + (let [&mut :Float score 0] + (case line + ((Incomplete expected) + (doFor char (reversed expected) + (*= score 5) + (+= score (case char + (")" 1) + ("]" 2) + ("}" 3) + (">" 4) + (otherwise (throw "")))))) + (otherwise)) + (if (= score 0) null score))) \ No newline at end of file