From b4b7a5ae9ed05653dfa11c792de87de4d508884e Mon Sep 17 00:00:00 2001 From: Jake Pullen Date: Mon, 18 Dec 2023 08:09:53 +0000 Subject: [PATCH] day 18 done --- 2023/18/part 1 solution.py | 62 ++++++++++++++++++++++++++++++++++++++ 2023/18/part 2 solution.py | 61 +++++++++++++++++++++++++++++++++++++ get_puzzle_text.py | 13 +++++--- 3 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 2023/18/part 1 solution.py create mode 100644 2023/18/part 2 solution.py diff --git a/2023/18/part 1 solution.py b/2023/18/part 1 solution.py new file mode 100644 index 0000000..4d8a45d --- /dev/null +++ b/2023/18/part 1 solution.py @@ -0,0 +1,62 @@ +with open(r'advent_of_code\2023\18\input.txt', 'r') as file: + input = file.read() + +test_input = '''R 6 (#70c710) +D 5 (#0dc571) +L 2 (#5713f0) +D 2 (#d2c081) +R 2 (#59c680) +D 2 (#411b91) +L 5 (#8ceee2) +U 2 (#caa173) +L 1 (#1b58a2) +U 2 (#caa171) +R 2 (#7807d2) +U 3 (#a77fa3) +L 2 (#015232) +U 2 (#7a21e3)''' + +# input = test_input +input = input.split('\n') + +# Initialize a list of points with the origin (0, 0) +points = [(0, 0)] + +# Define the directions for "Up", "Down", "Left", "Right" +directions = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)} + +# Initialize a variable to keep track of the total distance moved +total_distance = 0 + +# Read each line from the input +for line in input: + # Split the line into direction, number of steps, and ignore the rest + direction, num_steps, _ = line.split() + + # Get the direction vector for the current direction + direction_vector = directions[direction] + + # Convert the number of steps to an integer + num_steps = int(num_steps) + + # Add the number of steps to the total distance + total_distance += num_steps + + # Get the last point we moved to + last_point = points[-1] + + # Calculate the new point after moving in the current direction + new_point = (last_point[0] + direction_vector[0] * num_steps, last_point[1] + direction_vector[1] * num_steps) + + # Add the new point to the list of points + points.append(new_point) + +# Calculate the area of the polygon formed by the points +area = abs(sum(points[i][0] * (points[i - 1][1] - points[(i + 1) % len(points)][1]) for i in range(len(points)))) // 2 + +# Calculate the number of interior points +interior_points = area - total_distance // 2 + 1 + +#print(points) +# Print the total number of points covered +print(interior_points + total_distance) \ No newline at end of file diff --git a/2023/18/part 2 solution.py b/2023/18/part 2 solution.py new file mode 100644 index 0000000..e53984a --- /dev/null +++ b/2023/18/part 2 solution.py @@ -0,0 +1,61 @@ +with open(r'advent_of_code\2023\18\input.txt', 'r') as file: + input = file.read() + +test_input = '''R 6 (#70c710) +D 5 (#0dc571) +L 2 (#5713f0) +D 2 (#d2c081) +R 2 (#59c680) +D 2 (#411b91) +L 5 (#8ceee2) +U 2 (#caa173) +L 1 (#1b58a2) +U 2 (#caa171) +R 2 (#7807d2) +U 3 (#a77fa3) +L 2 (#015232) +U 2 (#7a21e3)''' + +# input = test_input +input = input.split('\n') + +# Initialize a list of points with the origin (0, 0) +points = [(0, 0)] + +# Define the directions for "Up", "Down", "Left", "Right" +directions = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)} + +# Initialize a variable to keep track of the total distance moved +total_distance = 0 + +# Read each line from the input +for line in input: + # Ignore the first two parts of the line, get the third part + _, _, third_part = line.split() + + # Remove the first two characters and the last character from the third part + third_part = third_part[2:-1] + + # Get the direction and number of steps from the third part + direction, num_steps = directions["RDLU"[int(third_part[-1])]], int(third_part[:-1], 16) + + # Add the number of steps to the total distance + total_distance += num_steps + + # Get the last point we moved to + last_point = points[-1] + + # Calculate the new point after moving in the current direction + new_point = (last_point[0] + direction[0] * num_steps, last_point[1] + direction[1] * num_steps) + + # Add the new point to the list of points + points.append(new_point) + +# Calculate the area of the polygon formed by the points +area = abs(sum(points[i][0] * (points[i - 1][1] - points[(i + 1) % len(points)][1]) for i in range(len(points)))) // 2 + +# Calculate the number of interior points +interior_points = area - total_distance // 2 + 1 + +# Print the total number of points covered +print(interior_points + total_distance) diff --git a/get_puzzle_text.py b/get_puzzle_text.py index b349a62..88f3a5d 100644 --- a/get_puzzle_text.py +++ b/get_puzzle_text.py @@ -86,11 +86,16 @@ def create_solution_file(year, day, part): os.makedirs(folder, exist_ok=True) solution_file = os.path.join(folder, f"part {part} solution.py") input_file_path = os.path.join(folder, "input.txt") - with open(solution_file, "w") as file: - file.write(f"""with open(r'{input_file_path}', 'r') as file: + + # Check if the solution file already exists + if not os.path.exists(solution_file): + with open(solution_file, "w") as file: + file.write(f"""with open(r'{input_file_path}', 'r') as file: input = file.read() """) - print(f"Created {solution_file}") + print(f"Created {solution_file}") + else: + print(f"{solution_file} already exists. No action taken.") # Usage example @@ -125,4 +130,4 @@ def populate_data(year = current_year, day=current_day): else: print("Something went wrong. Check the puzzle text.") -populate_data(2023,1) +populate_data(2023,17)