AoC Day 16, Parts 1 & 2 (Solved)

This commit is contained in:
🐙PiperYxzzy
2023-12-16 09:21:32 +02:00
parent ddb00d61b5
commit 0891160e88
4 changed files with 410 additions and 0 deletions

70
2023/16/README.md Executable file
View File

@@ -0,0 +1,70 @@
## \-\-\- Day 16: The Floor Will Be Lava ---
With the beam of light completely focused _somewhere_, the reindeer leads you deeper still into the Lava Production Facility. At some point, you realize that the steel facility walls have been replaced with cave, and the doorways are just cave, and the floor is cave, and you're pretty sure this is actually just a giant cave.
Finally, as you approach what must be the heart of the mountain, you see a bright light in a cavern up ahead. There, you discover that the beam of light you so carefully focused is emerging from the cavern wall closest to the facility and pouring all of its energy into a contraption on the opposite side.
Upon closer inspection, the contraption appears to be a flat, two-dimensional square grid containing _empty space_ ( `.`), _mirrors_ ( `/` and `\`), and _splitters_ ( `|` and `-`).
The contraption is aligned so that most of the beam bounces around the grid, but each tile on the grid converts some of the beam's light into _heat_ to melt the rock in the cavern.
You note the layout of the contraption (your puzzle input). For example:
```
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....
```
The beam enters in the top-left corner from the left and heading to the _right_. Then, its behavior depends on what it encounters as it moves:
- If the beam encounters _empty space_ ( `.`), it continues in the same direction.
- If the beam encounters a _mirror_ ( `/` or `\`), the beam is _reflected_ 90 degrees depending on the angle of the mirror. For instance, a rightward-moving beam that encounters a `/` mirror would continue _upward_ in the mirror's column, while a rightward-moving beam that encounters a `\` mirror would continue _downward_ from the mirror's column.
- If the beam encounters the _pointy end of a splitter_ ( `|` or `-`), the beam passes through the splitter as if the splitter were _empty space_. For instance, a rightward-moving beam that encounters a `-` splitter would continue in the same direction.
- If the beam encounters the _flat side of a splitter_ ( `|` or `-`), the beam is _split into two beams_ going in each of the two directions the splitter's pointy ends are pointing. For instance, a rightward-moving beam that encounters a `|` splitter would split into two beams: one that continues _upward_ from the splitter's column and one that continues _downward_ from the splitter's column.
Beams do not interact with other beams; a tile can have many beams passing through it at the same time. A tile is _energized_ if that tile has at least one beam pass through it, reflect in it, or split in it.
In the above example, here is how the beam of light bounces around the contraption:
```
>|<<<\....
|v-.\^....
.v...|->>>
.v...v^.|.
.v...v^...
.v...v^..\
.v../2\\..
<->-/vv|..
.|<<<2-|.\
.v//.|.v..
```
Beams are only shown on empty tiles; arrows indicate the direction of the beams. If a tile contains beams moving in multiple directions, the number of distinct directions is shown instead. Here is the same diagram but instead only showing whether a tile is _energized_ ( `#`) or not ( `.`):
```
######....
.#...#....
.#...#####
.#...##...
.#...##...
.#...##...
.#..####..
########..
.#######..
.#...#.#..
```
Ultimately, in this example, `46` tiles become _energized_.
The light isn't energizing enough tiles to produce lava; to debug the contraption, you need to start by analyzing the current situation. With the beam starting in the top-left heading right, _how many tiles end up being energized?_

220
2023/16/code.go Normal file
View File

@@ -0,0 +1,220 @@
package main
import (
"strings"
"github.com/jpillora/puzzler/harness/aoc"
)
func main() {
aoc.Harness(run)
}
// on code change, run will be executed 4 times:
// 1. with: false (part1), and example input
// 2. with: true (part2), and example input
// 3. with: false (part1), and user input
// 4. with: true (part2), and user input
// the return value of each run is printed to stdout
type BeamSect struct {
Direction rune
Y int
X int
}
type Beam struct {
Direction rune
Path []BeamSect
EnergizedTiles int
}
type Tile struct {
Type rune
ShineN bool
ShineE bool
ShineS bool
ShineW bool
X int
Y int
}
func calcEnergized(grid [][]Tile, start Tile) int {
tilesChanged := []Tile{start}
for len(tilesChanged) > 0 {
// Contagion
stillChanged := make([]Tile, 0)
for _, t := range tilesChanged {
if t.ShineE && t.X < len(grid[t.Y])-1 {
eX := t.X + 1
eY := t.Y
shineInto := &grid[eY][eX]
if (shineInto.Type == '.' || shineInto.Type == '-') && !shineInto.ShineE {
// Change!
shineInto.ShineE = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '|' && (!shineInto.ShineN || !shineInto.ShineS) {
shineInto.ShineN = true
shineInto.ShineS = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '/' && !shineInto.ShineN {
shineInto.ShineN = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '\\' && !shineInto.ShineS {
shineInto.ShineS = true
stillChanged = append(stillChanged, *shineInto)
}
}
if t.ShineW && t.X > 0 {
wX := t.X - 1
wY := t.Y
shineInto := &grid[wY][wX]
if (shineInto.Type == '.' || shineInto.Type == '-') && !shineInto.ShineW {
// Change!
shineInto.ShineW = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '|' && (!shineInto.ShineN || !shineInto.ShineS) {
shineInto.ShineN = true
shineInto.ShineS = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '/' && !shineInto.ShineS {
shineInto.ShineS = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '\\' && !shineInto.ShineN {
shineInto.ShineN = true
stillChanged = append(stillChanged, *shineInto)
}
}
if t.ShineN && t.Y > 0 {
nX := t.X
nY := t.Y - 1
shineInto := &grid[nY][nX]
if (shineInto.Type == '.' || shineInto.Type == '|') && !shineInto.ShineN {
// Change!
shineInto.ShineN = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '-' && (!shineInto.ShineE || !shineInto.ShineW) {
shineInto.ShineE = true
shineInto.ShineW = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '/' && !shineInto.ShineE {
shineInto.ShineE = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '\\' && !shineInto.ShineW {
shineInto.ShineW = true
stillChanged = append(stillChanged, *shineInto)
}
}
if t.ShineS && t.Y < len(grid)-1 {
sX := t.X
sY := t.Y + 1
shineInto := &grid[sY][sX]
if (shineInto.Type == '.' || shineInto.Type == '|') && !shineInto.ShineS {
// Change!
shineInto.ShineS = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '-' && (!shineInto.ShineE || !shineInto.ShineW) {
shineInto.ShineE = true
shineInto.ShineW = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '/' && !shineInto.ShineW {
shineInto.ShineW = true
stillChanged = append(stillChanged, *shineInto)
} else if shineInto.Type == '\\' && !shineInto.ShineE {
shineInto.ShineE = true
stillChanged = append(stillChanged, *shineInto)
}
}
}
tilesChanged = stillChanged
}
energized := 0
for v := range grid {
for h := range grid[v] {
if grid[v][h].ShineE || grid[v][h].ShineN || grid[v][h].ShineS || grid[v][h].ShineW {
energized += 1
}
}
}
return energized
}
func resetGrid(grid [][]Tile) {
for v := range grid {
for h := range grid[v] {
grid[h][v].ShineE = false
grid[h][v].ShineN = false
grid[h][v].ShineS = false
grid[h][v].ShineW = false
}
}
}
func run(part2 bool, input string) any {
// when you're ready to do part 2, remove this "not implemented" block
grid := make([][]Tile, 0)
for _, l := range strings.Split(input, "\n") {
if l == "" {
continue
}
gridLine := make([]Tile, 0)
for r := range l {
gridLine = append(gridLine, Tile{Type: rune(l[r]), X: len(gridLine), Y: len(grid)})
}
grid = append(grid, gridLine)
}
if part2 {
maxEnergized := -1
for v := range grid {
testE := calcEnergized(grid, Tile{ShineE: true, X: -1, Y: v})
resetGrid(grid)
if testE > maxEnergized {
maxEnergized = testE
}
testW := calcEnergized(grid, Tile{ShineW: true, X: len(grid[v]), Y: v})
resetGrid(grid)
if testW > maxEnergized {
maxEnergized = testW
}
}
for h := range grid[0] {
testN := calcEnergized(grid, Tile{ShineN: true, X: h, Y: len(grid)})
resetGrid(grid)
if testN > maxEnergized {
maxEnergized = testN
}
testS := calcEnergized(grid, Tile{ShineS: true, X: h, Y: -1})
resetGrid(grid)
if testS > maxEnergized {
maxEnergized = testS
}
}
return maxEnergized
}
// solve part 1 her
return calcEnergized(grid, Tile{ShineE: true, X: -1, Y: 0})
}

10
2023/16/input-example.txt Executable file
View File

@@ -0,0 +1,10 @@
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....

110
2023/16/input-user.txt Normal file
View File

@@ -0,0 +1,110 @@
\................................................................\.-.........../.................-........-./.
......./........|..../......\...................../......\.....\.....-|/|............................/.../....
....\...........-.....-..|\..../\...-............./../......\..|......\............/...................\\.....
............................................................................-....|.........../.|............-.
....-..\..........\..........-....................../......|\...........................-...-.................
..\....../.......\....|....................-......\.-.........|......../.|../.................................
.................../....../-.-.\..........|............................../\.../...-.|....|............../.....
.......................\................\....|....|....|........\..................../|/-.....................
...........\../...|...............-...............|........-./..../-.............-.............-......../.....
./...-..............................-../.../..../...-....../.............|........-..............|............
..|...........................................-..-\.......-......\...........-...........-..............-.....
........................./.|..........-...--......./..................\..........-.............../...//|......
............-|.......|.-.....\.............................\........../.-.............|.......................
-...........\.........../......|......|...|.-............/.\.........\.................\.......-.-.....\-.....
................/...\....................../........................||.................../...........-..|../..
...|./........./................|...................../....-.........|.......|............|................/..
..............|.......\..........\..................|.\.....-...........|..|...-.....\../..........|....-.....
...\...............\............-........../..........\.\.........................|......|....................
............-......-.../..................-.........../........................-............/|..../......./...
............/....../............................|....................\............|.........\..-......../...-|
.../\............./...............\........\\........|../....-...|.............................../..........-.
..........................................|/..............|-.|.........-.-............-.........../......-....
-\....|......................\............||.............\../..-....-.............-.../..................-....
......-..............|...|......./\............|................/................||...........................
................||.....-...|....................\..................\..../.\....\.....|.../.............|......
..-.|..............\.................................-.........../...............\......................|.....
.......-......|../.....................-...................................-......./......./............|..\..
...\................/..................................|...........................|...|............/.........
................\.........|................|......................-...|...............-......\..-/......-.....
....|..................\......\.|..../....................................\...........-........|..............
........|...../.......................|-...-.......-.......|./......./......................|...........\.....
\\-.................|..........|.\......|......--.......\.....|-..\................./............\........\...
.|..../.......\...|.|..............................\.....................|\..................../|...........|.
........\..............|.....|..\....-.....|....../...-.\.....\.........../.........-.....\......-.-..........
.-.......\.....|........\...|......./....|.\................|....|......./.....\/..-............|.............
../\/.........../...../..-..\....../.................--......../.........\......./.........\\.................
./....-...-......./...-...\-............|..............................-............-...\......|............./
........./.....\.....-.........................\./.........................|.....|............................
...-....\.............-.\..|/........./................../..|.................|.......|................\......
...|.............|.......\...................|......./.............-...........-............/...............\.
..........|..../.........|.......|.............\....-.....|\....|..-\....../............|...\...........\.....
......./\../.../|.-...........\\..../..........|......................./....-./.........\.....\..............\
......\......-................................-\..........................\.........................../..-....
.............................-.......-.......-.|...|/..................-.....-.....|.|......-.....-...........
..-.............\......../..../.............|............../......|......../....../...........................
\............|.....................................................|.............................../..........
...\.................|.-.......|....|......-..........................\.........|........................\...|
.|...........|......../..................-...../.\/.............-..|.......--...-.......|...../...|...........
.../....\....\......................|...............-.....|.............../.-..\......-......|................
.....-..-..../........-...\.../..-........................\...........................-.......................
-.........................\.................|...\.\...................|..........\-......-|...-...............
.................................................../.............-.-...................../................/.\.
...-./...\......................\..-./.................................|...\.......-............|.|..\......|.
./....../.....././.........................\...........-.............../.....|........................|..|.../
............\........-|../..........\-....\................|.|................\............../...............-
.....|..-................|......................|................../-..................../..\.................
.............|.................../......|..................|.../.....................-....|...................
............./.........................|.........--......|.-..|................-....................|.........
.-.....\....|\.../.............................................../........-......../.../..............|.......
.......|....-...|.............|...............|....\.|.........|......../...............|..|..................
...-....-...-.........--..\.........../|..-../......\/.......................|..-......||/..........|./.......
............./.................\..-....../........|........\.|................\...||......................\...
.\....................\..-....\...........||.................-....\.....-.............../.....\.../......../..
........|..-....................-...../..................\..-..-/.....|....\...|.-............|..........\....
.................../......\......................|.....././.............-.../.................\...............
.-..........\........./....|...............-/..................-....|......-.........................|.......-
-...............-....-........./.......\....../............./.-..............\.....\........-........-..|-....
................................--./........|.../........-.....|../.......................\....|.........|....
.|.........................-/..........................................|........................../.....\.../.
....-................................|......|......../|..|....../...-........./...../........./.......|./.....
.........../\.........../....../../.......\........|/.........-/..\/.................../......................
.........\................\.............-.....-.......\\|..\...\......\...............\/.....||...............
.\.............|............../.........................\.\...|.................../.......................\.|.
..................../.......|.............................\.......-..........-......-............./....\......
...................\.|.....................-.....-/-............\............../.............../.\.\..........
.....-...................|........|..........\...../.........................\.........|.......\...-..../.....
............................-...........\./....|./................\.............................-.....\..../..
.....\\|......-...\......................................./.........................\.../....\....../.........
...................-.|..................-..-......-.|.....\................/...................\..............
.|.........................\........................|...................................\.................\...
......|-...-.....................................|...\|...../............/.../.........|........\.............
\........\...............................\..\...|./............................-..-....-\.....................
./............../......./.-.............\.................\.........|.\....................../.............|.|
...........|......-.....-.....\...|...................................\..................................-....
................./................./...........|..../.............|..........\................/...............
.........\./..\..-......|........-.-.........-\../................................-....\.....|.........-......
....................\.......-.....\-/.\.......................................\............-...-..............
....................|..|/-....\............/../.........\..............|....\....\././....|...........\.......
........../......-.../..\...............|....................................../-......|..../...|..|\.........
.......\........../...\...................................-.........../.......................................
..|.............|............................/.......-.................\.-.................\...-......\.\.....
.....//\|...../\..|..................|.............\..-........./........../..................................
.|..............-../..............|...........|..............|......\...\.|.......|........-..............|.\.
.|......./.........-|.......\..-................../...\..\.....\./...\...............|..............\-........
...............\......-.......................\................./..................................\..........
......................................|/.|....................../........./......\...........-......\.\.......
..................................../........\....\...........-\..|........./..\.........../.\................
........\......./.......|.........................../.................-....|........./........./..............
....|-...........|............-...........................................-.|-..\............-..|.............
........./..\.\...............|..../....................\..............-.../....../..........-.....\.|........
.............................-.....|........................|./........................\./..\........../\.....
...|...........-........|...../..................|.././........../.............../....../..........-...../....
..-..........|....-.....|...../.././........|.|......................................./.-............|/.......
/.-............................../........../......\/...-.......\.....\.........\./.........-.................
.-.......|........-..........-...........|............................................/..........-.....-.....\
.-...../...............-........................................\...\.........-.........-.....\..........\....
......-...........\./......\../.............../........-...........|....|./...............................\...
........................|/\....../.....\....|............................../......................./........-.
...../..........\............./......\....................|.........-|.................../.\..................
...........\.|...|..\....-............|.|...........-........\-................-.............|.-..............