-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.hs
62 lines (46 loc) · 1.88 KB
/
7.hs
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Data.Char
import Data.List
import Data.List.Split
import qualified Data.Map as Map
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
data Node = Node { nodeName :: Char,
parents :: String,
children :: String,
weight :: Int
} deriving (Eq, Ord, Show)
main = do
nodesText <- fmap Text.lines (Text.readFile "7.txt")
let nodesStrings = map Text.unpack nodesText
nodes = getNodes $ map stringToTuple nodesStrings
path = takeWhile (/= []) $ iterate f nodes
solution = map nodeName $ map getNextToRemove $ path
numberCanRemove = map ((map nodeName) . canRemove) path
putStrLn solution
print $ show numberCanRemove
stringToTuple s = (a, b)
where
w = words s
a = head $ w!!1
b = head $ w!!7
testData = [('C','A'),('C','F'),('A','B'),('A','D'),('B','E'),('D','E'),('F','E')]
findAllChildren a as = (map snd $ filter (\x -> a == fst x) as)
findAllParents a as = (map fst $ filter (\x -> a == snd x) as)
allID as = nub $ (map fst as) ++ (map snd as)
getNodes xs = [ Node {nodeName = x, parents = findAllParents x xs, children = findAllChildren x xs, weight = charToWeight x} | x <- allID xs ]
charToWeight :: Char -> Int
charToWeight c = ord c - 4
canRemove xs = filter (\x -> parents x == "") $ sort xs
getNextToRemove xs = head $ canRemove xs
removeNode x xs = ys
where
bs = filter (/= x) xs
ys = [Node {nodeName = nodeName y, parents = filter (/=(nodeName x)) $ parents y, children = children y, weight = weight y} | y <- bs]
f x = removeNode (getNextToRemove x) x
queues :: [[Node]]
queues = [[],[]]
subWeight x y = max 0 $ x - y
sortNodeByWeight n1 n2
| weight n1 < weight n2 = LT
| weight n1 > weight n2 = GT
| weight n1 == weight n2 = compare (nodeName n1) (nodeName n2)