Created
September 6, 2017 08:31
-
-
Save rumblesan/bea654cbd3c6d21f1f4bcc7050060570 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rumblesan.marsrover | |
import cats._ | |
import cats.instances.list._ | |
import cats.instances.either._ | |
import Commands.Command | |
import Headings.Heading | |
case class Mission(startX: Int, startY: Int, startHeading: Heading, commands: List[Command]) | |
object Mission { | |
sealed trait FailedMissionResult | |
case class LostBot(bot: Bot) extends FailedMissionResult | |
type MissionResult[Result] = Either[FailedMissionResult, Result] | |
def run(mission: Mission, planet: Planet): MissionResult[Bot] = { | |
val startBot = Bot(mission.startX, mission.startY, mission.startHeading) | |
Foldable[List].foldM[MissionResult, Command, Bot](mission.commands, startBot)((bot, command) => { | |
val newBot = Bot.command(bot, command) | |
if (Planet.validPosition(planet, newBot.xPos, newBot.yPos)) { | |
Right(newBot) | |
} else if (Planet.checkMarker(planet, bot.xPos, bot.yPos)) { | |
// Skip command | |
Right(bot) | |
} else { | |
// Return last valid bot | |
Left(LostBot(bot)) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment