Skip to content

Instantly share code, notes, and snippets.

@louy2
Created February 21, 2019 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louy2/3e2d80949bc6b8c1ac937f630053b153 to your computer and use it in GitHub Desktop.
Save louy2/3e2d80949bc6b8c1ac937f630053b153 to your computer and use it in GitHub Desktop.
問題「隔離された街のゲート」 | エンジニアが死滅シタ世界 〜アンドロイドとふたりぼっちで生きろ〜
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