Skip to content

Commit 0dd0bc3

Browse files
committed
Add gen to solve
1 parent 747c1a3 commit 0dd0bc3

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

app/Main.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
module Main where
22

3+
import System.Random
4+
35
import Structures
46
import Algorithms
57

68

79
main = do
10+
let rand = randomIO :: IO Int
11+
seed <- rand
12+
let gen = mkStdGen seed
13+
814
let m = generate
9-
let sol = solve m
15+
let sol = solve m gen
1016
print sol

hidato.cabal

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 1adf0231ee1c9c3fa35bb688d8f64a8891b7f4ed5a43a7f6bad46e2a7c24f6c1
7+
-- hash: 8990a2fc1a1f4fa5cf6bcf0dc27fc8585348d19663dd6d611e9d1f7fb613dc4c
88

99
name: hidato
1010
version: 0.1.0.0
@@ -35,6 +35,7 @@ library
3535
build-depends:
3636
base >=4.7 && <5
3737
, containers
38+
, random
3839
default-language: Haskell2010
3940

4041
executable hidato-exe
@@ -48,6 +49,7 @@ executable hidato-exe
4849
base >=4.7 && <5
4950
, containers
5051
, hidato
52+
, random
5153
default-language: Haskell2010
5254

5355
test-suite hidato-test
@@ -62,4 +64,5 @@ test-suite hidato-test
6264
base >=4.7 && <5
6365
, containers
6466
, hidato
67+
, random
6568
default-language: Haskell2010

package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ description: Please see the README on GitHub at <https://https://github.
2121
dependencies:
2222
- base >= 4.7 && < 5
2323
- containers
24+
- random
2425

2526
library:
2627
source-dirs: src

src/Algorithms.hs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Algorithms
44
, solveAll
55
) where
66

7+
import System.Random
78
import Structures
89
import Data.Set (Set, lookupMin, lookupMax)
910
import qualified Data.Set as Set
@@ -14,10 +15,10 @@ import qualified Data.Map as Map
1415
generate :: Matrix
1516
generate = read "{. x x x x x x x x \n . 8 x x x x x x x \n . . 11 x x x x x x \n 29 . 10 . x x x x x \n 30 . . . . x x x x \n . 31 1 38 . . x x x \n . 32 . . 39 41 . x x \n . . . 22 . . 42 . x \n . . . . . . . 44 45}" :: Matrix
1617

17-
stepMatrix :: Int -> Matrix -> Cell -> Map Int Cell -> [(Matrix, Cell)]
18-
stepMatrix step m@(Matrix rs cs ma) prevCell map = if Map.notMember step map
18+
stepMatrix :: Int -> Matrix -> Cell -> Map Int Cell -> StdGen -> [(Matrix, Cell)]
19+
stepMatrix step m@(Matrix rs cs ma) prevCell map gen = if Map.notMember step map
1920
then [
20-
(Matrix rs cs (Set.insert cell ma), cell) | cell <- getAdjacents prevCell rs cs step,
21+
(Matrix rs cs (Set.insert cell ma), cell) | cell <- getAdjacents prevCell rs cs step gen,
2122
let actCell = Set.elemAt (Set.findIndex cell $ ma) ma,
2223
value actCell == 0
2324
]
@@ -35,16 +36,16 @@ buildMap ma@(Matrix r c m) = Set.foldl (\acc cell -> Map.insert (value cell) cel
3536
-- | matrix <- stepMatrix step actualMatrix]
3637
-- in solveRecursiveBFS (queue ++ toAdd)
3738

38-
solveRecursiveDFS :: Matrix -> Int -> Cell -> Int -> Map Int Cell -> [Matrix]
39-
solveRecursiveDFS actualMatrix step prevCell obs map
39+
solveRecursiveDFS :: Matrix -> Int -> Cell -> Int -> Map Int Cell -> StdGen -> [Matrix]
40+
solveRecursiveDFS actualMatrix step prevCell obs map gen
4041
| step == obs + 1 = [actualMatrix]
41-
| otherwise = let toAdd = stepMatrix step actualMatrix prevCell map
42-
in concat [ solveRecursiveDFS matrix (step + 1) prevCell obs map | (matrix, prevCell) <- toAdd ]
42+
| otherwise = let toAdd = stepMatrix step actualMatrix prevCell map gen
43+
in concat [ solveRecursiveDFS matrix (step + 1) prevCell obs map gen | (matrix, prevCell) <- toAdd ]
4344

44-
solveAll :: Matrix -> [Matrix]
45-
solveAll m = solveRecursiveDFS m 1 (Cell 0 0 0) ((rows m) * (columns m) - countObstacles m) $ buildMap m
45+
solveAll :: Matrix -> StdGen -> [Matrix]
46+
solveAll m gen = solveRecursiveDFS m 1 (Cell 0 0 0) ((rows m) * (columns m) - countObstacles m) (buildMap m) gen
4647

47-
solve :: Matrix -> Matrix
48-
solve m = let solves = solveAll m
48+
solve :: Matrix -> StdGen -> Matrix
49+
solve m gen = let solves = solveAll m gen
4950
in case solves of [] -> error "Solve not found"
5051
(x: xs) -> x

src/Structures.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Data.Char (isDigit)
1616
import Data.List
1717
import Data.Set (Set, lookupMin, lookupMax)
1818
import qualified Data.Set as Set
19+
import System.Random
1920

2021
data Cell = Cell { row :: Int
2122
, column :: Int
@@ -50,8 +51,8 @@ instance Read Cell where
5051
[(Cell row column value, rest7) |
5152
opar == '(' && comma1 == comma2 && comma2 == ',' && cpar == ')']
5253

53-
getAdjacents :: Cell -> Int -> Int -> Int -> [Cell]
54-
getAdjacents (Cell r c v) rs cs s = [Cell nr nc s | dr <- [-1, 0, 1], dc <- [-1, 0, 1], let (nr, nc) = (r + dr, c + dc), nr > 0, nr <= rs, nc > 0, nc <= cs]
54+
getAdjacents :: Cell -> Int -> Int -> Int -> StdGen -> [Cell]
55+
getAdjacents (Cell r c v) rs cs s gen = [Cell nr nc s | dr <- [-1, 0, 1], dc <- [-1, 0, 1], let (nr, nc) = (r + dr, c + dc), nr > 0, nr <= rs, nc > 0, nc <= cs]
5556

5657
isAdjacent :: Cell -> Cell -> Bool
5758
isAdjacent (Cell f1 c1 v1) (Cell f2 c2 v2)

0 commit comments

Comments
 (0)