Compare commits

...

2 Commits

Author SHA1 Message Date
John Shaver 320b035125 15.2 done 2022-12-15 11:33:23 -08:00
John Shaver 7cc2ed62a6 15.1, but not 15.2 2022-12-15 00:07:09 -08:00
4 changed files with 145 additions and 0 deletions

14
2022/15/example.txt Normal file
View File

@ -0,0 +1,14 @@
Sensor at x=2, y=18: closest beacon is at x=-2, y=15
Sensor at x=9, y=16: closest beacon is at x=10, y=16
Sensor at x=13, y=2: closest beacon is at x=15, y=3
Sensor at x=12, y=14: closest beacon is at x=10, y=16
Sensor at x=10, y=20: closest beacon is at x=10, y=16
Sensor at x=14, y=17: closest beacon is at x=10, y=16
Sensor at x=8, y=7: closest beacon is at x=2, y=10
Sensor at x=2, y=0: closest beacon is at x=2, y=10
Sensor at x=0, y=11: closest beacon is at x=2, y=10
Sensor at x=20, y=14: closest beacon is at x=25, y=17
Sensor at x=17, y=20: closest beacon is at x=21, y=22
Sensor at x=16, y=7: closest beacon is at x=15, y=3
Sensor at x=14, y=3: closest beacon is at x=15, y=3
Sensor at x=20, y=1: closest beacon is at x=15, y=3

44
2022/15/index.js Normal file
View File

@ -0,0 +1,44 @@
const fs = require('fs/promises');
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {});
const parseXY = str => str.match(/x=(.*), y=(.*)$/).slice(1).map(x => BigInt(x));
const ROW = 2000000n;
const abs = x => x < 0n ? x * -1n : x;
const main = async () => {
const input = (await fs.readFile("./input.txt", 'utf8')).split("\n").slice(0, -1);
console.log(input);
const sets = input.map(row => row.split(": ").map(parseXY));
const map = new Set();
const getDistance = (x1, y1, x2, y2) => abs(x1 - x2) + abs(y1 - y2);
const fillRow = (centerX, radius) => {
console.log("Fill radius: ", radius);
for(let i = centerX - radius; i <= centerX + radius; ++i) {
map.add(i);
}
}
for(let [[x, y], b] of sets) {
const radius = getDistance(x, y, ...b);
const halfWidth = radius - abs(ROW - y)
if(halfWidth > 0) {
fillRow(x, halfWidth);
}
}
for(let [_, b] of sets) {
if(b[1] === ROW) {
map.delete(b[0]);
}
}
console.log("ANSWER: ", map.size);
}
main();

64
2022/15/index2.js Normal file
View File

@ -0,0 +1,64 @@
// Comnbining all ranges (min to max) from each beacon at each level of Y may be fast enough
const fs = require('fs/promises');
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
const arrayOfLength = n => Array.from(Array(n).keys()).map(() => {});
const parseXY = str => str.match(/x=(.*), y=(.*)$/).slice(1).map(x => parseInt(x));
const HEIGHT = 4000000;
const doTouch = (a, b, a2, b2) => {
const a1 = a - 1;
const b1 = b + 1;
if(b1 >= a2 && a1 <= b2) {
return true;
}
return false;
}
const getStartEnd = (x, rad) => [x-rad, x+rad];
const getDistance = (x1, y1, x2, y2) => abs(x1 - x2) + abs(y1 - y2);
const combineTouching = (a,b,c,d) => {
return [Math.min(a,c), Math.max(b,d)];
}
const abs = x => x < 0 ? x * -1 : x;
const main = async () => {
const input = (await fs.readFile("./input.txt", 'utf8')).split("\n").slice(0, -1);
const sets = input.map(row => row.split(": ").map(parseXY));
const sensors = sets.map(([[sX, sY], [bX, bY]]) => {
return [sX, sY, getDistance(sX, sY, bX, bY)];
});
for(let row = 0; row <= HEIGHT; row++) {
const coveredArea = sensors.filter(
([_, y, rad]) => Math.abs(row-y) <= rad
).map(
([x, y, rad]) => getStartEnd(x, rad - Math.abs(row - y))
).sort(
(a, b) => a[0] - b[0]
).reduce((ranges, [a1, b1]) => {
if(!ranges) {
return [[a1, b1]];
}
const [a, b] = ranges[ranges.length - 1];
if(doTouch(a, b, a1, b1)){
ranges[ranges.length - 1] = combineTouching(a, b, a1, b1);
} else {
ranges.push([a1, b1]);
}
return ranges;
}, false);
if(coveredArea.length > 1) {
console.log("FOUND IT! Answer: ", (coveredArea[0][1] + 1) * 4000000 + row);
process.exit();
}
if(row % 100000 === 0) {
console.log("DONE: ", row);
}
}
}
main();

23
2022/15/input.txt Normal file
View File

@ -0,0 +1,23 @@
Sensor at x=3907621, y=2895218: closest beacon is at x=3790542, y=2949630
Sensor at x=1701067, y=3075142: closest beacon is at x=2275951, y=3717327
Sensor at x=3532369, y=884718: closest beacon is at x=2733699, y=2000000
Sensor at x=2362427, y=41763: closest beacon is at x=2999439, y=-958188
Sensor at x=398408, y=3688691: closest beacon is at x=2275951, y=3717327
Sensor at x=1727615, y=1744968: closest beacon is at x=2733699, y=2000000
Sensor at x=2778183, y=3611924: closest beacon is at x=2275951, y=3717327
Sensor at x=2452818, y=2533012: closest beacon is at x=2733699, y=2000000
Sensor at x=88162, y=2057063: closest beacon is at x=-109096, y=390805
Sensor at x=2985370, y=2315046: closest beacon is at x=2733699, y=2000000
Sensor at x=2758780, y=3000106: closest beacon is at x=3279264, y=2775610
Sensor at x=3501114, y=3193710: closest beacon is at x=3790542, y=2949630
Sensor at x=313171, y=1016326: closest beacon is at x=-109096, y=390805
Sensor at x=3997998, y=3576392: closest beacon is at x=3691556, y=3980872
Sensor at x=84142, y=102550: closest beacon is at x=-109096, y=390805
Sensor at x=3768533, y=3985372: closest beacon is at x=3691556, y=3980872
Sensor at x=2999744, y=3998031: closest beacon is at x=3691556, y=3980872
Sensor at x=3380504, y=2720962: closest beacon is at x=3279264, y=2775610
Sensor at x=3357940, y=3730208: closest beacon is at x=3691556, y=3980872
Sensor at x=1242851, y=838744: closest beacon is at x=-109096, y=390805
Sensor at x=3991401, y=2367688: closest beacon is at x=3790542, y=2949630
Sensor at x=3292286, y=2624894: closest beacon is at x=3279264, y=2775610
Sensor at x=2194423, y=3990859: closest beacon is at x=2275951, y=3717327