diamonds everywhere

This commit is contained in:
eleith 2022-12-17 06:56:54 -08:00
parent a0462f3490
commit 721ff25926
4 changed files with 314 additions and 0 deletions

116
15/README.md Normal file
View File

@ -0,0 +1,116 @@
--- Day 15: Beacon Exclusion Zone ---
You feel the ground rumble again as the distress signal leads you to a large network of subterranean tunnels. You don't have time to search them all, but you don't need to: your pack contains a set of deployable sensors that you imagine were originally built to locate lost Elves.
The sensors aren't very powerful, but that's okay; your handheld device indicates that you're close enough to the source of the distress signal to use them. You pull the emergency sensor system out of your pack, hit the big button on top, and the sensors zoom off down the tunnels.
Once a sensor finds a spot it thinks will give it a good reading, it attaches itself to a hard surface and begins monitoring for the nearest signal source beacon. Sensors and beacons always exist at integer coordinates. Each sensor knows its own position and can determine the position of a beacon precisely; however, sensors can only lock on to the one beacon closest to the sensor as measured by the Manhattan distance. (There is never a tie where two beacons are the same distance to a sensor.)
It doesn't take long for the sensors to report back their positions and closest beacons (your puzzle input). For example:
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
So, consider the sensor at 2,18; the closest beacon to it is at -2,15. For the sensor at 9,16, the closest beacon to it is at 10,16.
Drawing sensors as S and beacons as B, the above arrangement of sensors and beacons looks like this:
1 1 2 2
0 5 0 5 0 5
0 ....S.......................
1 ......................S.....
2 ...............S............
3 ................SB..........
4 ............................
5 ............................
6 ............................
7 ..........S.......S.........
8 ............................
9 ............................
10 ....B.......................
11 ..S.........................
12 ............................
13 ............................
14 ..............S.......S.....
15 B...........................
16 ...........SB...............
17 ................S..........B
18 ....S.......................
19 ............................
20 ............S......S........
21 ............................
22 .......................B....
This isn't necessarily a comprehensive map of all beacons in the area, though. Because each sensor only identifies its closest beacon, if a sensor detects a beacon, you know there are no other beacons that close or closer to that sensor. There could still be beacons that just happen to not be the closest beacon to any sensor. Consider the sensor at 8,7:
1 1 2 2
0 5 0 5 0 5
-2 ..........#.................
-1 .........###................
0 ....S...#####...............
1 .......#######........S.....
2 ......#########S............
3 .....###########SB..........
4 ....#############...........
5 ...###############..........
6 ..#################.........
7 .#########S#######S#........
8 ..#################.........
9 ...###############..........
10 ....B############...........
11 ..S..###########............
12 ......#########.............
13 .......#######..............
14 ........#####.S.......S.....
15 B........###................
16 ..........#SB...............
17 ................S..........B
18 ....S.......................
19 ............................
20 ............S......S........
21 ............................
22 .......................B....
This sensor's closest beacon is at 2,10, and so you know there are no beacons that close or closer (in any positions marked #).
None of the detected beacons seem to be producing the distress signal, so you'll need to work out where the distress beacon is by working out where it isn't. For now, keep things simple by counting the positions where a beacon cannot possibly be along just a single row.
So, suppose you have an arrangement of beacons and sensors like in the example above and, just in the row where y=10, you'd like to count the number of positions a beacon cannot possibly exist. The coverage from all sensors near that row looks like this:
1 1 2 2
0 5 0 5 0 5
9 ...#########################...
10 ..####B######################..
11 .###S#############.###########.
In this example, in the row where y=10, there are 26 positions where a beacon cannot be present.
Consult the report from the sensors you just deployed. In the row where y=2000000, how many positions cannot contain a beacon?
Your puzzle answer was 5525990.
--- Part Two ---
Your handheld device indicates that the distress signal is coming from a beacon nearby. The distress beacon is not detected by any sensor, but the distress beacon must have x and y coordinates each no lower than 0 and no larger than 4000000.
To isolate the distress beacon's signal, you need to determine its tuning frequency, which can be found by multiplying its x coordinate by 4000000 and then adding its y coordinate.
In the example above, the search space is smaller: instead, the x and y coordinates can each be at most 20. With this reduced search area, there is only a single position that could have a beacon: x=14, y=11. The tuning frequency for this distress beacon is 56000011.
Find the only possible position for the distress beacon. What is its tuning frequency?
Your puzzle answer was 11756174628223.

