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

View File

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

View File

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