Created
June 15, 2013 17:38
-
-
Save ibigbug/5788879 to your computer and use it in GitHub Desktop.
A Call Forwarding System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* | |
* Author: ibigbug | |
* Language: NodeJS | |
*/ | |
var fs = require('fs'); | |
function Controller(){ // Global Controller | |
if ( !(this instanceof arguments.callee) ){ | |
var F = arguments.callee; | |
return new F(arguments); | |
} | |
} | |
Controller.prototype.input = function(path){ | |
var input = fs.readFileSync(path).toString().split('\n').filter(function(line){return line!=='';}); | |
this.user_count = input.shift(); | |
this.calc_day = input.pop(); | |
if (parseInt(this.user_count, 10) != input.length){ | |
throw Error('Invalid input'); | |
} | |
this.users = input.map(function(user){ | |
var ret = {}; | |
ret.user = user; | |
ret.config = user.split(/\s/); | |
return ret; | |
}); | |
return this; | |
}; | |
Controller.prototype.process = function(){ | |
this.users.forEach(function(user){ | |
Transformer.regist(user.config); | |
}); | |
Transformer.calculate(); | |
return this; | |
}; | |
Controller.prototype.output = function(){ | |
Transformer.print_output(this.calc_day); | |
}; | |
var Transformer = { // Main process | |
user_instances: [], | |
chains: [], | |
regist: function(config){ | |
this.user_instances.push(new User(config)); | |
}, | |
calculate: function(){ | |
this.user_instances.forEach(function(user, i){ | |
var chain = new Chain(user); | |
chain.owner = user.phone_number; | |
chain.track_user(user); | |
Transformer.chains.push(chain); | |
}); | |
}, | |
print_output: function(calc_day){ | |
var cont = {}; | |
cont.chain = []; | |
cont.count = []; | |
this.chains.forEach(function(chain){ | |
if (chain.start_day < calc_day < chain.start_day + chain.days_lasts - 1){ | |
cont.chain.push(chain); | |
cont.count.push(chain.chain.length); | |
} | |
}); | |
console.log('第 %1 天有 %2 个呼叫转移设置'.replace('%1', calc_day).replace('%2', cont.chain.length)); | |
console.log('第 %1 天最长的呼叫转移是 %2 次'.replace('%1', calc_day).replace('%2', Math.max.apply(Math, cont.count))); | |
} | |
}; | |
/*===Models===*/ | |
function User(config){ // config = [phone_number, next_number, start_day, days_lasts]; | |
config.filter(function(item){return item!=='';}); | |
this.phone_number = config.shift(); | |
this.next_number = config.shift(); | |
this.start_day = config.shift(); | |
this.days_lasts = config.shift(); | |
} | |
User.find_by_number = function(phone_number){ | |
var user_instances = Transformer.user_instances; | |
var length = user_instances.length; | |
for (var i=0; i < length; i++){ | |
if (user_instances[i].phone_number == phone_number) return user_instances[i]; | |
} | |
return null; | |
}; | |
User.prototype.check_available = function(calc_day){ | |
return (this.start_day > calc_day || calc_day >= this.start_day + this.days_lasts); | |
}; | |
function Chain(user_conf){ | |
this.owner = user_conf.phone_number; | |
this.chain = []; | |
this.start_day = user_conf.start_day; | |
this.days_lasts = user_conf.days_lasts; | |
} | |
Chain.prototype.track_user = function(user){ | |
var err; | |
this.chain.push(user.phone_number); | |
var next = User.find_by_number(user.next_number); | |
if (next !== null){ | |
if ( this.check_circle(next) ){ | |
err = 'Circle Error: User %s is in circle'.replace('%s', next.phone_number); | |
throw Error(err); | |
} | |
else if (!next.check_available(controller.calc_day)){ | |
err = 'Available Error: User %1 is not available on day %2'.replace('%1', next.phone_number).replace('%2', controller.calc_day); | |
throw Error(err); | |
} | |
else { | |
arguments.callee.call(this, next); | |
} | |
} | |
}; | |
Chain.prototype.check_circle = function(user){ | |
return this.chain.some(function(phone_number){ return phone_number == user.phone_number; }); | |
}; | |
// Start System | |
var controller = new Controller(); | |
controller.input('./input').process().output(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 | |
0000 0001 1 3 | |
0001 4964 5 1 | |
4964 0005 5 3 | |
2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment