add conditionals

This commit is contained in:
opfez 2021-05-28 13:44:02 +02:00
parent a6ad446f59
commit 7b29bcb20a
5 changed files with 65 additions and 65 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.o *.hi

View File

@ -39,3 +39,32 @@ showFactory f = map (map (head . show)) f
printFactory :: Factory -> [Element] -> IO () printFactory :: Factory -> [Element] -> IO ()
printFactory f es = mapM_ (print . snd) lines printFactory f es = mapM_ (print . snd) lines
where lines = putElements es $ indexCharFactory f where lines = putElements es $ indexCharFactory f
-- AAAAAAAA
spitOutEast :: Factory -> Element -> Element
spitOutEast f elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
spitOutSouth :: Factory -> Element -> Element
spitOutSouth f elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
spitOutWest :: Factory -> Element -> Element
spitOutWest f elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
spitOutNorth :: Factory -> Element -> Element
spitOutNorth f elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem

68
Main.hs
View File

@ -1,9 +1,5 @@
module Main where module Main where
-- TODO:
-- arithmetic
-- I/O
import Data.List import Data.List
import System.Environment import System.Environment
import System.Exit import System.Exit
@ -15,18 +11,15 @@ import Dirty
main :: IO () main :: IO ()
main = do main = do
args <- getArgs args <- getArgs
if length args < 1 if null args
then die "No filename provided." then die "No filename provided."
else run $ args !! 0 else run $ head args
run :: String -> IO () 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 [ Element interpretLoop internalFactory [uncurry (Element 0) $ entryPointLocation internalFactory]
0
(fst $ entryPointLocation internalFactory)
(snd $ entryPointLocation internalFactory)]
interpretLoop :: Factory -> [Element] -> IO () interpretLoop :: Factory -> [Element] -> IO ()
interpretLoop f es = do interpretLoop f es = do
@ -73,34 +66,9 @@ spitOut dir f elem = case dir of
West -> spitOutWest f elem West -> spitOutWest f elem
North -> spitOutNorth f elem North -> spitOutNorth f elem
-- AAAAAAAA spitOutNth :: Int -> Direction -> Factory -> Element -> Element
spitOutEast :: Factory -> Element -> Element spitOutNth 0 dir f elem = spitOut dir f elem
spitOutEast f elem spitOutNth n dir f elem = spitOutNth (n - 1) (succ dir) f elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
spitOutSouth :: Factory -> Element -> Element
spitOutSouth f elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
spitOutWest :: Factory -> Element -> Element
spitOutWest f elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
spitOutNorth :: Factory -> Element -> Element
spitOutNorth f elem
| (f !! (yPos elem - 1)) !! xPos elem == Track North = decYPos elem
| (f !! yPos elem) !! (xPos elem + 1) == Track East = incXPos elem
| (f !! (yPos elem + 1)) !! xPos elem == Track South = incYPos elem
| (f !! yPos elem) !! (xPos elem - 1) == Track West = decXPos elem
duplicate :: Element -> [Element] duplicate :: Element -> [Element]
duplicate e = [e, e] duplicate e = [e, e]
@ -109,19 +77,21 @@ update :: Factory -> Element -> [Element]
update f e = if oobCheck f e then error "Element out of bounds check failed!" update f e = if oobCheck f e then error "Element out of bounds check failed!"
else tileActionMap t tileActions e else tileActionMap t tileActions e
where t = (f !! yPos e) !! xPos e where t = (f !! yPos e) !! xPos e
tileActions = [ (Track East, singleton . incXPos) tileActions = [ (Track East, singleton . incXPos)
, (Track West, singleton . decXPos) , (Track West, singleton . decXPos)
, (Track South, singleton . incYPos) , (Track South, singleton . incYPos)
, (Track North, singleton . decYPos) , (Track North, singleton . decYPos)
, (Modifier Increment, singleton . incVal . spitOut East f) , (Modifier Increment, singleton . incVal . spitOut East f)
, (Modifier Decrement, singleton . decVal . spitOut East f) , (Modifier Decrement, singleton . decVal . spitOut East f)
, (Special Duplicate, dupFunc) , (Special Duplicate, dupFunc)
, (Entrypoint, singleton . spitOut East f) , (Special Conditional, singleton . zeroBranch)
, (Entrypoint, singleton . spitOut East f)
] ]
dupFunc :: Element -> [Element] dupFunc :: Element -> [Element]
dupFunc elem = [ spitOut East f elem dupFunc elem = [ spitOutNth 0 East f elem
, spitOut South f elem , spitOutNth 1 East f elem
] ]
zeroBranch elem = if value elem == 0 then spitOut East f elem else spitOut South f elem
stepInterpret :: Factory -> [Element] -> [Element] stepInterpret :: Factory -> [Element] -> [Element]
stepInterpret f = concat . map (update f) stepInterpret f = concatMap (update f)

View File

@ -30,7 +30,7 @@ instance Enum Direction where
data ModifierKind = Increment | Decrement data ModifierKind = Increment | Decrement
deriving (Eq) deriving (Eq)
data SpecialKind = Duplicate data SpecialKind = Duplicate | Conditional
deriving (Eq) deriving (Eq)
data Tile = Entrypoint data Tile = Entrypoint
@ -56,16 +56,18 @@ charToTile c = case c of
'+' -> Modifier Increment '+' -> Modifier Increment
'-' -> Modifier Decrement '-' -> Modifier Decrement
'D' -> Special Duplicate 'D' -> Special Duplicate
'?' -> Special Conditional
_ -> Ignored _ -> Ignored
tileToChar :: Tile -> Char tileToChar :: Tile -> Char
tileToChar t = case t of tileToChar t = case t of
Entrypoint -> 'E' Entrypoint -> 'E'
Track East -> '>' Track East -> '>'
Track West -> '<' Track West -> '<'
Track South -> 'v' Track South -> 'v'
Track North -> '^' Track North -> '^'
Modifier Increment -> '+' Modifier Increment -> '+'
Modifier Decrement -> '-' Modifier Decrement -> '-'
Special Duplicate -> 'D' Special Duplicate -> 'D'
_ -> ' ' Special Conditional -> '?'
_ -> ' '

View File

@ -1,6 +1,4 @@
E >>v | E>?>+ |
v ^ + | ^ v v |
>>^ v | ^<- v |
^ D>v | ^<<< |
^ v v |
^<<<< |