This commit is contained in:
2023-12-17 17:26:34 +01:00
parent e772a7617c
commit a8d5a79235
5 changed files with 137 additions and 0 deletions

30
day17/Part1.hs Normal file
View File

@@ -0,0 +1,30 @@
module Part1 where
import Commons
import Data.Array (Ix(inRange), bounds, (!))
import Data.Maybe (catMaybes)
cleanAstarNext :: City -> [Node] -> [(Node, Int)]
cleanAstarNext city = map (\ (c, d, n) -> ((c, d, n), city ! c))
. filter (\ ((y, x), _, n) -> inRange (bounds city) (y, x) && n <= 3)
astarNext :: City -> Node -> [(Node, Int)]
astarNext city ((y, x), East, n) = cleanAstarNext city [((y - 1, x), North, 1),
((y + 1, x), South, 1),
((y, x + 1), East, n + 1)]
astarNext city ((y, x), South, n) = cleanAstarNext city [((y + 1, x), South, n + 1),
((y, x + 1), East, 1),
((y, x - 1), West, 1)]
astarNext city ((y, x), West, n) = cleanAstarNext city [((y - 1, x), North, 1),
((y + 1, x), South, 1),
((y, x - 1), West, n + 1)]
astarNext city ((y, x), North, n) = cleanAstarNext city [((y - 1, x), North, n + 1),
((y, x + 1), East, 1),
((y, x - 1), West, 1)]
applyAstar :: City -> Direction -> Int -> Maybe Int
applyAstar city d n = astar ((1, 1), East, 0) (snd $ bounds city, d, n) (astarNext city) astarEstimation
getAllAstar :: City -> [Int]
getAllAstar city = catMaybes [applyAstar city d n | d <- [East, South], n <- [1..3]]