solve Day18 in Rust

This commit is contained in:
2023-12-18 18:05:55 +01:00
parent 65424cea56
commit bfea3d7501
5 changed files with 130 additions and 20 deletions

View File

@@ -1,7 +1,7 @@
def parse_instructions(file_path):
"""Parse instructions from the given file."""
try:
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
instructions = [line.strip().split() for line in file.readlines()]
print(f"Parsed {len(instructions)} instructions from {file_path}")
return instructions
@@ -9,16 +9,18 @@ def parse_instructions(file_path):
print(f"Error reading file {file_path}: {e}")
raise
def apply_instruction(grid, position, direction, steps):
"""Apply a single instruction to the grid and update the position."""
deltas = {'U': (0, -1), 'D': (0, 1), 'L': (-1, 0), 'R': (1, 0)}
deltas = {"U": (0, -1), "D": (0, 1), "L": (-1, 0), "R": (1, 0)}
dx, dy = deltas[direction]
for _ in range(steps):
position = (position[0] + dx, position[1] + dy)
grid.add(position)
#print(f"Moved {direction} to {position}")
# print(f"Moved {direction} to {position}")
return position
def create_path(instructions):
"""Create the path based on the instructions."""
grid = set()
@@ -29,29 +31,37 @@ def create_path(instructions):
direction, steps = instruction[0], int(instruction[1])
print(f"Applying instruction: {direction} {steps}")
position = apply_instruction(grid, position, direction, steps)
return grid
def flood_fill(grid, bounds):
"""Perform flood-fill to find cells outside the path."""
filled = set()
to_fill = [(bounds[0], y) for y in range(bounds[2], bounds[3] + 1)] \
+ [(bounds[1], y) for y in range(bounds[2], bounds[3] + 1)] \
+ [(x, bounds[2]) for x in range(bounds[0], bounds[1] + 1)] \
+ [(x, bounds[3]) for x in range(bounds[0], bounds[1] + 1)]
to_fill = (
[(bounds[0], y) for y in range(bounds[2], bounds[3] + 1)]
+ [(bounds[1], y) for y in range(bounds[2], bounds[3] + 1)]
+ [(x, bounds[2]) for x in range(bounds[0], bounds[1] + 1)]
+ [(x, bounds[3]) for x in range(bounds[0], bounds[1] + 1)]
)
while to_fill:
x, y = to_fill.pop()
if (x, y) in filled or (x, y) in grid:
continue
filled.add((x, y))
if x > bounds[0]: to_fill.append((x - 1, y))
if x < bounds[1]: to_fill.append((x + 1, y))
if y > bounds[2]: to_fill.append((x, y - 1))
if y < bounds[3]: to_fill.append((x, y + 1))
if x > bounds[0]:
to_fill.append((x - 1, y))
if x < bounds[1]:
to_fill.append((x + 1, y))
if y > bounds[2]:
to_fill.append((x, y - 1))
if y < bounds[3]:
to_fill.append((x, y + 1))
return filled
def calculate_area(grid):
"""Calculate the area inside the loop."""
min_x = min(grid, key=lambda x: x[0])[0]
@@ -66,6 +76,7 @@ def calculate_area(grid):
return inside_area
def run_test(test_file):
"""Run the algorithm with test input."""
try:
@@ -79,6 +90,7 @@ def run_test(test_file):
print(f"Assertion Error: {e}")
return False
def main():
"""Main function to run the puzzle solution."""
test_file = "../test.txt"
@@ -97,5 +109,6 @@ def main():
else:
print("Test failed. Halting execution.")
if __name__ == "__main__":
main()
main()

View File

@@ -1,7 +1,7 @@
def parse_instructions(file_path):
"""Parse instructions from the given file."""
try:
with open(file_path, 'r') as file:
with open(file_path, "r") as file:
lines = file.read().splitlines()
print(f"Parsed {len(lines)} instructions from {file_path}")
return lines
@@ -9,6 +9,7 @@ def parse_instructions(file_path):
print(f"Error reading file {file_path}: {e}")
raise
def calculate_area(lines):
"""Calculate the area inside the loop defined by the instructions."""
DIRECTIONS = [(0, 1), (1, 0), (0, -1), (-1, 0)]
@@ -24,18 +25,29 @@ def calculate_area(lines):
row, column = points[-1]
points.append((row + dr * steps, column + dc * steps))
area = abs(
sum(x1 * y2 - x2 * y1 for (x1, y1), (x2, y2) in zip(points, points[1:] + points[:1]))
) // 2 + boundary // 2 + 1
area = (
abs(
sum(
x1 * y2 - x2 * y1
for (x1, y1), (x2, y2) in zip(points, points[1:] + points[:1])
)
)
// 2
+ boundary // 2
+ 1
)
return area
def run_test(test_file, expected_result):
"""Run the algorithm with test input and compare with the expected result."""
try:
lines = parse_instructions(test_file)
calculated_area = calculate_area(lines)
assert calculated_area == expected_result, f"Test failed, expected {expected_result}, got {calculated_area}"
assert (
calculated_area == expected_result
), f"Test failed, expected {expected_result}, got {calculated_area}"
print(f"Test passed, area: {calculated_area}")
except AssertionError as e:
print(e)
@@ -44,11 +56,14 @@ def run_test(test_file, expected_result):
print(f"Error during test execution: {e}")
raise
def main():
"""Main function to run the puzzle solution."""
test_file = "../test.txt"
input_file = "../input.txt"
expected_test_area = 952408144115 # Replace with the correct expected area for the test
expected_test_area = (
952408144115 # Replace with the correct expected area for the test
)
# Run test
try:
@@ -61,5 +76,6 @@ def main():
except Exception as e:
print(f"Execution halted due to error: {e}")
if __name__ == "__main__":
main()