Skip to content

Instantly share code, notes, and snippets.

@Mellen
Created December 3, 2020 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mellen/bc5388b96ed3b1dfef3af3c6b4d5138b to your computer and use it in GitHub Desktop.
Save Mellen/bc5388b96ed3b1dfef3af3c6b4d5138b to your computer and use it in GitHub Desktop.
function treeHitCount()
{
const lines = document.getElementsByTagName('pre')[0].innerHTML;
const input = lines.split('\n').map(line => line.split(''));
let slopes = [[1,1],[3,1],[5,1],[7,1],[1,2]];
let total = 1;
for(let slope of slopes)
{
let [xinc, yinc] = slope;
let x = 0;
let hitcount = 0;
for(let y = 0; y < input.length; y += yinc)
{
if(input[y][x] == '#')
{
hitcount++
}
x += xinc;
if(x >= input[y].length)
{
x = x%input[y].length;
}
}
total = total * hitcount;
}
return total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment