diff --git a/2023/day_16/input.txt b/2023/day_16/input.txt index b221142..1778ec0 100644 --- a/2023/day_16/input.txt +++ b/2023/day_16/input.txt @@ -1 +1,110 @@ -Paste any inputs into this file. \ No newline at end of file +\..../.....-...|........\.|...........-..........-......|....././..........\..|............................... +.../..|.......-............\...........-\|...............\.....|......-...........................-./.......|. +......../.-.-....\...\-.......................................|..........-.......\.....-.........|......|..... +....\........./..............|.............-..|.|.\.|.-......\........./..................|..........\........ +...|/.|..........................-........................|............|-..|...../\................./.../..... +...|.|.......\...................|/..........\\....|....................-..................|.................. +..................\-|..........|.\......|........\.....\...........................-..............\.....\..... +........./.-./..............................-..........|......|................/.............-......./...|./.. +....-...\..........-......\...............-/.....................\.........-\.........|..........|.....|...... +./............\......./..-......................../.|.-.|........................./.|......|...\.......-.-.... +...........././.............|..........-....../..............-.....\.-......-.............................-..\ +......................\.|.\..................-./.....\....|......................|..\...|..................... +...|.-...|.............../..//....-\...-.-...........|......-....\.|-.............\....-.|.............\...... +......|.....\-....\.............|../..|......\....--................/....|..../|../.....-........../.......... +.............|.........-.../.|....................\........................................................... +....\...\.-..-.....|.|...........-........./..../..........-.-....|....-..............-....|........|....../.. +..........|..............\..|........../..........................|.....|\.-...........-......\....../........ +...../.......|..../........-........|./\..\..\.\././.\.............--.......-..................\./......-..... +....................//...-..........-.............|................/............./.\....../...........-....... +.\-...|-/.......-........-...-...............\../...........................\/.....\...........\........|..... +.............................\-......|......\../......-..........|...........|.........-.....\..|......-...... +./................/........-.......|..\..../|............................|...........|.-.|.................... +.\.....|...............-\............|.................|...././.-...\..........|...........-...|...-.....\.... +.......-..../.........\|.....................|....../..|/./|..........|.......|-.......\\./......-............ +.....................\|......../-.............\...|...........................\.....\.\....................... +...|\.././...................|..................-.../\.-............\.-.........../............../.....\...... +.......\..................\..|../...........................\....\....\..../...\..\\./.-....................|. +...............|./...\..........|...|....\........./......\...\.............\.\-......|......-...-........|..| +.........-...........................-...-............\...\.\.................................-....|./........ +.-.|............-.....................-.................\..../......\....\....\....................|.......-.. +.....\.|......|..../................|......|.....-...\|../................./...................//............. +.|.........\..../......-........-............--...................../........|.......\.....\..........-....... +../.....|.|............-.................|\.....|....||...-..-....|/......................|.|-..-..........||. +...-............|.-.............-.....................\.|.............\.\............\...|........|...../.\... +\...........................\.|................................................/.|......-.......\........../.. +...................|....|.......|.-.............-..-\...-...............................|..................../ +/...../.....\............../...........|..|./...-.../...............................\........../|.........\... +..-.......|.........../.......|......../.......\.............../..........-|.................................. +...\.\........\............|......................\........|...\.............................................. +../..........--........|....-..............\..\.............\...................-........./...../..-.......... +..................|.................../\..-..|..................\....././..|......-.................../....... +-...\.............\..-..-.................../...|.|.........|../|./............/...../............../......... +.|\............................-....................|......./..............................|..-|...|.\........ +-........../...-....-.......................|...\........|.......\................../......................... +........................................................././.|..|.........\................/|........\........ +-.....\.........................|...........|..||..|......................................\...\....../.......| +....\...|........\.........................//...........|........-.........\..........\-.....\................ +....|........|.........../........../....\........|......-../..............|...............|.........../...... +......\................-...........|.................\...../........|................-........................ +...............|......|...\............\............/.../.......-.-../............................\......|.... +............-........-..../.......................\..-./.....\...................................-\.....|..... +...|...../......\...........\......\...\......./...........-../..|......................../...|............... +...........................-...../....-.....//................../..........-.......|.................|..-..... +................-...\.............\.../.../.\../...|.........-.\...../.................\.............../\...\. +.....-..................../......................................-...-............/...........|...-..........| +.........-..........\.\................-..\............................|...................-........./........ +...............-....../.....\...|.........\.../\.......-......|............\-.................|...-..........\ +.....\/......../../|......\......|.....--............|....../........-.../....-...-........................... +..../....\....|.............../..|........./............-.........................................../......... +.........../.................|....-..........................................................................\ +...-.....-./....................\..../.\\.................-.....-....|..\.....-....|.......................... +.....-..\..........|....|...........|.|....................................\.......-.../.../-.-........-.../.. +............../......./...-.......-..|.......-|..........\..............-.-....|..............\............... +-........../.........|.......................\....-........../.........\.................../.............-.... +.......-....\.....\..................\..../-.//.....-\..........|........................../....|.....\....... +..-./............./|..|........|...\\.........\...|......|.....\.........../.\......................\........\ +...../|.......\............../....................|.............-...........|......|....../.........../....-.. +.............-...-...|..\-.........................................|.......-.........|.........\.............. +......|......|............/.........................|.............\.............../....................|..-... +../../|\......|..//....../......................................................./-...................../..... +.....-........-................-.............|.-./..............|...-...../......................../..../..... +...........\......|.............|....|.|.........../........|....................\.\......./..\.\..|.......... +.\................/....................-..............................|...........\....\.......-....../.../... +...............|................-.....................................-..................\...............\.... +..........|\..................../............................./...............-.....-....-..|................\ +.................\.\...........................................-../......\.....-...............|...\.......... +..|....\............................................./.........|...-\.........-...\..|..........\............. +..................................|...........-....-./-.\.-..................-............................./.. +....................................\-.....\\...../..................-............-........../........-....\.. +...........\......./...../|\..../-....|........|.|-.....-....|.....||.....|...........\..........\....|.-..... +...\..........\./..............-........................././..........\././...-...........-............-...../ +\........\.....-..............................\................../.............-........................\..\.. +.........../.\...............-.\...-.....\............\....\......--.....\...................................\ +............\...............\.........\\....\....|......\.......-.................|...|....\.....\...\........ +.....|.....-............../............-............../.............../...................-......\...........- +..........................-...|......\.....\.......\........-................./..........-......../.../...-/.. +../........|....\........|..../...........|...-..|..........\............\..../........-................|.\... +..........-............./........................................-.......\.|/......./.................\.../.-. +......................\.........................../.../...../../.......|...................|............|..... +..............\..........-/........|/................................\...........................-............ +........\....\....../.......|.-........\........-..\.................................|.....\-................. +........\......|.\.................-..-.../.............\..................|.../.-.............-/............. +.|/.............-.\.........................|...|./........-...........\../...............\./.......-......\.. +..................-....|.....................-........|/.................\.........../..-.............../..\.. +......-......-.........../......................-......./...../.../.|........\.../....../............\.....|.. +...|../..................-.................\.....................\............-...........|.....|.|.........-| +..............................-...........|..-/.................|................................../.......... +.../......../.............-./.............|...../......./...|...........\..............-..........-....|..-./. +/..\...|...........|...........................|/................../......./......................\.....\..... +..|.........-........|..|......-...................\.........|...........................-.................... +../...-.....-.............../...|\...\.......|.................|.\.........-...................-\.........-./. +../..............................|.......|..............\...\......................\..........|............... +............/....|../....../.............-............................./......./.................\............ +/.....\...../.........../.............../...-.../\.....|.....|............|....|..........\.....\..../........ +....\....-/.........................|............-...........\........\...\.....\\................\........-/. +.....................-..-\...............././...........................\....-./......|..............\..-..... +................./........|.......-.-....................\.-.||...-.......\./.|./.....|...-.../......./....... +........-..-...../.......\.........................\.....././.\.../..|...../.................................. +.............|...............--.........../.|\/.....\.........................|..|.....................-...... +..........-...|.......\/...................|....../.-...-...|....................\..../.-...../..../..-....... \ No newline at end of file diff --git a/2023/day_16/part 1 solution.py b/2023/day_16/part 1 solution.py index ffa3d0b..a8313cc 100644 --- a/2023/day_16/part 1 solution.py +++ b/2023/day_16/part 1 solution.py @@ -1,6 +1,77 @@ -import os - +# Open and read the file with open(r'advent_of_code\2023\day_16\input.txt', 'r') as file: - input = file.read() + input_data = file.read() -print(input) +test_input = r'''.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|....''' + +# Assign the input data to a variable +data = input_data +#data = test_input + +# Split the input data into lines +lines = data.split('\n') + +# Create a grid from the lines +grid = [[char for char in row] for row in lines] + +# Get the number of rows and columns in the grid +num_rows = len(grid) +num_columns = len(grid[0]) + +# Define the directions for row and column +direction_row = [-1, 0, 1, 0] +direction_column = [0, 1, 0, -1] + +# Function to calculate the next step +def calculate_next_step(row, column, direction): + return (row + direction_row[direction], column + direction_column[direction], direction) + +# Function to calculate the score +def calculate_score(start_row, start_column, start_direction): + positions = [(start_row, start_column, start_direction)] + seen_positions = set() + seen_positions_2 = set() + + while True: + new_positions = [] + if not positions: + break + for (row, column, direction) in positions: + if 0 <= row < num_rows and 0 <= column < num_columns: + seen_positions.add((row, column)) + if (row, column, direction) in seen_positions_2: + continue + seen_positions_2.add((row, column, direction)) + char = grid[row][column] + if char == '.': + new_positions.append(calculate_next_step(row, column, direction)) + elif char == '/': + new_positions.append(calculate_next_step(row, column, {0: 1, 1: 0, 2: 3, 3: 2}[direction])) + elif char == '\\': + new_positions.append(calculate_next_step(row, column, {0: 3, 1: 2, 2: 1, 3: 0}[direction])) + elif char == '|': + if direction in [0, 2]: + new_positions.append(calculate_next_step(row, column, direction)) + else: + new_positions.append(calculate_next_step(row, column, 0)) + new_positions.append(calculate_next_step(row, column, 2)) + elif char == '-': + if direction in [1, 3]: + new_positions.append(calculate_next_step(row, column, direction)) + else: + new_positions.append(calculate_next_step(row, column, 1)) + new_positions.append(calculate_next_step(row, column, 3)) + positions = new_positions + return len(seen_positions) + +score = calculate_score(0,0,1) +print(score) diff --git a/2023/day_16/part 2 solution.py b/2023/day_16/part 2 solution.py index ffa3d0b..9756f52 100644 --- a/2023/day_16/part 2 solution.py +++ b/2023/day_16/part 2 solution.py @@ -1,6 +1,103 @@ -import os - +# Open and read the file with open(r'advent_of_code\2023\day_16\input.txt', 'r') as file: - input = file.read() + input_data = file.read() -print(input) +test_input = r'''.|...\.... +|.-.\..... +.....|-... +........|. +.......... +.........\ +..../.\\.. +.-.-/..|.. +.|....-|.\ +..//.|....''' + +# Assign the input data to a variable +data = input_data +#data = test_input + +# Split the input data into lines +lines = data.split('\n') + +# Create a grid from the lines +grid = [[char for char in row] for row in lines] + +# Get the number of rows and columns in the grid +num_rows = len(grid) +num_columns = len(grid[0]) + +# Define the directions for row and column +direction_row = [-1, 0, 1, 0] +direction_column = [0, 1, 0, -1] + +# Function to calculate the next step +def calculate_next_step(row, column, direction): + return (row + direction_row[direction], column + direction_column[direction], direction) + +# Function to calculate the score +def calculate_score(start_row, start_column, start_direction): + positions = [(start_row, start_column, start_direction)] + seen_positions = set() + seen_positions_2 = set() + + while True: + new_positions = [] + if not positions: + break + for (row, column, direction) in positions: + if 0 <= row < num_rows and 0 <= column < num_columns: + seen_positions.add((row, column)) + if (row, column, direction) in seen_positions_2: + continue + seen_positions_2.add((row, column, direction)) + char = grid[row][column] + if char == '.': + new_positions.append(calculate_next_step(row, column, direction)) + elif char == '/': + new_positions.append(calculate_next_step(row, column, {0: 1, 1: 0, 2: 3, 3: 2}[direction])) + elif char == '\\': + new_positions.append(calculate_next_step(row, column, {0: 3, 1: 2, 2: 1, 3: 0}[direction])) + elif char == '|': + if direction in [0, 2]: + new_positions.append(calculate_next_step(row, column, direction)) + else: + new_positions.append(calculate_next_step(row, column, 0)) + new_positions.append(calculate_next_step(row, column, 2)) + elif char == '-': + if direction in [1, 3]: + new_positions.append(calculate_next_step(row, column, direction)) + else: + new_positions.append(calculate_next_step(row, column, 1)) + new_positions.append(calculate_next_step(row, column, 3)) + positions = new_positions + return len(seen_positions) + +score = calculate_score(0,0,1) +print(score) + +# Initialize the maximum score +max_score = 0 + +# Iterate over each row +for row in range(num_rows): + # Calculate the score for the current row starting from the first column moving right + score_from_start = calculate_score(row, 0, 1) + # Calculate the score for the current row starting from the last column moving left + score_from_end = calculate_score(row, num_columns - 1, 3) + + # Update the maximum score + max_score = max(max_score, score_from_start, score_from_end) + +# Iterate over each column +for column in range(num_columns): + # Calculate the score for the current column starting from the first row moving down + score_from_top = calculate_score(0, column, 2) + # Calculate the score for the current column starting from the last row moving up + score_from_bottom = calculate_score(num_rows - 1, column, 0) + + # Update the maximum score + max_score = max(max_score, score_from_top, score_from_bottom) + +# Print the maximum score +print(max_score) \ No newline at end of file diff --git a/2023/day_16/puzzle_text.md b/2023/day_16/puzzle_text.md index 8d95565..11646b4 100644 --- a/2023/day_16/puzzle_text.md +++ b/2023/day_16/puzzle_text.md @@ -1 +1,80 @@ -# Day 16 Puzzle Text. \ No newline at end of file + +

--- 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:

+ +

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 8901.

The first half of this puzzle is complete! It provides one gold star: *

+

--- 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?