-
-
Save Mellen/16aca830d5bb5a498e000b538464cd22 to your computer and use it in GitHub Desktop.
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
(function(inp) | |
{ | |
function getNeighbours(x,y) | |
{ | |
let deltas = [ | |
[-1,0], | |
[0,-1], | |
[0,1], | |
[1,0], | |
]; | |
let neighbours = []; | |
for(let delta of deltas) | |
{ | |
let [xd, yd] = delta; | |
neighbours.push([xd+x, yd+y]); | |
} | |
return neighbours; | |
} | |
let points = inp.split('\n').map(line => line.split('')).filter(line => line.length > 0).flat().map(v => parseInt(v,10)); | |
const width = 100; | |
let lowpoints = []; | |
for(let i = 0; i < points.length; i++) | |
{ | |
let x = i % width; | |
let y = Math.floor(i / width); | |
let neighbours = getNeighbours(x,y); | |
let highercount = 0; | |
for(let neigh of neighbours) | |
{ | |
let [nx, ny] = neigh; | |
if(nx >= width || nx < 0 || ny >= width || ny < 0) | |
{ | |
highercount++; | |
continue; | |
} | |
let ni = nx + (ny * width); | |
if(points[ni] > points[i]) | |
{ | |
highercount++; | |
} | |
} | |
if(highercount == 4) | |
{ | |
lowpoints.push(i); | |
} | |
} | |
let basins = new Map(); | |
for(let index of lowpoints) | |
{ | |
basins.set(index, new Set([index])); | |
} | |
for(let index of lowpoints) | |
{ | |
let lx = index%width; | |
let ly = Math.floor(index/width); | |
let neighbours = getNeighbours(lx, ly); | |
let pointsToCheck = []; | |
for(let neigh of neighbours) | |
{ | |
let [nx, ny] = neigh; | |
if(nx >= width || nx < 0 || ny >= width || ny < 0) | |
{ | |
continue; | |
} | |
let ni = nx + (ny*width); | |
if(points[ni] < 9) | |
{ | |
let b = basins.get(index) | |
b.add(ni); | |
pointsToCheck.push(ni); | |
} | |
} | |
let npi = 0; | |
while(npi <= pointsToCheck.length) | |
{ | |
let pindex = pointsToCheck[npi]; | |
let px = pindex%width; | |
let py = Math.floor(pindex/width); | |
let pneighbours = getNeighbours(px,py); | |
for(let neigh of pneighbours) | |
{ | |
let [nx, ny] = neigh; | |
if(nx >= width || nx < 0 || ny >= width || ny < 0) | |
{ | |
continue; | |
} | |
let ni = nx + (ny*width); | |
if(points[ni] < 9) | |
{ | |
let b = basins.get(index) | |
b.add(ni); | |
if(!pointsToCheck.includes(ni)) | |
{ | |
pointsToCheck.push(ni); | |
} | |
} | |
} | |
npi++; | |
} | |
} | |
let longests=[new Set(),new Set(),new Set()]; | |
for(let [index, basin] of basins) | |
{ | |
if(basin.size > longests[0].size) | |
{ | |
longests[2] = longests[1]; | |
longests[1] = longests[0]; | |
longests[0] = basin; | |
} | |
else if(basin.size > longests[1].size) | |
{ | |
longests[2] = longests[1]; | |
longests[1] = basin; | |
} | |
else if(basin.size > longests[2].size) | |
{ | |
longests[2] = basin; | |
} | |
} | |
return longests[0].size * longests[1].size * longests[2].size; | |
})(document.querySelector('pre').textContent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment