Partial progress for Problem 83.
This commit is contained in:
parent
bbffcd4141
commit
3a3dcc70eb
30
83.hs
30
83.hs
|
@ -1,12 +1,28 @@
|
||||||
import Data.List.Split
|
import Data.List.Split
|
||||||
import Data.Matrix
|
import Data.Matrix
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
import Data.Foldable
|
||||||
|
|
||||||
type Value = Integer
|
type Value = Integer
|
||||||
data IsVisited = Visited | Unvisited deriving (Eq, Show)
|
data IsVisited = Visited | Unvisited deriving (Eq, Show)
|
||||||
type Position = (Int, Int)
|
type Position = (Int, Int)
|
||||||
data Element = Element Value IsVisited Position deriving (Eq, Show)
|
data Element = Element Value IsVisited Position deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Monoid Element where
|
||||||
|
mempty = Element 0 Visited (0, 0)
|
||||||
|
e `mappend` Element _ Unvisited _ = e
|
||||||
|
Element _ Unvisited _ `mappend` e = e
|
||||||
|
Element v1 Visited p1 `mappend` Element v2 Visited p2 =
|
||||||
|
case getMin v1 v2 of
|
||||||
|
0 -> mempty
|
||||||
|
v1 -> Element v1 Visited p1
|
||||||
|
v2 -> Element v2 Visited p2
|
||||||
|
|
||||||
|
getMin :: Value -> Value -> Value
|
||||||
|
getMin v1 0 = v1
|
||||||
|
getMin 0 v2 = v2
|
||||||
|
getMin v1 v2 = min v1 v2
|
||||||
|
|
||||||
getUnvisitedNeighbours :: Matrix Element -> Element -> [Element]
|
getUnvisitedNeighbours :: Matrix Element -> Element -> [Element]
|
||||||
getUnvisitedNeighbours m (Element _ _ (i, j)) =
|
getUnvisitedNeighbours m (Element _ _ (i, j)) =
|
||||||
filter (\(Element _ isVisited _) -> isVisited == Visited)
|
filter (\(Element _ isVisited _) -> isVisited == Visited)
|
||||||
|
@ -19,16 +35,10 @@ getNeighbourDistances :: Element -> [Element] -> [Element]
|
||||||
getNeighbourDistances e = map (addValue e)
|
getNeighbourDistances e = map (addValue e)
|
||||||
where addValue (Element ve isVisited _) (Element vn _ p) = Element (ve + vn) isVisited p
|
where addValue (Element ve isVisited _) (Element vn _ p) = Element (ve + vn) isVisited p
|
||||||
|
|
||||||
getMin :: Value -> Value -> Value
|
dijkstraMatrix :: Matrix Element -> Element
|
||||||
getMin v1 0 = v1
|
|
||||||
getMin 0 v2 = v2
|
|
||||||
getMin v1 v2 = min v1 v2
|
|
||||||
|
|
||||||
dijkstraMatrix :: Matrix Element -> (Value, Position)
|
|
||||||
dijkstraMatrix m =
|
dijkstraMatrix m =
|
||||||
let neighbourDistanceMatrix = fmap (\e -> getNeighbourDistances e (getUnvisitedNeighbours m e)) m
|
let neighbourDistanceMatrix = fmap (\e -> getNeighbourDistances e (getUnvisitedNeighbours m e)) m
|
||||||
-- fold over Visited with getMin
|
in fold (fmap fold neighbourDistanceMatrix)
|
||||||
in (0, (0, 0))
|
|
||||||
-- for unvisited element in matrix
|
-- for unvisited element in matrix
|
||||||
-- for unvisited neighbour of element
|
-- for unvisited neighbour of element
|
||||||
-- if element value + neighbour value < min then set as min
|
-- if element value + neighbour value < min then set as min
|
||||||
|
@ -36,7 +46,7 @@ dijkstraMatrix m =
|
||||||
|
|
||||||
nextMatrix :: Matrix Element -> Matrix Element
|
nextMatrix :: Matrix Element -> Matrix Element
|
||||||
nextMatrix m =
|
nextMatrix m =
|
||||||
let (minValue, position) = dijkstraMatrix m
|
let Element minValue _ position = dijkstraMatrix m
|
||||||
maybeMatrix = safeSet (Element minValue Visited position) position m
|
maybeMatrix = safeSet (Element minValue Visited position) position m
|
||||||
in case maybeMatrix of
|
in case maybeMatrix of
|
||||||
Just next -> next
|
Just next -> next
|
||||||
|
@ -57,4 +67,4 @@ main = do
|
||||||
let listsMatrix = map (map read . (splitOn ",")) $ lines contents :: [[Integer]]
|
let listsMatrix = map (map read . (splitOn ",")) $ lines contents :: [[Integer]]
|
||||||
valueMatrix = fromLists listsMatrix
|
valueMatrix = fromLists listsMatrix
|
||||||
mtrx = toElementMatrix valueMatrix
|
mtrx = toElementMatrix valueMatrix
|
||||||
print mtrx
|
print $ nextMatrix mtrx
|
Loading…
Reference in New Issue