summaryrefslogtreecommitdiff
path: root/testsuite/tests/ghci.debugger/scripts/T18045.hs
diff options
context:
space:
mode:
Diffstat (limited to 'testsuite/tests/ghci.debugger/scripts/T18045.hs')
-rw-r--r--testsuite/tests/ghci.debugger/scripts/T18045.hs29
1 files changed, 29 insertions, 0 deletions
diff --git a/testsuite/tests/ghci.debugger/scripts/T18045.hs b/testsuite/tests/ghci.debugger/scripts/T18045.hs
new file mode 100644
index 0000000000..2bb9b90d05
--- /dev/null
+++ b/testsuite/tests/ghci.debugger/scripts/T18045.hs
@@ -0,0 +1,29 @@
+import qualified Data.IntSet as IntSet
+import Data.Array
+
+newtype Graph = Graph { neighbors :: Array Int [Int] }
+
+readGraph :: [[Int]] -> Graph
+readGraph ([n] : edges) = ans where
+ ans = Graph $ accumArray (flip (:)) [] (1, n) edgesTwice
+ edgesTwice = concat [[(u, v), (v, u)] | [u, v] <- edges]
+readGraph _ = error "readGraph: bad format"
+
+bfs :: Graph -> Int -> Array Int Int
+-- returns an array with the distance from each node to the start node
+bfs g start = array (bounds (neighbors g)) $ assocList where
+ assocList = _bfs 0 IntSet.empty (IntSet.singleton start)
+ _bfs dist visited currs = if IntSet.null currs
+ then []
+ else map (\x -> (x, dist)) currli ++ _bfs (dist+1) nvisit ncurr where
+ currli = IntSet.toList currs
+ nvisit = IntSet.union visited currs
+ ncurr = IntSet.difference nbrs nvisit
+ nbrs = IntSet.fromList (concatMap (neighbors g !) currli)
+
+solve :: [[Int]] -> [Int]
+solve li@(_:edges) = map ((`mod` 2) . (bfs (readGraph li) 1 !) . head) edges
+solve _ = error "no input?"
+
+parse :: String -> [[Int]]
+parse = map (map read . words) . lines