1
0
Fork 0

Problem 83: technically correct solution, but very slow...

This commit is contained in:
Jonathan Chan 2017-08-17 22:29:21 -07:00
parent 3a3dcc70eb
commit 5a49c8845f
4 changed files with 33 additions and 15 deletions

BIN
83

Binary file not shown.

43
83.hs
View File

@ -9,14 +9,15 @@ type Position = (Int, Int)
data Element = Element Value IsVisited Position deriving (Eq, Show)
instance Monoid Element where
mempty = Element 0 Visited (0, 0)
mempty = Element 0 Unvisited (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
v -> if v == v1
then Element v1 Visited p1
else Element v2 Visited p2
getMin :: Value -> Value -> Value
getMin v1 0 = v1
@ -25,11 +26,11 @@ getMin v1 v2 = min v1 v2
getUnvisitedNeighbours :: Matrix Element -> Element -> [Element]
getUnvisitedNeighbours m (Element _ _ (i, j)) =
filter (\(Element _ isVisited _) -> isVisited == Visited)
(catMaybes [safeGet (i - 1) j m
,safeGet (i + 1) j m
,safeGet i (j - 1) m
,safeGet i (j + 1) m])
filter (\(Element _ isVisited _) -> isVisited == Unvisited)
(catMaybes [safeGet (i - 1) j m,
safeGet (i + 1) j m,
safeGet i (j - 1) m,
safeGet i (j + 1) m])
getNeighbourDistances :: Element -> [Element] -> [Element]
getNeighbourDistances e = map (addValue e)
@ -39,10 +40,6 @@ dijkstraMatrix :: Matrix Element -> Element
dijkstraMatrix m =
let neighbourDistanceMatrix = fmap (\e -> getNeighbourDistances e (getUnvisitedNeighbours m e)) m
in fold (fmap fold neighbourDistanceMatrix)
-- for unvisited element in matrix
-- for unvisited neighbour of element
-- if element value + neighbour value < min then set as min
-- return min; default is (0, (0, 0))
nextMatrix :: Matrix Element -> Matrix Element
nextMatrix m =
@ -52,6 +49,14 @@ nextMatrix m =
Just next -> next
Nothing -> m
setInitial :: Matrix Element -> Matrix Element
setInitial m =
let Element v _ _ = m ! (1, 1)
maybeMatrix = safeSet (Element v Visited (1, 1)) (1, 1) m
in case maybeMatrix of
Just mm -> mm
Nothing -> m
initElement :: Matrix Integer -> Position -> Element
initElement m p =
let value = m ! p
@ -61,10 +66,18 @@ toElementMatrix :: Matrix Integer -> Matrix Element
toElementMatrix m =
matrix (nrows m) (ncols m) (initElement m)
findShortestPathLength :: Matrix Element -> Value
findShortestPathLength m =
let Element v isVisited _ = m ! (nrows m, ncols m)
in case isVisited of
Visited -> v
Unvisited -> findShortestPathLength $ nextMatrix m
main :: IO ()
main = do
contents <- readFile "p081_matrix.txt"
contents <- readFile "p083_matrix.txt"
let listsMatrix = map (map read . (splitOn ",")) $ lines contents :: [[Integer]]
valueMatrix = fromLists listsMatrix
mtrx = toElementMatrix valueMatrix
print $ nextMatrix mtrx
unvisitedMatrix = toElementMatrix valueMatrix
mtrx = setInitial unvisitedMatrix
print $ findShortestPathLength mtrx

BIN
83.o

Binary file not shown.

5
p083_test.txt Normal file
View File

@ -0,0 +1,5 @@
131,673,234,103,18
201,96,342,965,150
630,803,746,422,111
537,699,497,121,956
805,732,524,37,331