advent-of-code

Entries to advent of code, multiple years
git clone git://git.finwo.net/misc/advent-of-code
Log | Files | Refs

commit 96935fb8eb497d4d111e645ffc57b8e0792fea56
parent 54fe807e4d4c175d1c433b3a7cc7c5ee0a4805a6
Author: finwo <finwo@pm.me>
Date:   Thu, 15 Dec 2022 14:06:26 +0100

Optimized step 1 of 2022/15

Diffstat:
M2022/day-15/index.js | 65++++++++++++++++++++++-------------------------------------------
1 file changed, 22 insertions(+), 43 deletions(-)

diff --git a/2022/day-15/index.js b/2022/day-15/index.js @@ -40,12 +40,6 @@ fs.createReadStream('input') parseInt(beaconTokens[6].replace(/[^\d\-]/g, '')), ]; - // // Track in visual representation - // rows[sensorPosition[1]] = rows[sensorPosition[1]] || []; - // rows[sensorPosition[1]][sensorPosition[0]] = 'S'; - // rows[beaconPosition[1]] = rows[beaconPosition[1]] || []; - // rows[beaconPosition[1]][beaconPosition[0]] = 'B'; - // Calculate distance between beacon and sensor const d = Math.abs(sensorPosition[0] - beaconPosition[0]) + Math.abs(sensorPosition[1] - beaconPosition[1]); @@ -55,22 +49,10 @@ fs.createReadStream('input') d, }); - // // Mark locations that can NOT have a beacon (step 1) - // for(let y=sensorPosition[1]-d; y<=sensorPosition[1]+d; y++) { - // if (!small && y !== scanline) continue; - // const X = d - Math.abs(sensorPosition[1] - y); - // for(let x=sensorPosition[0]-X; x<=sensorPosition[0]+X; x++) { - // const D = Math.abs(sensorPosition[0] - x) + Math.abs(sensorPosition[1] - y); - // if (D > d) continue; - // rows[y] = rows[y] || []; - // rows[y][x] = rows[y][x] || '#'; - // } - // } - - // xmin = Math.min(xmin, sensorPosition[0]-d, beaconPosition[0]-d); - // ymin = Math.min(ymin, sensorPosition[1]-d, beaconPosition[1]-d); - // xmax = Math.max(xmax, sensorPosition[0]+d, beaconPosition[0]+d); - // ymax = Math.max(ymax, sensorPosition[1]+d, beaconPosition[1]+d); + xmin = Math.min(xmin, sensorPosition[0]-d, beaconPosition[0]-d); + ymin = Math.min(ymin, sensorPosition[1]-d, beaconPosition[1]-d); + xmax = Math.max(xmax, sensorPosition[0]+d, beaconPosition[0]+d); + ymax = Math.max(ymax, sensorPosition[1]+d, beaconPosition[1]+d); // console.log({ sensorTokens, sensorPosition, beaconTokens, beaconPosition, d }); cb(); @@ -79,29 +61,26 @@ fs.createReadStream('input') .on('finish', () => { console.log('loaded'); - xmin -= 2; - ymin -= 2; - xmax += 2; - ymax += 2; - - // Debug display for small - if (small) { - for(let y=ymin; y<=ymax; y++) { - if (!rows[y]) { - process.stdout.write('.'.repeat(xmax-xmin+1) + '\n'); - continue; - } - for(let x=xmin; x<=xmax; x++) { - if (rows[y][x]) { - process.stdout.write(rows[y][x]); - } else { - process.stdout.write('.'); - } - } - process.stdout.write(` ${y}\n`); - } + // Step 1 + let covered = -1; + for(let x=xmin; x<=xmax; x++) { + + const found = sensors.find(sensor => { + const d = Math.abs(sensor.pos[0] - x) + Math.abs(sensor.pos[1] - scanline); + return d <= sensor.d; + }); + + if (!found) continue; + + // Add width at current Y and skip steps + const X = found.d - Math.abs(found.pos[1] - scanline); + covered += (found.pos[0] - x) + 1 + X; + x = found.pos[0] + X; } + console.log({ covered }); + + // Step 2 for(let y=0; y<=interestingArea; y++) { // console.log(y);