Solve AOC day 10

This commit is contained in:
2021-12-12 19:23:36 -07:00
parent 617a31af7a
commit 79f385417a
4 changed files with 50 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
package year2021;
enum LineType {
Incomplete(expected:Array<String>);
Corrupt(char:String);
}

View File

@@ -12,6 +12,9 @@ import year2021.Day4;
#if day5
import year2021.Day5;
#end
#if day10
import year2021.Day10;
#end
using hx.strings.Strings;

View File

@@ -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")

View File

@@ -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)))