Solve AOC day 10
This commit is contained in:
6
projects/aoc/src/year2021/Day10.hx
Normal file
6
projects/aoc/src/year2021/Day10.hx
Normal file
@@ -0,0 +1,6 @@
|
||||
package year2021;
|
||||
|
||||
enum LineType {
|
||||
Incomplete(expected:Array<String>);
|
||||
Corrupt(char:String);
|
||||
}
|
@@ -12,6 +12,9 @@ import year2021.Day4;
|
||||
#if day5
|
||||
import year2021.Day5;
|
||||
#end
|
||||
#if day10
|
||||
import year2021.Day10;
|
||||
#end
|
||||
|
||||
using hx.strings.Strings;
|
||||
|
||||
|
@@ -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")
|
||||
|
36
projects/aoc/src/year2021/day10.kiss
Normal file
36
projects/aoc/src/year2021/day10.kiss
Normal 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)))
|
Reference in New Issue
Block a user