Created
December 2, 2017 09:34
-
-
Save kei-q/5dbdd9f96aa84f048828f7582d65d49e to your computer and use it in GitHub Desktop.
どう書くE20の解答例 問題:http://nabetani.sakura.ne.jp/hena/orde20maze/
This file contains hidden or 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
module Lib where | |
import Data.List.Split | |
import Data.List | |
solve :: String -> String | |
solve = show . solve' | |
solve' :: String -> Int | |
solve' [start,goal] = walk [start] [] 0 | |
where | |
walk s r n | goal `elem` s = n | |
walk s r n = walk (next s r) (r++s) (n+1) | |
next s r = [b| (a,b) <- maze, a `elem` s, b `notElem` r] | |
solve' _ = error "unexpected input" | |
maze, field, walls :: [(Char,Char)] | |
maze = t ++ revt | |
where | |
revt = map (\(a,b) -> (b,a)) t | |
t = field \\ walls | |
field = hol ++ ver | |
where | |
list = chunksOf 6 $ ['0'..'9'] ++ ['A'..'Z'] | |
hol = concatMap (\l -> zip l (tail l)) list | |
ver = concatMap (\(x,y) -> zip x y) $ zip list (tail list) | |
walls = map (\[a,b] -> (a,b)) $ chunksOf 2 "12284A67AB7D8EAGBHDEEFGHDJFLIJMNKQLROPRSOUQWTZWXXY" | |
-- 以下説明用 | |
walk :: [Char] -> Int | |
walk [start,goal] = length $ walk' start goal | |
walk _ = error "walk" | |
walk' :: Char -> Char -> [([Char], Int, [Char])] | |
walk' start goal = takeWhile (\(starts,_,_) -> goal `notElem` starts) $ iterate step ([start],0,[]) | |
step :: ([Char], Int, [Char]) -> ([Char], Int, [Char]) | |
step (starts,n,walked) = (next,(n+1),(starts++walked)) | |
where next = [to| (from,to) <- maze, from `elem` starts, to `notElem` walked] | |
printList :: (Show a2, Show a1, Show a, Foldable t) => t (a, a1, a2) -> IO () | |
printList xs = mapM_ (\(starts,steps,walked) -> putStrLn $ "starts: " ++ show starts ++ "\tsteps: " ++ show steps ++ "\twalked: " ++ show walked) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment