-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.coffee
45 lines (36 loc) · 993 Bytes
/
graph.coffee
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
fs = require 'fs'
_ = require 'underscore'
users = [3,5,6]
class Graph
constructor: (text) ->
states = text.split '\n'
@states = {}
@s = []
@taken = {}
_.each states, (state) =>
[current, contigs...] = state.split ','
@states[current] = contigs
@s.push current
randomstate: () ->
item = Math.floor ( Math.random() * 1000 ) % 48
for state,i in Object.keys(@states) when i == item
return state
map: (state, users, upos) ->
if users.length == upos
return
user = users[upos]
if user == 0
return @map @randomstate(), users, (upos+1)
else
if !@taken[state]
@taken[state] = upos
users[upos] = users[upos] - 1
for st in @states[state] when !@taken[st]
return @map st, users, upos
return @map @randomstate(), users, upos
print: () ->
console.log @taken
fs.readFile 'states.txt', 'utf8', (err, data) ->
g = new Graph data
g.map 'AL', users, 0
g.print()