prettify and lint optimization
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
import os
|
||||
|
||||
def parse_schematic(file_path):
|
||||
"""Reads the engine schematic from a file and returns it as a list of strings."""
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
with open(file_path, "r") as file:
|
||||
return [line.strip() for line in file.readlines()]
|
||||
except FileNotFoundError:
|
||||
raise Exception(f"File not found: {file_path}")
|
||||
|
||||
|
||||
def is_symbol(char):
|
||||
"""Checks if a character is a symbol (not a digit or a period)."""
|
||||
return not char.isdigit() and char != '.'
|
||||
return not char.isdigit() and char != "."
|
||||
|
||||
|
||||
def get_adjacent_positions(rows, cols, row, col):
|
||||
"""Generates positions adjacent (including diagonally) to the given coordinates."""
|
||||
@@ -19,26 +19,30 @@ def get_adjacent_positions(rows, cols, row, col):
|
||||
if i != row or j != col:
|
||||
yield i, j
|
||||
|
||||
|
||||
def find_start_of_number(schematic, row, col):
|
||||
"""Finds the start position of the number that includes the given digit."""
|
||||
while col > 0 and schematic[row][col - 1].isdigit():
|
||||
col -= 1
|
||||
return row, col
|
||||
|
||||
|
||||
def extract_full_number(schematic, start_row, start_col):
|
||||
"""Extracts the full number starting from the given digit coordinates."""
|
||||
start_row, start_col = find_start_of_number(schematic, start_row, start_col)
|
||||
number = ''
|
||||
rows, cols = len(schematic), len(schematic[0])
|
||||
number = ""
|
||||
cols = len(schematic[0])
|
||||
col = start_col
|
||||
|
||||
while col < cols and schematic[start_row][col].isdigit():
|
||||
number += schematic[start_row][col]
|
||||
schematic[start_row] = schematic[start_row][:col] + '.' + schematic[start_row][col + 1:]
|
||||
schematic[start_row] = (
|
||||
schematic[start_row][:col] + "." + schematic[start_row][col + 1 :]
|
||||
)
|
||||
col += 1
|
||||
|
||||
return int(number) if number else 0
|
||||
|
||||
|
||||
def sum_part_numbers(file_path):
|
||||
"""Calculates the sum of all part numbers in the engine schematic."""
|
||||
schematic = parse_schematic(file_path)
|
||||
@@ -48,7 +52,9 @@ def sum_part_numbers(file_path):
|
||||
for row in range(len(schematic)):
|
||||
for col in range(len(schematic[row])):
|
||||
if is_symbol(schematic[row][col]):
|
||||
for i, j in get_adjacent_positions(len(schematic), len(schematic[row]), row, col):
|
||||
for i, j in get_adjacent_positions(
|
||||
len(schematic), len(schematic[row]), row, col
|
||||
):
|
||||
if schematic[i][j].isdigit() and (i, j) not in visited:
|
||||
visited.add((i, j))
|
||||
number = extract_full_number(schematic, i, j)
|
||||
@@ -57,12 +63,14 @@ def sum_part_numbers(file_path):
|
||||
|
||||
return total_sum
|
||||
|
||||
|
||||
def run_tests():
|
||||
"""Runs tests on the test file and prints the results."""
|
||||
test_result = sum_part_numbers("../test.txt")
|
||||
print(f"Test Result: {test_result}")
|
||||
assert test_result == 4361, f"Test failed: Expected 4361, got {test_result}"
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to run the test and then process the input file."""
|
||||
try:
|
||||
@@ -73,5 +81,6 @@ def main():
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
import os
|
||||
|
||||
def parse_schematic(file_path):
|
||||
"""Reads the engine schematic from a file and returns it as a list of strings."""
|
||||
try:
|
||||
with open(file_path, 'r') as file:
|
||||
with open(file_path, "r") as file:
|
||||
return [line.strip() for line in file.readlines()]
|
||||
except FileNotFoundError:
|
||||
raise Exception(f"File not found: {file_path}")
|
||||
|
||||
|
||||
def get_adjacent_positions(rows, cols, row, col):
|
||||
"""Generates positions adjacent (including diagonally) to the given coordinates."""
|
||||
for i in range(max(0, row - 1), min(row + 2, rows)):
|
||||
@@ -15,26 +14,30 @@ def get_adjacent_positions(rows, cols, row, col):
|
||||
if i != row or j != col:
|
||||
yield i, j
|
||||
|
||||
|
||||
def find_start_of_number(schematic, row, col):
|
||||
"""Finds the start position of the number that includes the given digit."""
|
||||
while col > 0 and schematic[row][col - 1].isdigit():
|
||||
col -= 1
|
||||
return row, col
|
||||
|
||||
|
||||
def extract_full_number(schematic, start_row, start_col):
|
||||
"""Extracts the full number starting from the given digit coordinates."""
|
||||
start_row, start_col = find_start_of_number(schematic, start_row, start_col)
|
||||
number = ''
|
||||
rows, cols = len(schematic), len(schematic[0])
|
||||
number = ""
|
||||
cols = len(schematic[0])
|
||||
col = start_col
|
||||
|
||||
while col < cols and schematic[start_row][col].isdigit():
|
||||
number += schematic[start_row][col]
|
||||
schematic[start_row] = schematic[start_row][:col] + '.' + schematic[start_row][col + 1:]
|
||||
schematic[start_row] = (
|
||||
schematic[start_row][:col] + "." + schematic[start_row][col + 1 :]
|
||||
)
|
||||
col += 1
|
||||
|
||||
return int(number) if number else 0
|
||||
|
||||
|
||||
def find_gears_and_calculate_ratios(schematic):
|
||||
"""Finds gears in the schematic and calculates their gear ratios."""
|
||||
total_ratio_sum = 0
|
||||
@@ -42,7 +45,7 @@ def find_gears_and_calculate_ratios(schematic):
|
||||
|
||||
for row in range(rows):
|
||||
for col in range(cols):
|
||||
if schematic[row][col] == '*':
|
||||
if schematic[row][col] == "*":
|
||||
part_numbers = []
|
||||
for i, j in get_adjacent_positions(rows, cols, row, col):
|
||||
if schematic[i][j].isdigit():
|
||||
@@ -57,6 +60,7 @@ def find_gears_and_calculate_ratios(schematic):
|
||||
|
||||
return total_ratio_sum
|
||||
|
||||
|
||||
def run_tests():
|
||||
"""Runs tests on the test file and prints the results."""
|
||||
test_schematic = parse_schematic("../test.txt")
|
||||
@@ -64,6 +68,7 @@ def run_tests():
|
||||
print(f"Test Result: {test_result}")
|
||||
assert test_result == 467835, "Test failed: Expected 467835"
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to process the input file and calculate the sum of gear ratios."""
|
||||
try:
|
||||
@@ -75,5 +80,6 @@ def main():
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user