Unable to load image

Advent of Code 2022: Day 8

Post solutions here :marseysad: :marseyill:

25
Jump in the discussion.

No email address required.

let grid=fs.readFileSync('/tmp/teste', 'utf8').split('\n').map(a=>a.split('').map(a=>+a)).filter(a=>a.length)
let viewacc = acc => grid.flatMap((_,i)=>
  grid[0].map((_,j)=>
    acc(grid[i][j],
      grid.slice(0,i).map(a=>a[j]).reverse(),
      grid.slice(i+1).map(a=>a[j]),
      grid[i].slice(0,j).reverse(),
      grid[i].slice(j+1))))

console.log(viewacc((x,...r) => r.some(a=>a.every(a=>a<x))).reduce((a,b)=>a+b))

console.log(viewacc((x,...r) => (r.map(a=>((a.map((z,i)=>i).filter(i=>a[i]>=x||i+1==a.length))[0]??0) + 1)).reduce((a,b)=>a*b)).reduce((a,b)=>Math.max(a,b)))

python would be much better for this bc slices and sum()/max() or just numpy

Jump in the discussion.

No email address required.

Every time I read one of your answers I feel like you're one of those people who intentionally makes their code illegible so that they keep you around just so nobody else has to go figure out what you did.

Jump in the discussion.

No email address required.

the above is much simpler than the other solutions though. the names are shit because it's just a game but 'viewacc' takes a function and applies it to each tree's list of adjacent trees going out north, south, east, and west. so you pass it a function that takes the tree's value, and then four lists of tree values. this lets you make the two problems easier to understand, and the code for them doesn't have to worry about anything other than those four lists, and can treat each direction the same way. it's pre messy because vanilla js + written quickly, the numpy solution is much smaller and makes more sense, but if things were given better names it'd be much better than the others.

Jump in the discussion.

No email address required.

Link copied to clipboard
Action successful!
Error, please refresh the page and try again.