36
15/input.txt Normal file
View File

@ -0,0 +1,36 @@
Sensor at x=13820, y=3995710: closest beacon is at x=1532002, y=3577287
Sensor at x=3286002, y=2959504: closest beacon is at x=3931431, y=2926694
Sensor at x=3654160, y=2649422: closest beacon is at x=3702627, y=2598480
Sensor at x=3702414, y=2602790: closest beacon is at x=3702627, y=2598480
Sensor at x=375280, y=2377181: closest beacon is at x=2120140, y=2591883
Sensor at x=3875726, y=2708666: closest beacon is at x=3931431, y=2926694
Sensor at x=3786107, y=2547075: closest beacon is at x=3702627, y=2598480
Sensor at x=2334266, y=3754737: closest beacon is at x=2707879, y=3424224
Sensor at x=1613400, y=1057722: closest beacon is at x=1686376, y=-104303
Sensor at x=3305964, y=2380628: closest beacon is at x=3702627, y=2598480
Sensor at x=1744420, y=3927424: closest beacon is at x=1532002, y=3577287
Sensor at x=3696849, y=2604845: closest beacon is at x=3702627, y=2598480
Sensor at x=2357787, y=401688: closest beacon is at x=1686376, y=-104303
Sensor at x=2127900, y=1984887: closest beacon is at x=2332340, y=2000000
Sensor at x=3705551, y=2604421: closest beacon is at x=3702627, y=2598480
Sensor at x=1783014, y=2978242: closest beacon is at x=2120140, y=2591883
Sensor at x=2536648, y=2910642: closest beacon is at x=2707879, y=3424224
Sensor at x=3999189, y=2989409: closest beacon is at x=3931431, y=2926694
Sensor at x=3939169, y=2382534: closest beacon is at x=3702627, y=2598480
Sensor at x=2792378, y=2002602: closest beacon is at x=2332340, y=2000000
Sensor at x=3520934, y=3617637: closest beacon is at x=2707879, y=3424224
Sensor at x=2614525, y=1628105: closest beacon is at x=2332340, y=2000000
Sensor at x=2828931, y=3996545: closest beacon is at x=2707879, y=3424224
Sensor at x=2184699, y=2161391: closest beacon is at x=2332340, y=2000000
Sensor at x=2272873, y=1816621: closest beacon is at x=2332340, y=2000000
Sensor at x=1630899, y=3675405: closest beacon is at x=1532002, y=3577287
Sensor at x=3683190, y=2619409: closest beacon is at x=3702627, y=2598480
Sensor at x=180960, y=185390: closest beacon is at x=187063, y=-1440697
Sensor at x=1528472, y=3321640: closest beacon is at x=1532002, y=3577287
Sensor at x=3993470, y=2905566: closest beacon is at x=3931431, y=2926694
Sensor at x=1684313, y=20931: closest beacon is at x=1686376, y=-104303
Sensor at x=2547761, y=2464195: closest beacon is at x=2120140, y=2591883
Sensor at x=3711518, y=845968: closest beacon is at x=3702627, y=2598480
Sensor at x=3925049, y=2897039: closest beacon is at x=3931431, y=2926694
Sensor at x=1590740, y=3586256: closest beacon is at x=1532002, y=3577287
Sensor at x=1033496, y=3762565: closest beacon is at x=1532002, y=3577287

14
15/sample-input.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

148
15/src/index.ts Normal file
View File

