Skip to content

Instantly share code, notes, and snippets.

@guymorita
Created September 11, 2013 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guymorita/6517869 to your computer and use it in GitHub Desktop.
Save guymorita/6517869 to your computer and use it in GitHub Desktop.
three page path. final answer was P12:P9:P6 with 7 occurrences.
var fs = require('fs');
var _ = require('underscore');
var line, user, page;
var userPath = {};
fs.readFile('./input2.txt', 'utf8', function(err, data){
if (err) throw err;
var array = data.toString().split("\n");
for (i in array){
line = array[i].replace(/\s+/g,'').split(',');
user = line[1];
page = line[2];
if (line[0]){
if (!userPath[user]){
userPath[user] = [page];
} else {
userPath[user].push(page);
}
}
}
processFile();
});
var processFile = function(){
var finalPath = {};
var threePath;
_.each(userPath, function(value, key){
for (var i = 0; i < value.length; i++){
if (value[i-2]){
threePath = value.slice(i-2, i+1).join(":");
if (!finalPath[threePath]){
finalPath[threePath] = 1;
} else {
finalPath[threePath]++;
}
}
}
});
var maxPath, maxNum;
for (var key in finalPath){
if (!maxPath || finalPath[key] > maxNum){
maxPath = key;
maxNum = finalPath[key];
}
}
console.log('final', maxPath, maxNum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment