Skip to content

Commit

Permalink
Allow lines of different lengths
Browse files Browse the repository at this point in the history
It thus respects the original challenge
  • Loading branch information
Zigazou committed Sep 24, 2015
1 parent 1af98f6 commit d1b15df
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
19 changes: 12 additions & 7 deletions src/Grid/Parse.hs
Expand Up @@ -12,7 +12,6 @@ module Grid.Parse (toGrid) where
import Data.Array (listArray)
import Data.List (transpose)
import Data.Maybe (fromJust)
import Control.Arrow ((&&&))

import Cell (Cell (Start), toCell)

Expand All @@ -24,13 +23,19 @@ Verify a `String` contains a valid problem.
validateGrid :: String -> Either String String
validateGrid s
| null s = Left "Empty string"
| any ((/= width ls) . length) ls = Left "Rows with different lengths"
| startCounts s == 0 = Left "No start point"
| startCounts s > 1 = Left "More than one start point"
| otherwise = Right s
where ls = lines s
width = length . head
startCounts = length . filter (== 'S')
where startCounts = length . filter (== 'S')

{- |
Limit a `String` at a specific length. If the source `String` is less than the
targetted length, it is padded with spaces.
-}
limit :: Int -> String -> String
limit i s | l >= i = take i s
| otherwise = s ++ replicate (i - l) ' '
where l = length s

{- |
Convert a `String` into a `Grid`.
Expand All @@ -39,7 +44,7 @@ toGrid :: String -> Either String Grid
toGrid s = validateGrid s >> return (Grid start cells)
where
ls = lines s
(width, height) = (length . head &&& length) ls
(width, height) = (maximum (length <$> ls), length ls)
cells = toCell <$> listArray ((0, 0), (width - 1, height - 1))
(concat $ transpose ls)
(concat $ transpose (limit width <$> ls))
start = fromJust $ arraySearch cells Start
2 changes: 1 addition & 1 deletion test/valid01.txt
@@ -1,3 +1,3 @@
d s<+S+--V
||| Q
-++
-++
6 changes: 3 additions & 3 deletions test/valid03.txt
@@ -1,4 +1,4 @@
S-----+
| +-^
+---+->B
S-----+
| +-^
+---+->B
+---^
12 changes: 6 additions & 6 deletions test/valid04.txt
@@ -1,9 +1,9 @@
+---+
| |
+---S---+
| |
+---+
| |
+---S---+
| |
+++++>A
+++++>B
+++++>C
|
>Y
|
>Y
20 changes: 10 additions & 10 deletions test/valid05.txt
@@ -1,11 +1,11 @@
+-------+ +-------+--+-------+ +------+
| | | +-+ | | | | ++
| +----+ | | | | | +----+ | +-+ |
| | | | | | | | | | | |
| +--+ | +-+ ++ | +--+ | | | |
+-------+ +-------+--+-------+ +------+
| | | +-+ | | | | ++
| +----+ | | | | | +----+ | +-+ |
| | | | | | | | | | | |
| +--+ | +-+ ++ | +--+ | | | |
S----+ | | +--+ | | | | | +--->B
| +--+ | +-+ ++ | +--+ | | | |
| | | | | | | | | | | |
| | | | | | | +----+ | +-+ |
| | | | | | | | | ++
+--+-------+--+ +--+ +-------+--+------+
| +--+ | +-+ ++ | +--+ | | | |
| | | | | | | | | | | |
| | | | | | | +----+ | +-+ |
| | | | | | | | | ++
+--+-------+--+ +--+ +-------+--+------+
10 changes: 5 additions & 5 deletions test/valid06.txt
@@ -1,7 +1,7 @@
+--S--+
+--S--+
O<| | |>O
+-+++-+
+-+++-+
+-+++-+
+-+++-+
+-+++-+
+-+++-+
O<| | |>O
+->E<-+
+->E<-+

0 comments on commit d1b15df

Please sign in to comment.