|
| 1 | +class Point { |
| 2 | + constructor(x, y, z, t) { |
| 3 | + this.x = x; |
| 4 | + this.y = y; |
| 5 | + this.z = z; |
| 6 | + this.t = t; |
| 7 | + } |
| 8 | + |
| 9 | + calculateManhattenDistance(point) { |
| 10 | + return ( |
| 11 | + Math.abs(this.x - point.x) + |
| 12 | + Math.abs(this.y - point.y) + |
| 13 | + Math.abs(this.z - point.z) + |
| 14 | + Math.abs(this.t - point.t) |
| 15 | + ); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class Constellation { |
| 20 | + constructor(points) { |
| 21 | + this.points = points; |
| 22 | + } |
| 23 | + |
| 24 | + addPoint(point) { |
| 25 | + this.points.push(point); |
| 26 | + } |
| 27 | + |
| 28 | + addPoints(points) { |
| 29 | + this.points = this.points.concat(points); |
| 30 | + } |
| 31 | + |
| 32 | + getPointDistance(point) { |
| 33 | + let minDist = Number.MAX_SAFE_INTEGER; |
| 34 | + this.points.forEach(member => { |
| 35 | + const dist = point.calculateManhattenDistance(member); |
| 36 | + if (dist < minDist) { |
| 37 | + minDist = dist; |
| 38 | + } |
| 39 | + }); |
| 40 | + return minDist; |
| 41 | + } |
| 42 | + |
| 43 | + canMerge(constellation) { |
| 44 | + //console.log('can merge', this, constellation); |
| 45 | + return this.points.some(myPoint => { |
| 46 | + return constellation.points.some(point => { |
| 47 | + //console.log('distance', myPoint.calculateManhattenDistance(point)); |
| 48 | + return myPoint.calculateManhattenDistance(point) <= 3; |
| 49 | + }); |
| 50 | + }); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function consellationCount(input) { |
| 55 | + let constellations = []; |
| 56 | + let points = parseCoordinates(input); |
| 57 | + points.forEach(point => { |
| 58 | + //Add to first constellation that fits |
| 59 | + const joined = constellations.some(constellation => { |
| 60 | + if (constellation.getPointDistance(point) <= 3) { |
| 61 | + constellation.addPoint(point); |
| 62 | + return true; |
| 63 | + } |
| 64 | + return false; |
| 65 | + }); |
| 66 | + if (!joined) { |
| 67 | + constellations.push(new Constellation([point])); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + //Now merge |
| 72 | + let merge = false; |
| 73 | + do { |
| 74 | + merge = false; |
| 75 | + constellations.forEach(constellationA => { |
| 76 | + constellations.forEach(constellationB => { |
| 77 | + if ( |
| 78 | + constellationA !== constellationB && |
| 79 | + constellationA.canMerge(constellationB) |
| 80 | + ) { |
| 81 | + merge = true; |
| 82 | + constellationA.addPoints(constellationB.points); |
| 83 | + const index = constellations.indexOf(constellationB); |
| 84 | + constellations.splice(index, 1); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + } while (merge); |
| 89 | + return constellations.length; |
| 90 | +} |
| 91 | + |
| 92 | +function parseCoordinates(input) { |
| 93 | + return input.split(/\r?\n/).map(line => { |
| 94 | + line = line.trim(); |
| 95 | + return new Point(...line.split(',')); |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +module.exports.consellationCount = consellationCount; |
0 commit comments