add exitpoints
This commit is contained in:
parent
04098a9579
commit
2ad0de9cd2
16
Element.hs
16
Element.hs
|
@ -3,22 +3,26 @@ module Element where
|
||||||
data Element = Element { value :: Integer
|
data Element = Element { value :: Integer
|
||||||
, xPos :: Int
|
, xPos :: Int
|
||||||
, yPos :: Int
|
, yPos :: Int
|
||||||
|
, state :: Bool
|
||||||
} deriving (Eq, Show)
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
decVal :: Element -> Element
|
decVal :: Element -> Element
|
||||||
decVal e = Element (value e - 1) (xPos e) (yPos e)
|
decVal (Element v x y s) = Element (v - 1) x y s
|
||||||
|
|
||||||
incVal :: Element -> Element
|
incVal :: Element -> Element
|
||||||
incVal e = Element (value e + 1) (xPos e) (yPos e)
|
incVal (Element v x y s) = Element (v + 1) x y s
|
||||||
|
|
||||||
incXPos :: Element -> Element
|
incXPos :: Element -> Element
|
||||||
incXPos e = Element (value e) (xPos e + 1) (yPos e)
|
incXPos (Element v x y s) = Element v (x + 1) y s
|
||||||
|
|
||||||
decXPos :: Element -> Element
|
decXPos :: Element -> Element
|
||||||
decXPos e = Element (value e) (xPos e - 1) (yPos e)
|
decXPos (Element v x y s) = Element v (x - 1) y s
|
||||||
|
|
||||||
incYPos :: Element -> Element
|
incYPos :: Element -> Element
|
||||||
incYPos e = Element (value e) (xPos e) (yPos e + 1)
|
incYPos (Element v x y s) = Element v x (y + 1) s
|
||||||
|
|
||||||
decYPos :: Element -> Element
|
decYPos :: Element -> Element
|
||||||
decYPos e = Element (value e) (xPos e) (yPos e - 1)
|
decYPos (Element v x y s) = Element v x (y - 1) s
|
||||||
|
|
||||||
|
kill :: Element -> Element
|
||||||
|
kill (Element v x y _) = Element v x y False
|
||||||
|
|
14
Main.hs
14
Main.hs
|
@ -19,12 +19,19 @@ run :: String -> IO ()
|
||||||
run inputFile = do
|
run inputFile = do
|
||||||
input <- readFile inputFile
|
input <- readFile inputFile
|
||||||
let internalFactory = stringsToFactory $ lines input
|
let internalFactory = stringsToFactory $ lines input
|
||||||
interpretLoop internalFactory [uncurry (Element 0) $ entryPointLocation internalFactory]
|
let el = entryPointLocation internalFactory
|
||||||
|
interpretLoop internalFactory [Element 0 (fst el) (snd el) True]
|
||||||
|
|
||||||
interpretLoop :: Factory -> [Element] -> IO ()
|
interpretLoop :: Factory -> [Element] -> IO ()
|
||||||
interpretLoop f es = do
|
interpretLoop f es =
|
||||||
|
let getDeadElement (e:es)
|
||||||
|
| state e == False = e
|
||||||
|
| otherwise = getDeadElement es
|
||||||
|
in do
|
||||||
let elements = stepInterpret f es
|
let elements = stepInterpret f es
|
||||||
printFactory f elements
|
if any (\e -> state e == False) es
|
||||||
|
then exitWith (ExitFailure $ fromInteger $ value $ getDeadElement es)
|
||||||
|
else printFactory f elements
|
||||||
a <- getChar
|
a <- getChar
|
||||||
case a of
|
case a of
|
||||||
'q' -> return ()
|
'q' -> return ()
|
||||||
|
@ -89,6 +96,7 @@ update f e = if oobCheck f e then error "Element out of bounds check failed!"
|
||||||
, (Special Duplicate, dupFunc)
|
, (Special Duplicate, dupFunc)
|
||||||
, (Special Conditional, singleton . zeroBranch)
|
, (Special Conditional, singleton . zeroBranch)
|
||||||
, (Entrypoint, singleton . spitOut East f)
|
, (Entrypoint, singleton . spitOut East f)
|
||||||
|
, (Exitpoint, singleton . kill)
|
||||||
]
|
]
|
||||||
dupFunc :: Element -> [Element]
|
dupFunc :: Element -> [Element]
|
||||||
dupFunc elem = [ spitOutNth 0 East f elem
|
dupFunc elem = [ spitOutNth 0 East f elem
|
||||||
|
|
3
Types.hs
3
Types.hs
|
@ -34,6 +34,7 @@ data SpecialKind = Duplicate | Conditional
|
||||||
deriving (Eq)
|
deriving (Eq)
|
||||||
|
|
||||||
data Tile = Entrypoint
|
data Tile = Entrypoint
|
||||||
|
| Exitpoint
|
||||||
| Track Direction
|
| Track Direction
|
||||||
| Modifier ModifierKind
|
| Modifier ModifierKind
|
||||||
| Special SpecialKind
|
| Special SpecialKind
|
||||||
|
@ -49,6 +50,7 @@ type Factory = [[Tile]]
|
||||||
charToTile :: Char -> Tile
|
charToTile :: Char -> Tile
|
||||||
charToTile c = case c of
|
charToTile c = case c of
|
||||||
'E' -> Entrypoint
|
'E' -> Entrypoint
|
||||||
|
'X' -> Exitpoint
|
||||||
'>' -> Track East
|
'>' -> Track East
|
||||||
'<' -> Track West
|
'<' -> Track West
|
||||||
'v' -> Track South
|
'v' -> Track South
|
||||||
|
@ -62,6 +64,7 @@ charToTile c = case c of
|
||||||
tileToChar :: Tile -> Char
|
tileToChar :: Tile -> Char
|
||||||
tileToChar t = case t of
|
tileToChar t = case t of
|
||||||
Entrypoint -> 'E'
|
Entrypoint -> 'E'
|
||||||
|
Exitpoint -> 'X'
|
||||||
Track East -> '>'
|
Track East -> '>'
|
||||||
Track West -> '<'
|
Track West -> '<'
|
||||||
Track South -> 'v'
|
Track South -> 'v'
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
E>+>X
|
Loading…
Reference in New Issue