quick lint

This commit is contained in:
WieErWill 2023-12-21 22:02:54 +01:00
parent 816961a2b7
commit 1fe0310e21
3 changed files with 28 additions and 7 deletions

View File

@ -7,7 +7,7 @@ type Workflows = { [key: string]: [WorkflowRules, string] };
function parseInput(filePath: string): Workflows { function parseInput(filePath: string): Workflows {
const lines = fs.readFileSync(filePath, 'utf-8').split('\n').filter(line => line); const lines = fs.readFileSync(filePath, 'utf-8').split('\n').filter(line => line);
let workflows: Workflows = {}; const workflows: Workflows = {};
let currentWorkflow: string[] = []; let currentWorkflow: string[] = [];
for (const line of lines) { for (const line of lines) {

View File

@ -1,5 +1,6 @@
from collections import deque from collections import deque
def process_modules(filename, debug=False): def process_modules(filename, debug=False):
""" """
Reads the module configuration from a file, initializes the modules, and simulates the pulse propagation. Reads the module configuration from a file, initializes the modules, and simulates the pulse propagation.
@ -40,7 +41,9 @@ def process_modules(filename, debug=False):
name, target, pulse = queue.popleft() name, target, pulse = queue.popleft()
if debug: if debug:
print(f"Iteration {iteration}: Processing {target}, Pulse: {'High' if pulse else 'Low'}") print(
f"Iteration {iteration}: Processing {target}, Pulse: {'High' if pulse else 'Low'}"
)
if target not in modules: if target not in modules:
continue continue
@ -65,19 +68,25 @@ def process_modules(filename, debug=False):
low += 1 low += 1
if debug: if debug:
print(f"Iteration {iteration}: {target} -> {'High' if new_pulse else 'Low'} to {module[2]}") print(
f"Iteration {iteration}: {target} -> {'High' if new_pulse else 'Low'} to {module[2]}"
)
return low * high return low * high
def test(): def test():
""" """
Runs the process_modules function with the test input and checks if it meets the expected outcome. Runs the process_modules function with the test input and checks if it meets the expected outcome.
""" """
expected_result = 11687500 # Replace with the expected result of the test input expected_result = 11687500 # Replace with the expected result of the test input
test_result = process_modules("../test.txt", debug=True) test_result = process_modules("../test.txt", debug=True)
assert test_result == expected_result, f"Test failed: Expected {expected_result}, got {test_result}" assert (
test_result == expected_result
), f"Test failed: Expected {expected_result}, got {test_result}"
print(f"Test passed with result: {test_result}") print(f"Test passed with result: {test_result}")
def main(): def main():
""" """
Main function to run the test and then process the actual puzzle input. Main function to run the test and then process the actual puzzle input.
@ -94,5 +103,6 @@ def main():
except Exception as e: except Exception as e:
print(f"An error occurred: {e}") print(f"An error occurred: {e}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -1,14 +1,15 @@
from collections import deque from collections import deque
def read_grid(file_path): def read_grid(file_path):
"""Reads the grid from a file and returns it along with the starting position.""" """Reads the grid from a file and returns it along with the starting position."""
with open(file_path, 'r') as file: with open(file_path, "r") as file:
grid = [list(line.strip()) for line in file.readlines()] grid = [list(line.strip()) for line in file.readlines()]
start = None start = None
for i, row in enumerate(grid): for i, row in enumerate(grid):
for j, cell in enumerate(row): for j, cell in enumerate(row):
if cell == 'S': if cell == "S":
start = (i, j) start = (i, j)
break break
if start: if start:
@ -18,6 +19,7 @@ def read_grid(file_path):
print(f"Grid loaded from {file_path}. Start position: {start}") print(f"Grid loaded from {file_path}. Start position: {start}")
return grid, start return grid, start
def bfs(grid, start): def bfs(grid, start):
"""Performs BFS on the grid to track the minimum steps to each plot.""" """Performs BFS on the grid to track the minimum steps to each plot."""
printf("Start BFS") printf("Start BFS")
@ -33,18 +35,25 @@ def bfs(grid, start):
else: else:
for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]: for dx, dy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
nx, ny = x + dx, y + dy nx, ny = x + dx, y + dy
if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] == '.' and (nx, ny) not in distances: if (
0 <= nx < len(grid)
and 0 <= ny < len(grid[0])
and grid[nx][ny] == "."
and (nx, ny) not in distances
):
queue.append((nx, ny, step + 1)) queue.append((nx, ny, step + 1))
print(f"BFS completed. Number of plots visited: {len(distances)}") print(f"BFS completed. Number of plots visited: {len(distances)}")
return distances return distances
def count_reachable_plots(distances, steps): def count_reachable_plots(distances, steps):
"""Counts the number of plots reachable in exactly the given number of steps.""" """Counts the number of plots reachable in exactly the given number of steps."""
count = sum(1 for d in distances.values() if d == steps) count = sum(1 for d in distances.values() if d == steps)
print(f"Number of plots reachable in exactly {steps} steps: {count}") print(f"Number of plots reachable in exactly {steps} steps: {count}")
return count return count
def run_test(): def run_test():
"""Runs the algorithm with the test data and asserts the result.""" """Runs the algorithm with the test data and asserts the result."""
print("Running test...") print("Running test...")
@ -54,6 +63,7 @@ def run_test():
assert test_result == 16, f"Test failed: Expected 16, got {test_result}" assert test_result == 16, f"Test failed: Expected 16, got {test_result}"
print("Test passed successfully.") print("Test passed successfully.")
def main(): def main():
"""Main function to run the algorithm with the actual puzzle data.""" """Main function to run the algorithm with the actual puzzle data."""
try: try:
@ -69,5 +79,6 @@ def main():
except Exception as e: except Exception as e:
print(f"Unexpected error: {e}") print(f"Unexpected error: {e}")
if __name__ == "__main__": if __name__ == "__main__":
main() main()