diff --git a/pert.coffee b/pert.coffee index 6a31fc4..94c7588 100755 --- a/pert.coffee +++ b/pert.coffee @@ -1,47 +1,50 @@ #!/usr/bin/env coffee +chalk = require 'chalk' -console.log "pert" +verbose = no -list = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}] +log = (x...) -> if verbose then console.log chalk.bold("Log:"), x... +err = (x...) -> console.log chalk.bold (chalk.red "Error:"), x... +# Returns the highest number in an array of numbers maxa = (l) -> return Math.max.apply null, l # Find the activity with given id -toActivity = (id) -> +toActivity = (id,list) -> + if !list? then err "list is",list item = {} list.forEach (x) -> if x.id is id then item = x return item # Find the item -calculateEndDay = (item) -> +calculateEndDay = (item,list) -> if !item.startDay? - console.log "calculating start day of",item.id - item.startDay = calculateStartDay item - console.log "Start day of",item.id,":",item.startDay + log "calculating start day of",item.id + item.startDay = calculateStartDay item, list + log "start day of",item.id,"is",item.startDay item.endDay = item.startDay + item.duration - console.log "End Day of",item.id,":",item.endDay + log "end day of",item.id,"is",item.endDay return item.endDay # Find out which day the activity starts -calculateStartDay = (item) -> +calculateStartDay = (item,list) -> 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 + max = maxa item.depends.map((x) -> toActivity x,list).map((x) -> calculateEndDay x, list) + log "start day of",item.id,"is",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) + log "writing permittedDelay to dependency", x, "of", item + i = toActivity x, list + i.permittedDelay = max - calculateEndDay i, list + log "permitted delay of",x,"is",i.permittedDelay return item.startDay = max # Find out which activity has the highest id -highestID = -> return maxa(list.map (x) -> x.id) +highestID = (list) -> return maxa(list.map (x) -> x.id) -calculate = -> - calculateEndDay toActivity(highestID()) - list.forEach (x) -> if !x.permittedDelay? then x.final = true +calculate = (list) -> + calculateEndDay (toActivity highestID(list), list), list return list -console.log calculate() +ex = [{id: 0, depends: [], duration: 2}, { id: 1, depends: [0], duration: 3},{id: 2, depends: [0,1], duration: 4}] +console.log calculate ex