@ -0,0 +1,148 @@
import { readFileSync } from 'fs'
type Point = { x: number; y: number }
type Sensor = Point & { beacon: Point; distance: number }
const parser = /Sensor at x=(-?\d+), y=(-?\d+): closest beacon is at x=(-?\d+), y=(-?\d+)/
function manHattanDistance(a: Point, b: Point) {
return Math.abs(a.x - b.x) + Math.abs(a.y - b.y)
}
function isWithinSensorRange({ x, y, distance }: Sensor, point: Point) {
return manHattanDistance({ x, y }, point) <= distance
}
function isUncovered(sensors: Sensor[], point: Point): boolean {
return sensors.every((sensor) => {
return !isWithinSensorRange(sensor, point)
})
}
function findCoverageForRow(sensors: Sensor[], row: number) {
const coverage = new Set<number>()
const numSensors = sensors.reduce((count, s) => count + (s.y === row ? 1 : 0), 0)
const numBeacons = sensors
.map((s) => s.beacon)
.reduce((list, b) => {
list[b.y] = list[b.y] || new Set()
list[b.y].add(b.x)
return list
}, {} as { [key: number]: Set<number> })?.[row].size
sensors.forEach((sensor) => {
const { x, distance } = sensor
for (let i = x - distance; i <= x + distance; i++) {
if (isWithinSensorRange(sensor, { x: i, y: row })) {
coverage.add(i)
}
}
})
return coverage.size - numBeacons - numSensors
}
function withinBounds(x: number, y: number, high: number, low: number) {
return x <= high && x >= low && y >= low && y <= high
}
function searchPerimeter(sensors: Sensor[], low: number, high: number): Point | undefined {
let x = 0
let y = 0
const found = sensors.some((sensor) => {
const perimeter = 1
const lowX = sensor.x - sensor.distance - perimeter
const lowY = sensor.y - sensor.distance - perimeter
const highX = sensor.x + sensor.distance + perimeter
const highY = sensor.y + sensor.distance + perimeter
const ranges = [
{
start: () => [lowX, sensor.y],
end: (x: number, y: number) => y <= lowY && withinBounds(x, y, high, low),
step: (x: number, y: number) => [x - 1, y - 1],
},
{
start: () => [lowX, sensor.y],
end: (x: number, y: number) => y <= highY && withinBounds(x, y, high, low),
step: (x: number, y: number) => [x - 1, y + 1],
},
{
start: () => [sensor.x, lowY],
end: (x: number, y: number) => x <= highX && withinBounds(x, y, high, low),
step: (x: number, y: number) => [x + 1, y + 1],
},
{
start: () => [sensor.x, highY],
end: (x: number, y: number) => x <= lowX && withinBounds(x, y, high, low),
step: (x: number, y: number) => [x - 1, y - 1],
},
]
return ranges.some(({ start, end, step }) => {
for ([x, y] = start(); end(x, y); [x, y] = step(x, y)) {
if (isUncovered(sensors, { x, y })) {
return true
}
}
return false
})
})
return found ? { x, y } : undefined
}
function parseSensorData(lines: string[]): Sensor[] {
return lines.map((line) => {
const matcher = line.match(parser)
if (matcher) {
const sensor = {
x: Number(matcher[1]),
y: Number(matcher[2]),
}
const beacon = {
x: Number(matcher[3]),
y: Number(matcher[4]),
}
const distance = manHattanDistance(sensor, beacon)
return {
...sensor,
beacon,
distance,
} as Sensor
}
throw new Error(`Could not parse line: ${line}`)
})
}
function solvePart1(lines: string[]): number {
const sensors = parseSensorData(lines)
const row = 2000000
return findCoverageForRow(sensors, row)
}
function solvePart2(lines: string[]): number | undefined {
const sensors = parseSensorData(lines)
const signal = searchPerimeter(sensors, 0, 4000000)
// const signal = findSignalNearby(map, 0, 20)
return signal ? signal.x * 4000000 + signal.y : undefined
}
function run() {
const input = process.argv.slice(2)[0]
const file = readFileSync(input, 'utf8')
const lines = file.split('\n').slice(0, -1)
const solution1 = solvePart1(lines)
console.log(`part1: ${solution1}`)
const solution2 = solvePart2(lines)
console.log(`part2: ${solution2}`)
}
run()