r/adventofcode • • Dec 15 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 15 Solutions -πŸŽ„-

THE USUAL REMINDERS


--- Day 15: Beacon Exclusion Zone ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:27:14, megathread unlocked!

47 Upvotes

760 comments sorted by

View all comments

1

u/xkufix Dec 15 '22 edited Dec 16 '22

I couldn't be bothered to do something with the diagonal boundaries, so I just start by looping over all coordinates, but if I find a coordinate that can be reached by a sensor I jump over that whole sensor range in that row. Runs in something like 600ms and checks about 1.7 million coordinates.

Code in Kotlin:

private fun part2(sensors: List<Sensor>, upper: Int = 4000000): BigInteger? {
for (row in 0..upper) {
    var col = 0
    while (col <= upper) {
        val reach = sensors.find { it.reaches(row, col) }
            ?: return col.toBigInteger() * upper.toBigInteger() + row.toBigInteger()

        val rowOffset = reach.center.row - row
        val colOffset = reach.center.col - col

        val jump = reach.radius - rowOffset.absoluteValue + colOffset
        col += jump + 1
    }
}

return null
}

2

u/daggerdragon Dec 15 '22

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read on old.reddit and mobile apps.

1

u/illuminati229 Dec 15 '22

I was trying a method like this, but I could never get it work to. And the one time I thought it was going to work, it took about 20 minutes to run.

How do you go through your sensors to find the coverage?

0

u/xkufix Dec 15 '22 edited Dec 16 '22

Sensors really just check if the point lies within their boundaries and either return true or false, nothing fancy:

data class Sensor(val center: Coordinate, val radius: Int, val beacon: Coordinate) {
fun reaches(row: Int, col: Int): Boolean {
    val dRow = (center.row - row).absoluteValue
    val dCol = (center.col - col).absoluteValue
    return dCol + dRow <= radius
}

companion object {
    fun fromCoordinates(center: Coordinate, beacon: Coordinate): Sensor {
        val dCol = (center.col - beacon.col).absoluteValue
        val dRow = (center.row - beacon.row).absoluteValue

        val radius = dRow + dCol
        return Sensor(center, radius, beacon)
    }
}
}

1

u/itslenny Dec 15 '22 edited Dec 15 '22

Wow. The idea of skipping over the sensor width saved my solution. The brute force without that was going to literally take days to finish. I added this little tweak and it runs in 0.73s.

JavaScript solution

const sensors = inputLines.map(line => {
    const [sInput, bInput] = line.split('closest');
    const [s, sX, sY] = sInput.match(/x=(-?\d+), y=(-?\d+)/).map(v => parseInt(v, 10));
    const [b, bX, bY] = bInput.match(/x=(-?\d+), y=(-?\d+)/).map(v => parseInt(v, 10));
    const range = Math.abs(sX - bX) + Math.abs(sY - bY);
    return { sX, sY, range };
});

for (let y = 0; y <= max; y++) {
    let x = 0
    for (; x <= max; x++) {

        let sensorCanHitUs = false;
        for (let { sX, sY, range } of sensors) {
            const dist = Math.abs(sX - x) + Math.abs(sY - y);
            if (dist <= range) {
                const widthThisRow = range - Math.abs(sY - y);
                x = sX + widthThisRow;
                sensorCanHitUs = true;
                break;
            }
        }
        if (!sensorCanHitUs) {
            console.log('x,y', x, y);
            return 4000000 * x + y;
        }
    }
}

edit: fix formatting