day 6 done. im sure part 2 could be done better
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
testing = 0
|
||||
if not testing:
|
||||
with open(r"2024/06\input.txt", "r") as file:
|
||||
input = file.read()
|
||||
else:
|
||||
input = """....#.....
|
||||
.........#
|
||||
..........
|
||||
..#.......
|
||||
.......#..
|
||||
..........
|
||||
.#..^.....
|
||||
........#.
|
||||
#.........
|
||||
......#..."""
|
||||
|
||||
OBSTACLE = "#"
|
||||
# first we should turn the input into a list of lists so we can use grid references
|
||||
|
||||
input = input.split("\n")
|
||||
grid = [list(x) for x in input]
|
||||
|
||||
# for x in grid:
|
||||
# print(x)
|
||||
|
||||
# we need to find the starting position
|
||||
# our starting position is the only '^' in the grid
|
||||
for x in range(len(grid)):
|
||||
for y in range(len(grid[x])):
|
||||
if grid[x][y] == "^":
|
||||
position = (x, y)
|
||||
break
|
||||
print('start at',position)
|
||||
|
||||
# We start by travelling north until we hit an obstacle '#'
|
||||
# then we turn right and travel east until we hit an obstacle
|
||||
# we keep turning right and travelling until we hit an obstacle
|
||||
# once we make it off the grid we stop
|
||||
# count how many unique squares we visit
|
||||
|
||||
def count_unique_squares(grid, start):
|
||||
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] # North, East, South, West
|
||||
direction_index = 0 # Start by moving north
|
||||
x, y = start
|
||||
visited = set()
|
||||
|
||||
while True:
|
||||
visited.add((x, y))
|
||||
dx, dy = directions[direction_index]
|
||||
new_x, new_y = x + dx, y + dy
|
||||
# if new position is off the grid, we stop
|
||||
if (new_x < 0 or new_x >= len(grid) or new_y < 0 or new_y >= len(grid[0])):
|
||||
print(f"Moved off the grid to ({new_x}, {new_y}), stopping")
|
||||
break
|
||||
# if we hit an obstacle, we turn right
|
||||
elif grid[new_x][new_y] == OBSTACLE:
|
||||
direction_index = (direction_index + 1) % 4 # Turn right
|
||||
print(f"Hit obstacle at ({new_x}, {new_y}), turning right to direction {directions[direction_index]}")
|
||||
|
||||
else:
|
||||
x, y = new_x, new_y
|
||||
print(f"Moved to ({x}, {y})")
|
||||
|
||||
return len(visited)
|
||||
|
||||
print(count_unique_squares(grid, position))
|
||||
@@ -0,0 +1,74 @@
|
||||
testing = 0
|
||||
if not testing:
|
||||
with open(r"2024/06\input.txt", "r") as file:
|
||||
input = file.read()
|
||||
else:
|
||||
input = """....#.....
|
||||
.........#
|
||||
..........
|
||||
..#.......
|
||||
.......#..
|
||||
..........
|
||||
.#..^.....
|
||||
........#.
|
||||
#.........
|
||||
......#..."""
|
||||
|
||||
OBSTACLE = "#"
|
||||
# first we should turn the input into a list of lists so we can use grid references
|
||||
|
||||
split_input = input.split("\n")
|
||||
grid = [list(x) for x in split_input]
|
||||
ROWS = len(grid)
|
||||
COLUMNS = len(grid[0])
|
||||
|
||||
# for x in grid:
|
||||
# print(x)
|
||||
|
||||
# we need to find the starting position
|
||||
# our starting position is the only '^' in the grid
|
||||
for x in range(len(grid)):
|
||||
for y in range(len(grid[x])):
|
||||
if grid[x][y] == "^":
|
||||
position = (x, y)
|
||||
break
|
||||
#print('start at',position)
|
||||
|
||||
# We start by travelling north until we hit an obstacle '#'
|
||||
# then we turn right and travel east until we hit an obstacle
|
||||
# we keep turning right and travelling until we hit an obstacle
|
||||
# once we make it off the grid we stop
|
||||
# count how many unique squares we visit
|
||||
|
||||
loops = 0
|
||||
for row in range(ROWS):
|
||||
for col in range(COLUMNS):
|
||||
directions = [(-1, 0), (0, 1), (1, 0), (0, -1)] # North, East, South, West
|
||||
direction_index = 0 # Start by moving north
|
||||
x, y = position
|
||||
visited = set()
|
||||
visited_with_direction = set() # used for loop detection
|
||||
while True:
|
||||
visited.add((x, y))
|
||||
if (x, y, direction_index) in visited_with_direction:
|
||||
#print(f"Loop detected at ({x}, {y}) in direction {directions[direction_index]}")
|
||||
loops += 1
|
||||
break
|
||||
visited_with_direction.add((x, y, direction_index))
|
||||
dx, dy = directions[direction_index]
|
||||
new_x, new_y = x + dx, y + dy
|
||||
# if new position is off the grid, we stop
|
||||
if (new_x < 0 or new_x >= len(grid) or new_y < 0 or new_y >= len(grid[0])):
|
||||
#print(f"Moved off the grid to ({new_x}, {new_y}), stopping")
|
||||
break
|
||||
# if we hit an obstacle, we turn right
|
||||
elif (grid[new_x][new_y] == OBSTACLE or (new_x, new_y) == (row,col)):
|
||||
direction_index = (direction_index + 1) % 4 # Turn right
|
||||
#print(f"Hit obstacle at ({new_x}, {new_y}), turning right to direction {directions[direction_index]}")
|
||||
|
||||
else:
|
||||
x, y = new_x, new_y
|
||||
#print(f"Moved to ({x}, {y})")
|
||||
|
||||
print('part 1 answer',len(visited))
|
||||
print('part 2 answer',loops)
|
||||
+6
-1
@@ -90,8 +90,13 @@ def create_solution_file(year, day, part):
|
||||
# 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:
|
||||
file.write(f"""testing = 1
|
||||
if not testing:
|
||||
with open(r"{year}/{day:02}\input.txt", "r") as file:
|
||||
input = file.read()
|
||||
else:
|
||||
input = '''
|
||||
'''
|
||||
""")
|
||||
print(f"Created {solution_file}")
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user