Skip to content

Instantly share code, notes, and snippets.

@Mellen
Created December 9, 2021 09:17
Show Gist options
  • Save Mellen/4ab2bb7c2a5487ca056288a2483f2bef to your computer and use it in GitHub Desktop.
Save Mellen/4ab2bb7c2a5487ca056288a2483f2bef to your computer and use it in GitHub Desktop.
(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(points[i]);
}
}
return lowpoints.reduce((s,v) => s+v+1, 0);
})(document.querySelector('pre').textContent);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment