Skip to content

Instantly share code, notes, and snippets.

@kei-q
Last active June 15, 2016 11:11
Show Gist options
  • Save kei-q/0f16493d115158de6ac793db310dc46e to your computer and use it in GitHub Desktop.
Save kei-q/0f16493d115158de6ac793db310dc46e to your computer and use it in GitHub Desktop.
E04の別解

解法について

それぞれの横線があるところで、そこにいる人が場所を入れ替わる。 また両名ともその横線を通っているはず。

*Solver> swap ['A'..'H'] '2'
("ACBDEFGH","BC")

これを全部の横線について確認すると、出会ったペアと、最終的にどこに登頂するかがわかる。

*Solver> mapAccumL swap ['A'..'H'] (reverse "2512")
("CBADFEGH",["BC","AC","EF","AB"])

岩は誰かの登るルートと同じはずで、岩の位置に誰が登頂したかでどの出会いが死を意味するかがわかる。 今回は岩Cの位置にAさんが着ているので、出会いの中でAさんに出会う予定の人が死。

module Solver(solve) where
import Data.List
import Data.List.Split
solve input = climbers \\ (if null deadend then s else deadend)
where
[rs,s] = splitOn ":" input
climbers = ['A'..'H']
(cs,ret) = mapAccumL swap climbers (reverse rs)
(Just block) = lookup (head s) $ zip climbers cs
deadend = concat $ filter (elem block) ret
swap [a,b,c,d,e,f,g,h] '1' = ([b,a,c,d,e,f,g,h],[a,b])
swap [a,b,c,d,e,f,g,h] '2' = ([a,c,b,d,e,f,g,h],[b,c])
swap [a,b,c,d,e,f,g,h] '3' = ([a,b,d,c,e,f,g,h],[c,d])
swap [a,b,c,d,e,f,g,h] '4' = ([a,b,c,e,d,f,g,h],[d,e])
swap [a,b,c,d,e,f,g,h] '5' = ([a,b,c,d,f,e,g,h],[e,f])
swap [a,b,c,d,e,f,g,h] '6' = ([a,b,c,d,e,g,f,h],[f,g])
swap [a,b,c,d,e,f,g,h] '7' = ([a,b,c,d,e,f,h,g],[g,h])
swap [a,b,c,d,e,f,g,h] '8' = ([h,b,c,d,e,f,g,a],[h,a])
swap _ _ = error "oops!"
module Solver(solve) where
import Data.List
import Data.List.Split
solve input = climbers \\ (if null deadend then s else deadend)
where
[rs,s] = splitOn ":" input
climbers = ['A'..'H']
(cs,ret) = mapAccumL swap climbers (reverse rs)
(Just block) = lookup (head s) $ zip climbers cs
deadend = concat $ filter (elem block) ret
swap cs r = swap' cs n
where
n = read [r] - 1
swap' (a:b:xs) 0 = let (x,y) = splitAt (length cs - n) (b:a:xs) in (y++x,[a,b])
swap' (a:xs) n = swap' (xs ++ [a]) (n-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment