Skip to content

Instantly share code, notes, and snippets.

@louy2 louy2/gate.hs
Created Feb 21, 2019

Embed
What would you like to do?
問題「隔離された街のゲート」 | エンジニアが死滅シタ世界 〜アンドロイドとふたりぼっちで生きろ〜
decode :: String -> [Int]
decode "U" = [ 0, 1]
decode "D" = [ 0, -1]
decode "L" = [-1, 0]
decode "R" = [ 1, 0]
decode _ = [ 0, 0]
(.+) :: Num a => [a] -> [a] -> [a]
(.+) = zipWith (+)
f :: String -> String
f input = show $
let
[h, w] = take 2 . fmap read . words . head . lines $ input
positions = scanl (.+) [0,0] . fmap decode . tail . lines $ input
validate [x, y] = x >= 0 && y >= 0 && x < w && y < h
validate _ = False
in all validate positions
where
show True = "valid"
show False = "invalid"
main :: IO ()
main = interact f
testStr1 = "3 3 5\nU\nR\nD\nR\nL\n"
testStr2 = "4 4 7\nU\nU\nR\nR\nR\nR\nD\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.