Created
February 21, 2019 07:07
-
-
Save louy2/3e2d80949bc6b8c1ac937f630053b153 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
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