Skip to content

Instantly share code, notes, and snippets.

@tamzinblake
Created December 17, 2015 18:32
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 tamzinblake/a00c58afed94ea481581 to your computer and use it in GitHub Desktop.
Save tamzinblake/a00c58afed94ea481581 to your computer and use it in GitHub Desktop.
var fs = require('fs')
var parse_log = function (log) {
var userPages = {}
logLines = log.split(/\r?\n/)
logLines.forEach(function (logLine) {
logLineData = logLine.split(/, /)
var userId = logLineData[1]
var pageId = logLineData[2]
if (userId !== undefined && pageId !== undefined) {
if (!userPages[userId]) {
userPages[userId] = []
}
userPages[userId].push(pageId)
}
})
var paths = {}
var userIds = Object.keys(userPages)
userIds.forEach(function (userId) {
var userSession = userPages[userId]
for (var i = 0; userSession[i+2] !== undefined; i++) {
userPathId = userSession[i] + ',' + userSession[i+1] + ',' + userSession[i+2]
if (!paths[userPathId]) {
paths[userPathId] = 0
}
paths[userPathId]++
}
})
return paths
}
var log = fs.readFileSync('./input1.txt', {
encoding: 'utf8'
})
var paths = parse_log(log)
var best = 0
var bestId
var pathIds = Object.keys(paths)
pathIds.forEach(function (pathId) {
if (paths[pathId] > best) {
best = paths[pathId]
bestId = pathId
}
})
console.log(bestId, best)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment