Solve AOC day 12 pt 1
This commit is contained in:
@@ -3,9 +3,15 @@ package;
|
|||||||
import kiss.Kiss;
|
import kiss.Kiss;
|
||||||
import kiss.Prelude;
|
import kiss.Prelude;
|
||||||
import kiss.List;
|
import kiss.List;
|
||||||
|
#if year2018
|
||||||
import year2018.Solutions2018;
|
import year2018.Solutions2018;
|
||||||
|
#end
|
||||||
|
#if year2020
|
||||||
import year2020.Solutions2020;
|
import year2020.Solutions2020;
|
||||||
|
#end
|
||||||
|
#if year2021
|
||||||
import year2021.Solutions2021;
|
import year2021.Solutions2021;
|
||||||
|
#end
|
||||||
|
|
||||||
@:build(kiss.Kiss.build())
|
@:build(kiss.Kiss.build())
|
||||||
class Main {}
|
class Main {}
|
||||||
|
@@ -13,5 +13,7 @@ import year2021.Day4;
|
|||||||
import year2021.Day5;
|
import year2021.Day5;
|
||||||
#end
|
#end
|
||||||
|
|
||||||
|
using hx.strings.Strings;
|
||||||
|
|
||||||
@:build(kiss.Kiss.build())
|
@:build(kiss.Kiss.build())
|
||||||
class Solutions2021 {}
|
class Solutions2021 {}
|
||||||
|
@@ -63,11 +63,13 @@
|
|||||||
realBasins (for =>lp _ realLowPoints (basinAround lp "src/year2021/inputs/day9.txt"))
|
realBasins (for =>lp _ realLowPoints (basinAround lp "src/year2021/inputs/day9.txt"))
|
||||||
basinSizes (for b realBasins (count b))]
|
basinSizes (for b realBasins (count b))]
|
||||||
(assert (= 480 (apply + (for =>_ height realLowPoints (+ 1 height)))))
|
(assert (= 480 (apply + (for =>_ height realLowPoints (+ 1 height)))))
|
||||||
|
|
||||||
(assert (= 1045660 (apply * (.slice (reversed (sort basinSizes)) 0 3))))))
|
(assert (= 1045660 (apply * (.slice (reversed (sort basinSizes)) 0 3))))))
|
||||||
(dayTodo 10)
|
(dayTodo 10)
|
||||||
(dayTodo 11)
|
(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 13)
|
||||||
(dayTodo 14)
|
(dayTodo 14)
|
||||||
(dayTodo 15)
|
(dayTodo 15)
|
||||||
|
37
projects/aoc/src/year2021/day12.kiss
Normal file
37
projects/aoc/src/year2021/day12.kiss
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
(function link [from to :Map<String,Array<String>> m]
|
||||||
|
(unless (m.exists from)
|
||||||
|
(dictSet m from []))
|
||||||
|
(.push (dictGet m from) to))
|
||||||
|
|
||||||
|
(function pathsMap [file]
|
||||||
|
(let [:Map<String,Array<String>> m (new Map)]
|
||||||
|
(doFor line (Util.readLines file)
|
||||||
|
(let [[from to] (line.split "-")]
|
||||||
|
(link from to m)
|
||||||
|
(link to from m)))
|
||||||
|
m))
|
||||||
|
|
||||||
|
(function :Array<kiss.List<String>> allPaths [start end file]
|
||||||
|
(let [pMap (pathsMap file)
|
||||||
|
:Array<kiss.List<String>> paths []
|
||||||
|
:Array<Map<String,Bool>> 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))
|
7
projects/aoc/src/year2021/inputs/day12-example.txt
Normal file
7
projects/aoc/src/year2021/inputs/day12-example.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
start-A
|
||||||
|
start-b
|
||||||
|
A-c
|
||||||
|
A-b
|
||||||
|
b-d
|
||||||
|
A-end
|
||||||
|
b-end
|
24
projects/aoc/src/year2021/inputs/day12.txt
Normal file
24
projects/aoc/src/year2021/inputs/day12.txt
Normal file
@@ -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
|
Reference in New Issue
Block a user