Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active February 13, 2019 20:37
Show Gist options
  • Save Superstar64/f6a8a2bcfedfc1663aa428c09a2f8fa0 to your computer and use it in GitHub Desktop.
Save Superstar64/f6a8a2bcfedfc1663aa428c09a2f8fa0 to your computer and use it in GitHub Desktop.
2016 ACM ICPC Regionals Division 2 Problem 2
{-
Copyright (C) Freddy A Cubas "Superstar64" 2019
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
-}
--https://ser.cs.fit.edu/ser2016//problems/division_2/SER2016%20Problem%20Set%20-%20Division2.pdf
--Problem 2
--all type annotions are optional
import Prelude hiding(Left,Right)
import Data.List (elemIndex,findIndex,inits)
import Data.Maybe (isJust,fromJust,catMaybes)
data Direction = Left | Right | Up | Down deriving (Eq,Show)
data Box = Empty | Robot | Block | Exit deriving (Eq,Show)
replace :: [a] -> Int -> a -> [a]
replace list index element = let (h,_:t) = splitAt index list in h ++ (element : t)
remove :: [a] -> Int -> [a]
remove list index = let (h,_:t) = splitAt index list in h ++ t
insert :: [a] -> Int -> a -> [a]
insert list index element = let (h,t) = splitAt index list in h ++ (element : t)
findBox :: Eq a => a -> [[a]] -> (Int, Int)
findBox box grid = (x,y) where
list = fmap (elemIndex box) grid
x = head $ catMaybes $ list
y = fromJust $ findIndex isJust list
findRobot :: [[Box]] -> (Int, Int)
findRobot = findBox Robot
findExit :: [[Box]] -> (Int, Int)
findExit = findBox Exit
applyDiretion :: (Int, Int) -> Direction -> (Int, Int)
applyDiretion (x,y) Left = (x-1,y)
applyDiretion (x,y) Right = (x+1,y)
applyDiretion (x,y) Up = (x,y-1)
applyDiretion (x,y) Down = (x,y+1)
bound :: [[a]] -> (Int, Int) -> (Int, Int)
bound grid (x,y) = (fx x,fy y) where
width = head $ fmap (length) grid
height = length grid
fx = max 0 . min (width - 1)
fy = max 0 . min (height - 1)
index :: (Int, Int) -> [[a]] -> a
index (x,y) grid = grid !! y !! x
set :: (Int, Int) -> a -> [[a]] -> [[a]]
set (x,y) element grid = replace grid y (replace (grid !! y) x element)
move :: Direction -> [[Box]] -> [[Box]]
move direction grid = if valid then grid' else grid where
(x,y) = findRobot grid
(tx,ty) = bound grid $ applyDiretion (x,y) direction
target = index (tx,ty) grid
valid = target /= Block
grid' = set (tx,ty) Robot $ set(x,y) Empty grid
applyInstructions :: [[Box]] -> [Direction] -> [[Box]]
applyInstructions grid instructions = foldr (flip (.)) id (fmap move instructions) grid
nextToExit :: [[Box]] -> Bool
nextToExit grid = any (== findRobot grid) $ fmap (applyDiretion $ findExit grid) [Left,Right,Up,Down]
hitsExit :: [[Box]] -> [Direction] -> Bool
hitsExit grid instructions = any nextToExit $ fmap (applyInstructions grid) $ inits instructions
searchCases :: [Direction] -> [[Direction]]
searchCases instructions = subCases ++ plusCases where
subCases = [remove instructions x | x <- [0.. (length instructions) - 1] ]
plusCases = [insert instructions x | x <- [0.. length instructions] ] <*> [Left,Right,Up,Down]
searchImpl :: [Direction] -> [[[Direction]]]
searchImpl instructions = next : (next >>= searchImpl) where
next = searchCases instructions
search :: [Direction] -> [[[Direction]]]
search instructions = [instructions] : searchImpl instructions
convertGrid :: [[Char]] -> [[Box]]
convertGrid = fmap $ fmap convertBox
convertBox :: Char -> Box
convertBox '.' = Empty
convertBox 'R' = Robot
convertBox '#' = Block
convertBox 'E' = Exit
convertInstruction :: Char -> Direction
convertInstruction 'L' = Left
convertInstruction 'R' = Right
convertInstruction 'D' = Down
convertInstruction 'U' = Up
main :: IO ()
main = do
[rows,columns] <- fmap (fmap read .words) getLine
grid <- fmap convertGrid $ sequence $ replicate rows getLine
instructions <- fmap (fmap convertInstruction) getLine
print $ fst $ head $ filter snd $ zip [0..] $ fmap (any (hitsExit grid)) $ search instructions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment