2022 Day 15 Complete!

This commit is contained in:
Brian Buller 2022-12-20 07:34:46 -06:00
parent 6a843e488c
commit 4abc58d07d
2 changed files with 82 additions and 166 deletions

View File

@ -5,7 +5,6 @@ import (
"math" "math"
"os" "os"
"strings" "strings"
"time"
h "git.bullercodeworks.com/brian/adventofcode/helpers" h "git.bullercodeworks.com/brian/adventofcode/helpers"
) )
@ -79,121 +78,31 @@ func part1(inp []string) {
fmt.Println(nopeCount) fmt.Println(nopeCount)
} }
type Sensor struct {
c h.Coordinate // Sensor Position
b h.Coordinate // Beacon Position
d int // Distance
}
func (s Sensor) isInRange(c h.Coordinate) bool { return s.c.Distance(c) <= s.d }
func (s Sensor) String() string { return s.c.String() }
func part2(inp []string) { func part2(inp []string) {
var sensors []Sensor var spots [][]int
for i := range inp { for i := range inp {
s := Sensor{} s, b := strToCoords(inp[i])
s.c, s.b = strToCoords(inp[i]) xs, ys, xb, yb := s.X, s.Y, b.X, b.Y
s.d = s.c.Distance(s.b) dist := s.Distance(b)
sensors = append(sensors, s) spots = append(spots, []int{xs, ys, xb, yb, dist})
} }
min, max := 0, testRow*2
max := testRow * 2 // Brute-force it
found := []h.Coordinate{} for y := min; y <= max; y++ {
// Check around each sensor, the beacon must be it's distance + 1 SEARCH:
for _, sensor := range sensors { for x := min; x <= max; x++ {
d := sensor.d + 1 for _, c := range spots {
testEnds := []h.Coordinate{ if dx, dy := c[0]-x, c[1]-y; h.Abs(dx)+h.Abs(dy) <= c[4] {
{X: sensor.c.X, Y: sensor.c.Y - d}, // Jump across the sensor's scan area
{X: sensor.c.X + d, Y: sensor.c.Y}, x += c[4] - h.Abs(dy) + dx
{X: sensor.c.X, Y: sensor.c.Y + d}, continue SEARCH
{X: sensor.c.X - d, Y: sensor.c.Y},
}
printMap(sensors, max, max, h.Coordinate{X: math.MinInt, Y: math.MinInt})
var tstX, tstY int
for i := 0; i < 3; i++ {
// Test all spots between testEnds[i] & testEnds[i+1]
for tstX != testEnds[i+1].X && tstY != testEnds[i+1].Y {
if testEnds[i].X < testEnds[i+1].X {
tstX++
if testEnds[i].Y < testEnds[i+1].Y {
tstY++
} else {
tstY--
}
} else {
tstX--
if testEnds[i].Y < testEnds[i+1].Y {
tstY++
} else {
tstY--
} }
} }
} // If we get here, we found it.
if tstX <= max && tstY <= max {
fmt.Print(h.CLEAR_SCREEN)
printMap(sensors, max, max, h.Coordinate{X: tstX, Y: tstY})
fmt.Println("Testing", tstX, ",", tstY)
time.Sleep(time.Second / 10)
if testSpot(tstX, tstY, sensors) {
found = append(found, h.Coordinate{X: tstX, Y: tstY})
}
}
}
}
fmt.Println("# Part 2") fmt.Println("# Part 2")
fmt.Println(found) fmt.Println(x*4000000 + y)
//fmt.Println((found.X * 4000000) + found.Y) return
} }
func testSpot(x, y int, sensors []Sensor) bool {
var wasInRange bool
for i := range sensors {
if sensors[i].isInRange(h.Coordinate{X: x, Y: y}) {
wasInRange = true
break
}
}
return !wasInRange
}
func printMap(sensors []Sensor, ceilX, ceilY int, tstPos h.Coordinate) {
minX, maxX, minY, maxY := math.MaxInt, math.MinInt, math.MaxInt, math.MinInt
for i := range sensors {
minX = h.Min(sensors[i].b.X, minX)
maxX = h.Max(sensors[i].b.X, maxX)
minY = h.Min(sensors[i].b.Y, minY)
maxY = h.Max(sensors[i].b.Y, maxY)
}
for y := minY; y <= maxY; y++ {
for x := minX; x <= maxX; x++ {
wrk := h.Coordinate{X: x, Y: y}
var found bool
for _, sensor := range sensors {
if sensor.c.Equals(wrk) {
fmt.Print("S")
found = true
break
} else if sensor.b.Equals(wrk) {
fmt.Print("B")
found = true
break
} else if sensor.isInRange(wrk) {
fmt.Print("#")
found = true
break
}
}
if !found {
if wrk.Equals(tstPos) {
fmt.Print(h.FILL_CHAR)
} else {
fmt.Print(".")
}
}
}
fmt.Println()
} }
} }

View File

