Skip to content

Instantly share code, notes, and snippets.

@lefuturiste
Created February 25, 2021 11:47
Show Gist options
  • Save lefuturiste/004ec24f12efd8d2dbb51d3a500e12d6 to your computer and use it in GitHub Desktop.
Save lefuturiste/004ec24f12efd8d2dbb51d3a500e12d6 to your computer and use it in GitHub Desktop.
/*******
* Read input from STDIN
* Use: console.log() to output your result.
* Use: console.error() to output debug information into STDERR
* ***/
var input = [];
readline_object.on("line", (value) => { //Read input values
input.push(value);
})
//Call ContestResponse when all inputs are read
readline_object.on("close", ContestResponse);
function getId() {
return Math.random().toString(16).substr(2, 8);
}
function ContestResponse(){
// first step: get a ordered list of all the points
let points = []
input.slice(1).forEach(e => {
let r = e.split(' ')
let id = getId()
let start = parseInt(r[0])
let end = parseInt(r[1])
points.push({id, pos: start, as: 'start'})
points.push({id, pos: end, as: 'end'})
})
points.sort((a, b) => a.pos - b.pos)
let activeZones = [] // list of active zone id
let zonesCount = 0
// cursor
points.forEach((p, i) => {
console.error(p.pos)
if (p.as == 'start') {
activeZones.push(p.id)
}
if (p.as == 'end') {
activeZones = activeZones.filter(d => d != p.id)
}
if (activeZones.length == 0 && points[i+1] != undefined && points[i+1].pos != p.pos+1 && points[i+1].pos != p.pos) {
zonesCount = zonesCount + 1
}
})
console.error(points)
console.log(zonesCount+1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment