add exitpoints

This commit is contained in:
opfez 2021-05-29 00:13:50 +02:00
parent 04098a9579
commit 2ad0de9cd2
4 changed files with 25 additions and 9 deletions

View File

@ -3,22 +3,26 @@ module Element where
data Element = Element { value :: Integer
, xPos :: Int
, yPos :: Int
, state :: Bool
} deriving (Eq, Show)
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 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 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 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 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 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
View File

@ -19,12 +19,19 @@ run :: String -> IO ()
run inputFile = do
input <- readFile inputFile
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 f es = do
interpretLoop f es =
let getDeadElement (e:es)
| state e == False = e
| otherwise = getDeadElement es
in do
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
case a of
'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 Conditional, singleton . zeroBranch)
, (Entrypoint, singleton . spitOut East f)
, (Exitpoint, singleton . kill)
]
dupFunc :: Element -> [Element]
dupFunc elem = [ spitOutNth 0 East f elem

View File

@ -34,6 +34,7 @@ data SpecialKind = Duplicate | Conditional
deriving (Eq)
data Tile = Entrypoint
| Exitpoint
| Track Direction
| Modifier ModifierKind
| Special SpecialKind
@ -49,6 +50,7 @@ type Factory = [[Tile]]
charToTile :: Char -> Tile
charToTile c = case c of
'E' -> Entrypoint
'X' -> Exitpoint
'>' -> Track East
'<' -> Track West
'v' -> Track South
@ -62,6 +64,7 @@ charToTile c = case c of
tileToChar :: Tile -> Char
tileToChar t = case t of
Entrypoint -> 'E'
Exitpoint -> 'X'
Track East -> '>'
Track West -> '<'
Track South -> 'v'

1
examples/exit.fac Normal file
View File

@ -0,0 +1 @@
E>+>X