Last active
January 31, 2017 23:46
-
-
Save fitzk/d55df8cd7ac0e090e521cba769cf38ed to your computer and use it in GitHub Desktop.
HackerRank: input processing, Agency List class, and logic to solve problem
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
// input processing | |
function createMatrix(rows) { | |
// regex to filter only digits | |
let re = /[0-9]/ | |
let matrix = [] | |
for ( let row of rows ) { | |
matrix.push( row.split(' ') | |
.filter( i => re.test(i) === true) | |
.map( i => parseInt(i) ) | |
.sort( (a,b) => a - b ) ); | |
} | |
return matrix | |
} | |
function parseInput(input) { | |
let rows = input.split('\n') | |
let meta = rows.shift() | |
let [n,s,i] = meta.split(' ').map(i => parseInt(i)) | |
return { n, i, rows } | |
} | |
// agency list class | |
class AgencyList { | |
constructor() { | |
this.vertices = [] | |
this.edges = {} | |
this.edgeCount = 0 | |
} | |
addVertex(vertex) { this.vertices.push(vertex) } | |
getVerticies() { return this.vertices } | |
initEdgeVertex(v) { if(!this.edges[v]) this.edges[v] = new Set() } | |
initEdgeVertices(v1, v2) { | |
this.initEdgeVertex(v1) | |
this.initEdgeVertex(v2) | |
} | |
addEdge(v1, v2) { | |
this.initEdgeVertices(v1, v2) | |
this.edges[v1].add(v2) | |
this.edges[v2].add(v1) | |
this.edgeCount++ | |
} | |
build(matrix) { | |
for ( let row of matrix ) { | |
let v1 = row[0], | |
v2 = row[1] | |
this.addVertex(v1) | |
this.addVertex(v2) | |
this.addEdge(v1, v2) | |
} | |
} | |
traverseDFS(v, visited) { | |
if(!this.edges[v]) return [v] | |
let path = [] | |
let toVisit = [] | |
toVisit.push(v) | |
do{ | |
toVisit = unique(toVisit) | |
let ev = toVisit.pop() | |
if(!visited.includes(ev)) { | |
visited.push(ev) | |
path.push(ev) | |
} | |
for(let k of this.edges[ev]) { | |
if(!visited.includes(k)) { | |
toVisit.push(k) | |
} | |
} | |
} while( toVisit.length !== 0 ); | |
return path | |
} | |
} | |
// helper functions | |
function getDiff(a1, a2) { | |
let a1Diff = a2.filter( a => !a1.includes(a) ) | |
let a2Diff = a1.filter( a => !a2.includes(a) ) | |
return [...a1Diff,...a2Diff] | |
} | |
function unique(arr){ | |
return [...new Set(arr.sort((a,b) => b-a))] | |
} | |
function generateArray(N) { | |
return Array.apply(null, {length: N}).map(Number.call, Number) | |
} | |
// calculate # of ways to pick 2 distinct items from | |
// m groups of astronauts | |
function getPaths(n,matrix) { | |
let m = new AgencyList(); | |
m.build(matrix); | |
let paths=[]; | |
let all = generateArray(n); | |
let verticies = unique(m.getVerticies()); | |
let singletons = getDiff(all, verticies); | |
verticies=[...verticies, ...singletons]; | |
let visitedNet=[] | |
while(verticies.length > 0) { | |
let k = verticies.pop(); | |
let path = m.traverseDFS(k, visitedNet); | |
visitedNet = path.length > 0 ? [...visitedNet, ...path] : visitedNet; | |
path.length > 0 ? paths.push( path ) : null; | |
} | |
return paths.filter( path => path !== null); | |
} | |
function getNetPairsOfAstronauts( astronautGroups ) { | |
let groups = astronautGroups.map(group => group.length); | |
let netCombinations = 0; | |
for( var m = 0; m < groups.length; m++ ) { | |
let otherGroups = groups.filter( (g, index) => index > m); | |
let sumOtherGroups = otherGroups.reduce( (a,b) => { return a+b }, 0); | |
let a = groups[m]; | |
netCombinations += a*sumOtherGroups; | |
} | |
return netCombinations; | |
} | |
function processData(input) { | |
let { n, i, rows } = parseInput(input); | |
let matrix = createMatrix(rows); | |
let paths = getPaths(n,matrix); | |
// runTests() | |
console.log(getNetPairsOfAstronauts(paths)); | |
} | |
process.stdin.resume(); | |
process.stdin.setEncoding("ascii"); | |
var _input = ""; | |
process.stdin.on("data", function (input) { | |
_input += input; | |
}); | |
process.stdin.on("end", function () { | |
processData(_input); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment