first commit

This commit is contained in:
Enrico Fasoli 2015-03-13 11:01:18 +01:00
commit 96e78618c7
3 changed files with 70 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "pert",
"version": "0.0.1",
"description": "pert diagram calculator",
"main": "pert.coffee",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "http://github.com/fazo96/pert.git"
},
"author": "Enrico Fasoli (fazo96)",
"license": "MIT",
"bugs": {
"url": "https://github.com/fazo96/pert/issues"
},
"homepage": "https://github.com/fazo96/pert",
"dependencies": {
"chalk": "^1.0.0"
}
}

47
pert.coffee Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env coffee
console.log "pert"
list = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}]
maxa = (l) -> return Math.max.apply null, l
# Find the activity with given id
toActivity = (id) ->
item = {}
list.forEach (x) -> if x.id is id then item = x
return item
# Find the item
calculateEndDay = (item) ->
if !item.startDay?
console.log "calculating start day of",item.id
item.startDay = calculateStartDay item
console.log "Start day of",item.id,":",item.startDay
item.endDay = item.startDay + item.duration
console.log "End Day of",item.id,":",item.endDay
return item.endDay
# Find out which day the activity starts
calculateStartDay = (item) ->
if item.depends.length is 0 then return item.startDay = 0
console.log "Deps:",item.depends.map(toActivity)
console.log "EndDays:",item.depends.map(toActivity).map(calculateEndDay)
max = maxa item.depends.map(toActivity).map(calculateEndDay)
console.log max
# write max delay time to each depend
item.depends.forEach (x) ->
console.log "Writing permittedDelay to dependency", x
i = toActivity x
i.permittedDelay = max - calculateEndDay(i)
return item.startDay = max
# Find out which activity has the highest id
highestID = -> return maxa(list.map (x) -> x.id)
calculate = ->
calculateEndDay toActivity(highestID())
list.forEach (x) -> if !x.permittedDelay? then x.final = true
return list
console.log calculate()