35 lines
2.1 KiB
Haskell
35 lines
2.1 KiB
Haskell
module Part2 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 <= 10)
|
|
|
|
astarNext :: City -> Node -> [(Node, Int)]
|
|
astarNext city ((y, x), East, n) | n < 4 = cleanAstarNext city [((y, x + 1), East, n + 1)]
|
|
| otherwise = cleanAstarNext city [((y - 1, x), North, 1),
|
|
((y + 1, x), South, 1),
|
|
((y, x + 1), East, n + 1)]
|
|
astarNext city ((y, x), South, n) | n < 4 = cleanAstarNext city [((y + 1, x), South, n + 1)]
|
|
| otherwise = cleanAstarNext city [((y + 1, x), South, n + 1),
|
|
((y, x + 1), East, 1),
|
|
((y, x - 1), West, 1)]
|
|
astarNext city ((y, x), West, n) | n < 4 = cleanAstarNext city [((y, x - 1), West, n + 1)]
|
|
| otherwise = cleanAstarNext city [((y - 1, x), North, 1),
|
|
((y + 1, x), South, 1),
|
|
((y, x - 1), West, n + 1)]
|
|
astarNext city ((y, x), North, n) | n < 4 = cleanAstarNext city [((y - 1, x), North, n + 1)]
|
|
| otherwise = 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 <- [4..10]]
|