-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathgarage.hs
More file actions
32 lines (27 loc) · 1.48 KB
/
garage.hs
File metadata and controls
32 lines (27 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{-
initial: [1, 2, 3, 0, 4]
[0, 2, 3, 1, 4]
[2, 0, 3, 1, 4]
[2, 3, 0, 1, 4]
[0, 3, 2, 1, 4]
4
final should be: [0, 3, 2, 1, 4]
-}
-- getSteps [1, 2, 3, 0, 4] [0, 3, 2, 1, 4]
-- getSteps :: [Int] -> [Int] -> [[Int]]
getSteps initial final | initial == final = []
| otherwise = step : (getSteps step final)
where iInitial = zip [0..] initial
iFinal = zip [0..] final
zeroPos = fst $ head $ filter ((==0) . snd) iInitial -- where is zero
idealPos = map (\x -> fst $ head $ filter ((==x) . snd) iFinal) initial -- where everyone should be
benefit = map (\i -> abs(i - (idealPos !! i)) - abs(zeroPos - (idealPos !! i))) [0..length initial-1] -- what advantage if switching places with zero
bestPos = fst $ head $ filter ((== (maximum benefit)) . snd) $ filter ((/=zeroPos) . fst) $ zip [0..] benefit -- the one with highest advantage, other than zero
step = map (\(i,x) -> if i == zeroPos then initial !! bestPos else if i == bestPos then 0 else x) iInitial --swapping zero and the best
garage :: [Int] -> [Int] -> IO()
garage initial final = do
putStrLn ("initial: " ++ (show initial))
let steps = getSteps initial final
sequence $ map (\x -> putStrLn $ show x) steps
print $ length steps
putStrLn ("final should be: " ++ (show final))