Unable to load image

Advent of Code 2022 : Day 9

grate filter edition

:#marseycapyhacker:

17
Jump in the discussion.

No email address required.

let a=fs.readFileSync('/tmp/xpo','utf8').split('\n').filter(a=>a).map(a=>a.split(' ')).flatMap(a=>[...new Array(+a[1])].map(_=>a[0]))

for (let num of [2, 10]) {
let poses = [...new Array(num)].map(a=>[0,0])
let history = new Set()

for (let d of a) {
  let [x,y] = {R: [1,0], L:[-1,0], U: [0,1], D:[0,-1]}[d]
  poses[0][0] += x; poses[0][1] += y;
  for (let i = 0; i < poses.length-1; i++) {
    let [hx,hy]=poses[i],[tx,ty]=poses[i+1]
    let ax = Math.abs(tx-hx), ay=Math.abs(ty-hy), dx = Math.sign(tx-hx), dy = Math.sign(ty-hy)
    if (ax==2&&ay==2) { tx = hx + dx; ty = hy + dy;
    }
    else if (ax == 2) { ty = hy; tx = hx + dx }
    else if (ay == 2) { tx = hx; ty = hy + dy }
    poses[i+1] = [tx, ty]
    if (i === poses.length-2) history.add(tx + ':' + ty)
  }
}
console.error(history.size)
}

vanilla js parsing :/ also vanilla js can't treat lists as values

Jump in the discussion.

No email address required.

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