Created
September 11, 2013 00:36
-
-
Save guymorita/6517869 to your computer and use it in GitHub Desktop.
three page path. final answer was P12:P9:P6 with 7 occurrences.
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
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