Unable to load image

day 17 aoc thread: rocks fall everyone dies

bottom text

42
Jump in the discussion.

No email address required.

const gust = [...((fs.readFileSync(`/tmp/input.txt`,'utf8')))].filter(a => (a !== '\n'));
const shapes = ([
 [ [0,0],  [1,0],  [2,0],  [3,0], ],
 [ [0,1],  [1,0],  [1,1],  [2,1],  [1,2,] ],
 [ [0,0],  [1,0],  [2,0],  [2,1],  [2,2,] ],
 [ [0,0],  [0,1],  [0,2],  [0,3], ],
 [ [0,0],  [1,0],  [0,1],  [1,1], ],
]).map(a => ((i) => a.map(c => [i[0] + c[0], i[1] + c[1]])));
const toucheswall = (s => s.some(a => a[0] < 0 \|\| a[0] >= 7 \|\| a[1] < 0));
const touches = (s => toucheswall(s) || s.some(a => points.get(a[0] + ':' + a[1])));
let maxy = -1;
const move = ([x, y], s) => s.map(a => ([a[0] + x, a[1] + y]));
const put = s => {
    s.map(a => (points.set(a[0] + ':' + a[1], 1)));
    s.map((a => { maxy = Math.max(maxy, a[1]); }));
};
const points = (new Map());
const get = ((x,y) => points.get(x+':'+y));
const hashes = ({});
let nohash = false;
let jeti = 0;
let target = (p1 ? 2022 : 1000000000000);
let maxyadd = 0;
for (let ri = 0; ri < target; ri++) {
    let shape = (shapes[ri % shapes.length]([2, maxy + 4]));
    while (true) {
        jeti %= gust.length;
        let p = 0;
        const s2 = (move(gust[p = jeti++] == '>' ? [1, 0] : [-1, 0], shape));
        if (! touches(s2))
            shape = s2;
        const s3 = (move([0, -1], shape));
        if (touches(s3)) {
            put(shape);
            break;
        } else
            shape = s3;
    }
    if (!p1 && !nohash)
        if (maxy > 5) {
            for (let k = 0; k >= -5; k--) {
                const y = (k + maxy);
                let full = true;
                for (let j = 0; j < 7; j++) {
                    if (!get(j, y))
                        full = false;
                }
                if (full) {
                    const hash = (JSON.stringify({ rock: (ri % 5), jet: jeti % gust.length, point: range(k, 1).map(yo => range(0, 7).map(xo => get(xo, maxy + yo) ? 1 : 0))}));
                    if (hashes[hash]) {
                        const diff = (ri - hashes[hash].ri);
                        const mydiff = (maxy - hashes[hash].maxy);
                        while (diff + ri < target) {
                            ri += diff;
                            maxyadd += mydiff;
                            nohash = true;
                        }
                    } else
                        hashes[hash] = { ri, maxy };
                    break;
                }
            }
        }
}
/* the image */ (((range(0, maxy + 1).reverse())).map(y => (a => ('|' + a + '|'))((range(0, 7)).map((x => points.get(x + ':' + y) ? 'X' : ' ')).join(''))).join('\n'));
/* output */ (maxy+1+maxyadd);

not cleaned up even a bit

Jump in the discussion.

No email address required.

:#marseywoah:

Jump in the discussion.

No email address required.

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