diff --git a/projects/aoc/src/Main.hx b/projects/aoc/src/Main.hx index 130d77a6..1bb3e257 100644 --- a/projects/aoc/src/Main.hx +++ b/projects/aoc/src/Main.hx @@ -3,9 +3,15 @@ package; import kiss.Kiss; import kiss.Prelude; import kiss.List; +#if year2018 import year2018.Solutions2018; +#end +#if year2020 import year2020.Solutions2020; +#end +#if year2021 import year2021.Solutions2021; +#end @:build(kiss.Kiss.build()) class Main {} diff --git a/projects/aoc/src/year2021/Solutions2021.hx b/projects/aoc/src/year2021/Solutions2021.hx index 89ff3034..ee91930c 100644 --- a/projects/aoc/src/year2021/Solutions2021.hx +++ b/projects/aoc/src/year2021/Solutions2021.hx @@ -13,5 +13,7 @@ import year2021.Day4; import year2021.Day5; #end +using hx.strings.Strings; + @:build(kiss.Kiss.build()) class Solutions2021 {} diff --git a/projects/aoc/src/year2021/Solutions2021.kiss b/projects/aoc/src/year2021/Solutions2021.kiss index 7724d784..8eafd097 100644 --- a/projects/aoc/src/year2021/Solutions2021.kiss +++ b/projects/aoc/src/year2021/Solutions2021.kiss @@ -63,11 +63,13 @@ realBasins (for =>lp _ realLowPoints (basinAround lp "src/year2021/inputs/day9.txt")) 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) (dayTodo 11) - (dayTodo 12) + (day 12 + (load "day12.kiss") + (assert (= 10 .length (allPaths "start" "end" "src/year2021/inputs/day12-example.txt"))) + (assert (= 3421 .length (allPaths "start" "end" "src/year2021/inputs/day12.txt")))) (dayTodo 13) (dayTodo 14) (dayTodo 15) diff --git a/projects/aoc/src/year2021/day12.kiss b/projects/aoc/src/year2021/day12.kiss new file mode 100644 index 00000000..89eb0a6d --- /dev/null +++ b/projects/aoc/src/year2021/day12.kiss @@ -0,0 +1,37 @@ +(function link [from to :Map> m] + (unless (m.exists from) + (dictSet m from [])) + (.push (dictGet m from) to)) + +(function pathsMap [file] + (let [:Map> m (new Map)] + (doFor line (Util.readLines file) + (let [[from to] (line.split "-")] + (link from to m) + (link to from m))) + m)) + +(function :Array> allPaths [start end file] + (let [pMap (pathsMap file) + :Array> paths [] + :Array> pathsVisited []] + (paths.push [start]) + (let [pathVisited (new Map)] + (dictSet pathVisited start true) + (pathsVisited.push pathVisited)) + (until (apply = (concat [end] (for path paths (last path)))) + (doFor idx (reversed (collect (range paths.length))) + (let [path (nth paths idx) + pathVisited (nth pathsVisited idx) + position (last path)] + (when (= end position) (continue)) + (paths.splice idx 1) + (pathsVisited.splice idx 1) + (doFor nextPosition (dictGet pMap position) + (unless (dictGet pathVisited nextPosition) + (paths.insert idx (concat (path.copy) [nextPosition])) + (let [nextPathVisited (pathVisited.copy)] + (when (nextPosition.isLowerCase) + (dictSet nextPathVisited nextPosition true)) + (pathsVisited.insert idx nextPathVisited))))))) + paths)) \ No newline at end of file diff --git a/projects/aoc/src/year2021/inputs/day12-example.txt b/projects/aoc/src/year2021/inputs/day12-example.txt new file mode 100644 index 00000000..898cd568 --- /dev/null +++ b/projects/aoc/src/year2021/inputs/day12-example.txt @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end \ No newline at end of file diff --git a/projects/aoc/src/year2021/inputs/day12.txt b/projects/aoc/src/year2021/inputs/day12.txt new file mode 100644 index 00000000..e4501e5a --- /dev/null +++ b/projects/aoc/src/year2021/inputs/day12.txt @@ -0,0 +1,24 @@ +rf-RL +rf-wz +wz-RL +AV-mh +end-wz +end-dm +wz-gy +wz-dm +cg-AV +rf-AV +rf-gy +end-mh +cg-gy +cg-RL +gy-RL +VI-gy +AV-gy +dm-rf +start-cg +start-RL +rf-mh +AV-start +qk-mh +wz-mh