diff --git a/04/README.md b/04/README.md index 4908705..52ab8b6 100644 --- a/04/README.md +++ b/04/README.md @@ -43,3 +43,137 @@ x.x.@@@.x. ``` Consider your complete diagram of the paper roll locations. How many rolls of paper can be accessed by a forklift? + +## --- Part Two --- + +Now, the Elves just need help accessing as much of the paper as they can. + +Once a roll of paper can be accessed by a forklift, it can be removed. Once a roll of paper is removed, the forklifts might be able to access more rolls of paper, which they might also be able to remove. How many total rolls of paper could the Elves remove if they keep repeating this process? + +Starting with the same example as above, here is one way you could remove as many rolls of paper as possible, using highlighted @ to indicate that a roll of paper is about to be removed, and using x to indicate that a roll of paper was just removed: + +```txt +Initial state: +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@. + +Remove 13 rolls of paper: +..xx.xx@x. +x@@.@.@.@@ +@@@@@.x.@@ +@.@@@@..@. +x@.@@@@.@x +.@@@@@@@.@ +.@.@.@.@@@ +x.@@@.@@@@ +.@@@@@@@@. +x.x.@@@.x. + +Remove 12 rolls of paper: +.......x.. +.@@.x.x.@x +x@@@@...@@ +x.@@@@..x. +.@.@@@@.x. +.x@@@@@@.x +.x.@.@.@@@ +..@@@.@@@@ +.x@@@@@@@. +....@@@... + +Remove 7 rolls of paper: +.......... +.x@.....x. +.@@@@...xx +..@@@@.... +.x.@@@@... +..@@@@@@.. +...@.@.@@x +..@@@.@@@@ +..x@@@@@@. +....@@@... + +Remove 5 rolls of paper: +.......... +..x....... +.x@@@..... +..@@@@.... +...@@@@... +..x@@@@@.. +...@.@.@@. +..x@@.@@@x +...@@@@@@. +....@@@... + +Remove 2 rolls of paper: +.......... +.......... +..x@@..... +..@@@@.... +...@@@@... +...@@@@@.. +...@.@.@@. +...@@.@@@. +...@@@@@x. +....@@@... + +Remove 1 roll of paper: +.......... +.......... +...@@..... +..x@@@.... +...@@@@... +...@@@@@.. +...@.@.@@. +...@@.@@@. +...@@@@@.. +....@@@... + +Remove 1 roll of paper: +.......... +.......... +...x@..... +...@@@.... +...@@@@... +...@@@@@.. +...@.@.@@. +...@@.@@@. +...@@@@@.. +....@@@... + +Remove 1 roll of paper: +.......... +.......... +....x..... +...@@@.... +...@@@@... +...@@@@@.. +...@.@.@@. +...@@.@@@. +...@@@@@.. +....@@@... + +Remove 1 roll of paper: +.......... +.......... +.......... +...x@@.... +...@@@@... +...@@@@@.. +...@.@.@@. +...@@.@@@. +...@@@@@.. +....@@@... +``` + +Stop once no more rolls of paper are accessible by a forklift. In this example, a total of 43 rolls of paper can be removed. + +Start with your original diagram. How many rolls of paper in total can be removed by the Elves and their forklifts? diff --git a/04/input/test_grid.txt b/04/input/test_grid.txt new file mode 100644 index 0000000..9ad769d --- /dev/null +++ b/04/input/test_grid.txt @@ -0,0 +1,10 @@ +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@. \ No newline at end of file diff --git a/04/main.py b/04/main.py index ef42498..a1edfa4 100755 --- a/04/main.py +++ b/04/main.py @@ -37,26 +37,46 @@ def is_accessessible(grid: list[str], row: int, col: int) -> bool: ] if grid[row + x_offset][col + y_offset] == "@": count += 1 - debug(f"ADJACENT SPOTS FOR {row}, {col}: {adjacent_spots}") - return count < 4 + return count < 5 -def get_accessible_paper_rolls(grid: list[str]) -> int: - count = 0 +def get_accessible_paper_rolls(grid: list[str]) -> list[tuple[int, int, str]]: + accessible_paper_rolls: list[tuple[int, int, str]] = [] for row in range(len(grid)): - debug(f"CURRENT ROW: {grid[row]}") for col in range(len(grid[row])): - debug(f"CURRENT VALUE: {grid[row][col]}") if is_accessessible(grid, row, col): - count += 1 - return count + accessible_paper_rolls.append((row, col, grid[row][col])) + return accessible_paper_rolls + + +def remove_accessible_paper_rolls( + current_grid: list[str], paper_rolls: list[tuple[int, int, str]] +) -> list[str]: + for paper_roll in paper_rolls: + current_grid[paper_roll[0]] = ( + current_grid[paper_roll[0]][: paper_roll[1]] + + "x" + + current_grid[paper_roll[0]][(paper_roll[1] + 1):] + ) + debug("GRID AFTER REMOVALS:") + for row in current_grid: + debug(f"{row}") + return current_grid def main() -> None: - input_filepath = "input/grid.txt" + input_filepath = "input/test_grid.txt" input_grid = parse_input(input_filepath) paper_rolls = get_accessible_paper_rolls(input_grid) - print(f"Paper rolls accessible by the forklift: {paper_rolls}") + count = len([x[2] for x in paper_rolls]) + print(f"Paper rolls initially accessible by the forklift: {count}") + current_grid = input_grid + remove_accessible_paper_rolls(current_grid, paper_rolls) + while len(paper_rolls) > 0: + paper_rolls = get_accessible_paper_rolls(current_grid) + count += len([x[2] for x in paper_rolls]) + current_grid = remove_accessible_paper_rolls(current_grid, paper_rolls) + print(f"Total paper rolls accessible by the forklift: {count}") return