From a34f05d1afcb24af242ecd067e1ab4313429e2ea Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sat, 16 Dec 2023 10:17:04 -0600 Subject: [PATCH] 2023 Day 16 Complete! --- 2023/day16/input | 110 +++++++++++++++++++++++ 2023/day16/main.go | 124 ++++++++++++++++++++++++++ 2023/day16/problem | 203 +++++++++++++++++++++++++++++++++++++++++++ 2023/day16/testinput | 10 +++ 4 files changed, 447 insertions(+) create mode 100644 2023/day16/input create mode 100644 2023/day16/main.go create mode 100644 2023/day16/problem create mode 100644 2023/day16/testinput diff --git a/2023/day16/input b/2023/day16/input new file mode 100644 index 0000000..2b0a13f --- /dev/null +++ b/2023/day16/input @@ -0,0 +1,110 @@ +\..|...................|........\..............\.................-......./........./...\..........-......./... +........../...-/......\.|............../.................\...........\........-.........|.....\.-.|........... +...............-...........|......|...|......../.......\............../.................................../... +..................................|............../..|.|.../............./..\\........|...|..............\..|.. +...........................-...-...-.......................-.....-.-....\.......-............................/ +........-...|.......|./../......................./.........................\.................../.....\........ +\..\............-.\.........|../................................\....\......................./.............-.. +|\...|....\................\...|........\-..............|............\.........\.\..|.|....................... +.....\.........\......|...-...............................-.............-../.../..--......\.....\............. +................|...........................|..........\...........././.................../............../.... +.............................\......\.../..|................---................-......\....|...-............-. +..........|........./......\|...|.......|........\......\....-....\...-...../................/..-..../......-. +...|............./.................\../....|...\../.................|...|..|......|........................... +..................................-......../.....|............................................................ +...................-.............|...-.........../...........................-..............-...............|. +..-.............../.............................-......-.....-.............|-...../....................../.... +....||...../.........................................-...........................................--./-.......\ +...................\..........-......./..............|\........................../.-.|..............-..-/...|. +.-............-............../..-........../.......................\...-...||........./.-.....-....../........ +.......-.......-........................................../\\.......|......./../..\........\..-.........../../ +....../../.......................\.........\-....\.|..................\......................./...-\.......... +\.......-........\...-.....................\....-........./-...|......|.......-........................\...\.. +...|\.................................../..../..........|......-......../-..|......./..........\../..|......-\ +..........\...........................|..-........-/-......../....|...............................\.....|..... +..............\......-............................-.../........|........................./........-........... +.............-..\.//.....................-....-......|....../....-............................/..............| +\........../....|................./.....\......\.../.................|........\............../................ +.....-........-/|.........\.....-./............|...\.|......|.......|...................\\.....\.............- +................|.-...-.....................|..........-....\....\......../...............................././ +...........|.......\..............\......................................./........-.......................... +...........................-..../............|............................|...........\....................... +....................|..........|..................../...........\.....|.........-............\.\...-..\....... +................................-......-......\.....-/......\...\................/................-........... +..|.....\..\......|../....................................................\\......-...../..../..............// +.........\.................\..\..-.-..................\.....\..\.....\..........-............................. +...................\............/..\............|.............../..\............................|\.\...../.... +......|...............\.......|......|/........|....-.......|...........-.../.\....\.....|./.......|.......... +....\/|................-.-..........|.................../..............|../-...|/..........|.................. +..|........./-\.............|...|............|......\...................\..../.........|...............|.-.... +...................-................\....|/....-...|......./....../..........-|............/......\../..\\.... +...........................-..........................................-...|.|................\../...........-. +......../.....|.........-.......|........./-............|../.......|......-.-.-.......................-..|-... +.........|-...............|............./.....|........../..|........\..........|/.............../-........... +...\.....|..|...........\....-...\/.........................................|...................\......-.-.... +.../...-...|.............|.....\...|.................................../..............\.................../... +..........-.........../....|.....|..-.........../.................................-..................\....\/.. +....../..............\../......../.....-./....|../.\....-.................../............................../.\ +......-.|..........................\...............|.......-...-....../.............//...\|................/.. +................/.........|........................|......./..|......../-...........................\....|.... +........../...|.........\...........-./......\\...........\......|-......|...\...............\................ +../.........................|.................-/...........--.........\.../........................\.........\ +........\..|...|.............../...................-.........|.................\..|......./.........-.....-|.. +\.../.......|.............|-.../.................\...\..........\...|..........\....../......-................ +............|.|....\..--............-/-..................../........................-../..\..-................ +.....................................|.................../.................................|..../..-......\... +....|..............|..-.....|../.........................................|........\.................|......... +..............\..|...............\....||........../................................\................./.\...... +.....................-../......./....../.................................-............\........../............ +.....|.............../......-.\.|........-...................|.....-........\...-.............-....-.......-.. +..../.....|..............\../..............................\........|....................|..............\..... +....\................-........\../......|..../......................-.....|............../.../........../..... +....-...........................-.............|...........\..|........|.......\././.....|./..........\........ +/....................\..............\.............................-......................../\......../....-... +......\..................-............................................./.\...../.............\....-....\.\.... +..................................\-............................/.......|........................-|........... +.................../.../.......|./............\.........\....-.-....-../\.........|........./...\............. +......./.../....|...../|......................|.....|.....|.........|...........\......-.\..................|. +..././...........................|.......\.|......-........./................................................. +........|.|.......................-....................././.......||...........|/....-....\.......|......\/... +.\.\.....|....\......-....|...|.....//........|..-.........-.................-........./...................... +.|.....................\............-.................................|..../.............-....../.........../. +.|..\..../...............\..................../...\............................-....../................../.|.. +./.\............................/.............-...../-................\.........\.............|...../......... +..........\...............................\............../.........\.....-/........./|........................ +.............\..|....|..................................\.........|./......................\.........-........ +../.....|-............|...\................|...-....-........./........|.-..........\......................... +........-............./...........--................../...............................|..|.\....\............. +\.............-\.........................-|......|/.........\............/...........-.../.\........\..\.../.. +......................|........\....-../.............|......|.-...................................-|../...-... +.............|...|.....-...........|.....-...............|...\........................./..\....\.............. +..............|.............-.........../....-..................................|......./........|............ +...........|.-..../......................................\....-...\-......../........-..........-......-.....| +../...........-............-.../|...........-.-.............\.............................|.............-..... +.........../...-............../........./...................................../......-.......\\............... +........................./.||......|...../.......|....|..................-..........|......\.................. +............/....-./..-...........|...-..........\....../...\/.......|\.....\....|........../..\.\............ +.......................................-.../.......|............|........|...\..........\.....-............... +/..\|.............-...-.................................\........\...................\......|....-............ +..................\............../........................./.........|..........\...\......\..........|......| +.../.................................................../...|............../....\...\...../........|.......-... +..................-.-..........................\.................-......................-..\.........-........ +...................../\.....\.............\.............../................\.|............../........./....... +....................|../.\./......................................-.../.......\............................... +|........................-..............................................|...............................-..... +....\.\...........\......|.........\/|..|....|.....\............-...../.............................\\........ +......\....|......\|.....-..................\........\.......................-............-.|..............|.. +....|.....././.......................|......../...........-.|............./......-.|....../.....\......|....\. +.........\........|.......\..........|........./..................\.........................|................- +.....\..........\.....-......../...|-........../...\/|........................................................ +.../.....................\../.........\.|.........-..........\...............................|...............\ +.........-...........................................-.|...........\......//............../..|.\............/. +.-|../...../.........|............./..........................|..-.\......\............\................../... +../....../.................|..............\.........|.\......\...../.......\............\./.......|....../..\. +......../.-..-......\.........................\|...................................-....../................... +......................-....|........./\................|......\..-\....../.......\.................\/.-\...... +............\...|...............|............./...........................................\....-.-............ +\.........|.............................|............/.........-.....\........\|...-..-..\/.../......./....... +.............................\.|...............\....\..\-.....................\......-...../......\..../.....\ +..\-......\.\........-........../........\...............\..................../....-....\.\.|.|............... +................|./........../.................................-.............|...................\............ diff --git a/2023/day16/main.go b/2023/day16/main.go new file mode 100644 index 0000000..29aee43 --- /dev/null +++ b/2023/day16/main.go @@ -0,0 +1,124 @@ +package main + +import ( + "fmt" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) + fmt.Println() + part2(inp) +} + +var ( + N = h.Coordinate{X: 0, Y: -1} + E = h.Coordinate{X: 1, Y: 0} + S = h.Coordinate{X: 0, Y: 1} + W = h.Coordinate{X: -1, Y: 0} +) + +// movement describes how a beam moving in a direction should react +// to a byte +var movement = map[h.Coordinate]map[byte][]h.Coordinate{ + N: { + '|': {N}, + '-': {W, E}, + '\\': {W}, + '/': {E}, + '.': {N}, + }, + E: { + '|': {N, S}, + '-': {E}, + '\\': {S}, + '/': {N}, + '.': {E}, + }, + S: { + '|': {S}, + '-': {W, E}, + '\\': {E}, + '/': {W}, + '.': {S}, + }, + W: { + '|': {N, S}, + '-': {W}, + '\\': {N}, + '/': {S}, + '.': {W}, + }, +} + +var tileHistory map[h.Coordinate][]h.Coordinate + +func part1(input []string) { + mm := h.StringSliceToCoordByteMap(input) + tileHistory = make(map[h.Coordinate][]h.Coordinate) + proc(&mm, E, h.Coordinate{X: 0, Y: 0}) + + fmt.Println("# Part 1") + fmt.Println(len(tileHistory)) +} + +func part2(input []string) { + mm := h.StringSliceToCoordByteMap(input) + + maxTiles := h.MIN_INT + // starts are a map of positions -> directions to test as a starting point + starts := make(map[*h.Coordinate]h.Coordinate) + for y := mm.TLY; y <= mm.BRY; y++ { + for x := mm.TLX; x <= mm.BRX; x++ { + if y != mm.TLY && x != mm.TLX && y != mm.BRY && x != mm.BRX { + continue + } + if y == mm.TLY { + // South facing beams + starts[&h.Coordinate{Y: y, X: x}] = S + } else if y == mm.BRY { + // North facing beams + starts[&h.Coordinate{Y: y, X: x}] = N + } + if x == mm.TLX { + // East facing beams + starts[&h.Coordinate{Y: y, X: x}] = E + } + if x == mm.BRX { + // West facing beams + starts[&h.Coordinate{Y: y, X: x}] = W + } + } + } + for pos, dir := range starts { + tileHistory = make(map[h.Coordinate][]h.Coordinate) + proc(&mm, dir, *pos) + if len(tileHistory) > maxTiles { + maxTiles = len(tileHistory) + } + } + + fmt.Println("# Part 2") + fmt.Println(maxTiles) +} + +func proc(mm *h.CoordByteMap, dir, pos h.Coordinate) { + if !mm.ContainsCoord(pos) { + return + } + if visits, ok := tileHistory[pos]; ok { + for _, v := range visits { + if v == dir { + return + } + } + } + curr := mm.Get(pos) + tileHistory[pos] = append(tileHistory[pos], dir) + next := movement[dir][curr] + for _, n := range next { + proc(mm, n, pos.Add(n)) + } +} diff --git a/2023/day16/problem b/2023/day16/problem new file mode 100644 index 0000000..3dee1c0 --- /dev/null +++ b/2023/day16/problem @@ -0,0 +1,203 @@ + [1]Advent of Code + + • [2][About] + • [3][Events] + • [4][Shop] + • [5][Settings] + • [6][Log Out] + + br0xen [7](AoC++) 32* + +        λy.[8]2023 + + • [9][Calendar] + • [10][AoC++] + • [11][Sponsors] + • [12][Leaderboard] + • [13][Stats] + + Our [14]sponsors help make Advent of Code possible: + [15]JPMorgan Chase - With over 57,000 technologists globally and an annual + tech spend of $15 billion, JPMorgan Chase is dedicated to improving the + design, analytics, coding and testing that goes into creating high quality + software products. + +--- 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? + + Your puzzle answer was 6816. + +--- Part Two --- + + As you try to work out what might be wrong, the reindeer tugs on your + shirt and leads you to a nearby control panel. There, a collection of + buttons lets you align the contraption so that the beam enters from any + edge tile and heading away from that edge. (You can choose either of two + directions for the beam if it starts on a corner; for instance, if the + beam starts in the bottom-right corner, it can start heading either left + or upward.) + + So, the beam could start on any tile in the top row (heading downward), + any tile in the bottom row (heading upward), any tile in the leftmost + column (heading right), or any tile in the rightmost column (heading + left). To produce lava, you need to find the configuration that energizes + as many tiles as possible. + + In the above example, this can be achieved by starting the beam in the + fourth tile from the left in the top row: + + .|<2<\.... + |v-v\^.... + .v.v.|->>> + .v.v.v^.|. + .v.v.v^... + .v.v.v^..\ + .v.v/2\\.. + <-2-/vv|.. + .|<<<2-|.\ + .v//.|.v.. + + Using this configuration, 51 tiles are energized: + + .#####.... + .#.#.#.... + .#.#.##### + .#.#.##... + .#.#.##... + .#.#.##... + .#.#####.. + ########.. + .#######.. + .#...#.#.. + + Find the initial beam configuration that energizes the largest number of + tiles; how many tiles are energized in that configuration? + + Your puzzle answer was 8163. + + Both parts of this puzzle are complete! They provide two gold stars: ** + + At this point, you should [16]return to your Advent calendar and try + another puzzle. + + If you still want to see it, you can [17]get your puzzle input. + + You can also [Shareon [18]Twitter [19]Mastodon] this puzzle. + +References + + Visible links + 1. https://adventofcode.com/ + 2. https://adventofcode.com/2023/about + 3. https://adventofcode.com/2023/events + 4. https://teespring.com/stores/advent-of-code + 5. https://adventofcode.com/2023/settings + 6. https://adventofcode.com/2023/auth/logout + 7. Advent of Code Supporter + https://adventofcode.com/2023/support + 8. https://adventofcode.com/2023 + 9. https://adventofcode.com/2023 + 10. https://adventofcode.com/2023/support + 11. https://adventofcode.com/2023/sponsors + 12. https://adventofcode.com/2023/leaderboard + 13. https://adventofcode.com/2023/stats + 14. https://adventofcode.com/2023/sponsors + 15. https://www.jpmorgan.com/ + 16. https://adventofcode.com/2023 + 17. https://adventofcode.com/2023/day/16/input + 18. https://twitter.com/intent/tweet?text=I%27ve+completed+%22The+Floor+Will+Be+Lava%22+%2D+Day+16+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F16&related=ericwastl&hashtags=AdventOfCode + 19. javascript:void(0); diff --git a/2023/day16/testinput b/2023/day16/testinput new file mode 100644 index 0000000..d6805ce --- /dev/null +++ b/2023/day16/testinput @@ -0,0 +1,10 @@ +.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|....