prettify and lint optimization

This commit is contained in:
2023-12-10 17:24:14 +01:00
parent 274c69d0c4
commit 4cd62ef794
32 changed files with 599 additions and 262 deletions

View File

@@ -1,5 +1,3 @@
import os
def map_seed_through_category(seed, line):
"""Maps a single seed through a category based on a mapping line."""
dest_start, source_start, range_length = map(int, line.split())
@@ -7,20 +5,24 @@ def map_seed_through_category(seed, line):
return dest_start + (seed - source_start)
return seed
def process_category(file, seeds):
"""Processes seeds through a single category based on the file lines."""
print(f"Starting category processing")
print("Starting category processing")
updated_seeds = [-1] * len(seeds) # Initialize with -1 to indicate unmapped seeds
for line in file:
line = line.strip()
print(f"Processing category line: {line}")
if not line or ':' in line: # End of the current category map
if not line or ":" in line: # End of the current category map
break
dest_start, source_start, range_length = map(int, line.split())
for i, seed in enumerate(seeds):
if updated_seeds[i] == -1 and source_start <= seed < source_start + range_length:
if (
updated_seeds[i] == -1
and source_start <= seed < source_start + range_length
):
updated_seeds[i] = dest_start + (seed - source_start)
# For seeds that weren't mapped in this category, keep their original value
@@ -31,18 +33,19 @@ def process_category(file, seeds):
print(f"Seeds after category processing: {updated_seeds}")
return updated_seeds
def process_file(file_path, is_test=False):
"""Processes the file to find the lowest location number for the seeds."""
try:
with open(file_path, 'r') as file:
seeds = list(map(int, file.readline().split(':')[1].split()))
with open(file_path, "r") as file:
seeds = list(map(int, file.readline().split(":")[1].split()))
print(f"Initial Seeds: {seeds}")
while True:
line = file.readline()
if not line: # End of file
break
if ':' in line: # Start of a new category map
if ":" in line: # Start of a new category map
seeds = process_category(file, seeds)
lowest_location = min(seeds)
@@ -54,13 +57,15 @@ def process_file(file_path, is_test=False):
except Exception as e:
print(f"An error occurred processing '{file_path}': {e}")
def test():
"""Run tests using the test.txt file."""
expected_result = 35 # Based on the given example
result = process_file('../test.txt')
result = process_file("../test.txt")
assert result == expected_result, f"Test failed, expected 35 but got {result}"
print(f"Test passed: {result}")
def main():
"""Main function to process the input file and display results."""
try:
@@ -74,5 +79,6 @@ def main():
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()

View File

@@ -1,11 +1,12 @@
import os
import gc
from itertools import groupby
def free_up_memory():
"""Explicitly frees up memory."""
gc.collect()
def parse_input(file_path):
"""Parses the input file into seeds and categories."""
with open(file_path) as file:
@@ -20,6 +21,7 @@ def parse_input(file_path):
]
return seeds_numbers, categories
def process_categories(seeds_numbers, categories):
"""Processes the seed ranges through all categories."""
for category in categories:
@@ -49,6 +51,7 @@ def process_categories(seeds_numbers, categories):
seeds_numbers = sources
return seeds_numbers
def find_lowest_location(file_path, is_test=False, expected_result=None):
"""Finds the lowest location number from the input file."""
try:
@@ -57,7 +60,9 @@ def find_lowest_location(file_path, is_test=False, expected_result=None):
lowest_location = min(seeds_numbers)[0]
if is_test:
assert lowest_location == expected_result, f"Test failed, expected {expected_result} but got {lowest_location}"
assert (
lowest_location == expected_result
), f"Test failed, expected {expected_result} but got {lowest_location}"
print("Test passed.")
else:
print(f"Lowest location from {file_path}: {lowest_location}")
@@ -68,11 +73,13 @@ def find_lowest_location(file_path, is_test=False, expected_result=None):
except Exception as e:
print(f"An error occurred processing '{file_path}': {e}")
def test():
"""Run tests using the test.txt file."""
print("Starting test")
expected_result = 46 # Updated expected result for the new puzzle
find_lowest_location('../test.txt', is_test=True, expected_result=expected_result)
find_lowest_location("../test.txt", is_test=True, expected_result=expected_result)
def main():
"""Main function to process the input file and display results."""
@@ -87,5 +94,6 @@ def main():
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()