commit 96e78618c70b248af718f5bab089e044ce9081c3 Author: Enrico Fasoli Date: Fri Mar 13 11:01:18 2015 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/package.json b/package.json new file mode 100644 index 0000000..2f1431e --- /dev/null +++ b/package.json @@ -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" + } +} diff --git a/pert.coffee b/pert.coffee new file mode 100755 index 0000000..6a31fc4 --- /dev/null +++ b/pert.coffee @@ -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()