Last active
December 17, 2015 18:54
-
-
Save tamzinblake/80ff87c93c07b1838aa1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 parse_log = function (log) { | |
var userPages = {} | |
var paths = {} | |
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] = [] | |
} | |
var userSession = userPages[userId] | |
userSession.push(pageId) | |
if (userSession.length === 3) { | |
var userPathId = userSession[0] + ',' + userSession[1] + ',' + userSession[2] | |
if (!paths[userPathId]) { | |
paths[userPathId] = 0 | |
} | |
paths[userPathId]++ | |
userSession.shift() | |
} | |
} | |
}) | |
return paths | |
} | |
var log = fs.readFileSync('./input2.txt', { | |
encoding: 'utf8' | |
}) | |
var paths = parse_log(log) | |
var best = 0 | |
var bestIds = [] | |
var pathIds = Object.keys(paths) | |
pathIds.forEach(function (pathId) { | |
if (paths[pathId] > best) { | |
bestIds = [pathId] | |
best = paths[pathId] | |
} else if (paths[pathId] === best) { | |
bestIds.push(pathId) | |
} | |
}) | |
console.log(bestIds, best) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment