2022 Day 15 Complete!
This commit is contained in:
parent
6a843e488c
commit
4abc58d07d
@ -5,7 +5,6 @@ import (
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
@ -79,122 +78,32 @@ func part1(inp []string) {
|
||||
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) {
|
||||
var sensors []Sensor
|
||||
var spots [][]int
|
||||
for i := range inp {
|
||||
s := Sensor{}
|
||||
s.c, s.b = strToCoords(inp[i])
|
||||
s.d = s.c.Distance(s.b)
|
||||
sensors = append(sensors, s)
|
||||
s, b := strToCoords(inp[i])
|
||||
xs, ys, xb, yb := s.X, s.Y, b.X, b.Y
|
||||
dist := s.Distance(b)
|
||||
spots = append(spots, []int{xs, ys, xb, yb, dist})
|
||||
}
|
||||
|
||||
max := testRow * 2
|
||||
found := []h.Coordinate{}
|
||||
// Check around each sensor, the beacon must be it's distance + 1
|
||||
for _, sensor := range sensors {
|
||||
d := sensor.d + 1
|
||||
testEnds := []h.Coordinate{
|
||||
{X: sensor.c.X, Y: sensor.c.Y - d},
|
||||
{X: sensor.c.X + d, Y: sensor.c.Y},
|
||||
{X: sensor.c.X, Y: sensor.c.Y + d},
|
||||
{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 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})
|
||||
min, max := 0, testRow*2
|
||||
// Brute-force it
|
||||
for y := min; y <= max; y++ {
|
||||
SEARCH:
|
||||
for x := min; x <= max; x++ {
|
||||
for _, c := range spots {
|
||||
if dx, dy := c[0]-x, c[1]-y; h.Abs(dx)+h.Abs(dy) <= c[4] {
|
||||
// Jump across the sensor's scan area
|
||||
x += c[4] - h.Abs(dy) + dx
|
||||
continue SEARCH
|
||||
}
|
||||
}
|
||||
// If we get here, we found it.
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println(x*4000000 + y)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println(found)
|
||||
//fmt.Println((found.X * 4000000) + found.Y)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
func strToCoords(s string) (h.Coordinate, h.Coordinate) {
|
||||
|
@ -1,43 +1,29 @@
|
||||
Advent of Code
|
||||
|
||||
• [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.
|
||||
Advent of Code
|
||||
br0xen (AoC++) 32*
|
||||
|
||||
--- 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
|
||||
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
|
||||
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.)
|
||||
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:
|
||||
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
|
||||
@ -54,10 +40,11 @@
|
||||
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.
|
||||
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:
|
||||
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
|
||||
@ -85,10 +72,11 @@
|
||||
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:
|
||||
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
|
||||
@ -118,16 +106,18 @@
|
||||
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 #).
|
||||
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
|
||||
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:
|
||||
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
|
||||
@ -135,16 +125,36 @@
|
||||
10 ..####B######################..
|
||||
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
|
||||
contain a beacon?
|
||||
Consult the report from the sensors you just deployed. In the row where
|
||||
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
|
||||
|
||||
@ -152,7 +162,6 @@ References
|
||||
. https://adventofcode.com/
|
||||
. https://adventofcode.com/2022/about
|
||||
. https://adventofcode.com/2022/events
|
||||
. https://teespring.com/stores/advent-of-code
|
||||
. https://adventofcode.com/2022/settings
|
||||
. https://adventofcode.com/2022/auth/logout
|
||||
. Advent of Code Supporter
|
||||
@ -164,8 +173,6 @@ References
|
||||
. https://adventofcode.com/2022/leaderboard
|
||||
. https://adventofcode.com/2022/stats
|
||||
. https://adventofcode.com/2022/sponsors
|
||||
. https://www.bjss.com/
|
||||
. https://en.wikipedia.org/wiki/Taxicab_geometry
|
||||
. https://adventofcode.com/2022
|
||||
. 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);
|
||||
|
Loading…
Reference in New Issue
Block a user