@ -1,43 +1,29 @@
Advent of Code Advent of Code
br0xen (AoC++) 32*
• [About]
• [Events]
• [Shop]
• [Settings]
• [Log Out]
br0xen (AoC++) 28*
   <y>2022</y>
• [Calendar]
• [AoC++]
• [Sponsors]
• [Leaderboard]
• [Stats]
Our sponsors help make Advent of Code possible:
BJSS - Our people are a team of problem solvers, experienced in evolving technologies and delivering
world-class technology solutions.
--- Day 15: Beacon Exclusion Zone --- --- 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 feel the ground rumble again as the distress signal leads you to a
You don't have time to search them all, but you don't need to: your pack contains a set of deployable sensors 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. 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 sensors aren't very powerful, but that's okay; your handheld device
the source of the distress signal to use them. You pull the emergency sensor system out of your pack, hit the 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. 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 Once a sensor finds a spot it thinks will give it a good reading, it
begins monitoring for the nearest signal source beacon. Sensors and beacons always exist at integer attaches itself to a hard surface and begins monitoring for the nearest
coordinates. Each sensor knows its own position and can determine the position of a beacon precisely; however, signal source beacon. Sensors and beacons always exist at integer
sensors can only lock on to the one beacon closest to the sensor as measured by the Manhattan distance. (There coordinates. Each sensor knows its own position and can determine the
is never a tie where two beacons are the same distance to a sensor.) 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). It doesn't take long for the sensors to report back their positions and
For example: closest beacons (your puzzle input). For example:
Sensor at x=2, y=18: closest beacon is at x=-2, y=15 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=9, y=16: closest beacon is at x=10, y=16
@ -54,10 +40,11 @@
Sensor at x=14, y=3: 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 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 So, consider the sensor at 2,18; the closest beacon to it is at -2,15. For
beacon to it is at 10,16. 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: Drawing sensors as S and beacons as B, the above arrangement of sensors
and beacons looks like this:
1 1 2 2 1 1 2 2
0 5 0 5 0 5 0 5 0 5 0 5
@ -85,10 +72,11 @@
21 ............................ 21 ............................
22 .......................B.... 22 .......................B....
This isn't necessarily a comprehensive map of all beacons in the area, though. Because each sensor only This isn't necessarily a comprehensive map of all beacons in the area,
identifies its closest beacon, if a sensor detects a beacon, you know there are no other beacons that close or though. Because each sensor only identifies its closest beacon, if a
closer to that sensor. There could still be beacons that just happen to not be the closest beacon to any sensor detects a beacon, you know there are no other beacons that close or
sensor. Consider the sensor at 8,7: 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 1 1 2 2
0 5 0 5 0 5 0 5 0 5 0 5
@ -118,16 +106,18 @@
21 ............................ 21 ............................
22 .......................B.... 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 This sensor's closest beacon is at 2,10, and so you know there are no
positions marked #). 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 None of the detected beacons seem to be producing the distress signal, so
distress beacon is by working out where it isn't. For now, keep things simple by counting the positions where a 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. 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 So, suppose you have an arrangement of beacons and sensors like in the
y=10, you'd like to count the number of positions a beacon cannot possibly exist. The coverage from all sensors example above and, just in the row where y=10, you'd like to count the
near that row looks like this: number of positions a beacon cannot possibly exist. The coverage from all
sensors near that row looks like this:
1 1 2 2 1 1 2 2
0 5 0 5 0 5 0 5 0 5 0 5
@ -135,16 +125,36 @@
10 ..####B######################.. 10 ..####B######################..
11 .###S#############.###########. 11 .###S#############.###########.
In this example, in the row where y=10, there are 26 positions where a beacon cannot be present. 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 Consult the report from the sensors you just deployed. In the row where
contain a beacon? y=2000000, how many positions cannot contain a beacon?
To begin, get your puzzle input. Your puzzle answer was 4748135.
Answer: _____________________ [ [Submit] ] --- Part Two ---
You can also [Shareon Twitter Mastodon] this puzzle. 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 13743542639657.
Both parts of this puzzle are complete! They provide two gold stars: **
References References
@ -152,7 +162,6 @@ References
. https://adventofcode.com/ . https://adventofcode.com/
. https://adventofcode.com/2022/about . https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events . https://adventofcode.com/2022/events
. https://teespring.com/stores/advent-of-code
. https://adventofcode.com/2022/settings . https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout . https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter . Advent of Code Supporter
@ -164,8 +173,6 @@ References
. https://adventofcode.com/2022/leaderboard . https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats . https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors . https://adventofcode.com/2022/sponsors
. https://www.bjss.com/
. https://en.wikipedia.org/wiki/Taxicab_geometry . https://en.wikipedia.org/wiki/Taxicab_geometry
. https://adventofcode.com/2022
. https://adventofcode.com/2022/day/15/input . https://adventofcode.com/2022/day/15/input
. https://twitter.com/intent/tweet?text=%22Beacon+Exclusion+Zone%22+%2D+Day+15+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F15&related=ericwastl&hashtags=AdventOfCode
. javascript:void(0);