slow but better efficency solution2 for Day05 in python
This commit is contained in:
parent
18d6115fdf
commit
123edd8275
@ -5,57 +5,57 @@ def free_up_memory():
|
|||||||
"""Explicitly frees up memory."""
|
"""Explicitly frees up memory."""
|
||||||
gc.collect()
|
gc.collect()
|
||||||
|
|
||||||
def get_mapped_seed(seed, category_mapping):
|
def parse_ranges(line):
|
||||||
"""Finds the mapped seed for a given seed in a category."""
|
"""Parses a line containing seed ranges."""
|
||||||
for line in category_mapping:
|
numbers = list(map(int, line.split()))
|
||||||
dest_start, source_start, range_length = map(int, line.split())
|
return [(numbers[i], numbers[i + 1]) for i in range(0, len(numbers), 2)]
|
||||||
|
|
||||||
|
def map_single_seed_through_category(seed, category_map):
|
||||||
|
"""Maps a single seed through a category based on the mapping."""
|
||||||
|
print(f"map seed {seed} in category map")
|
||||||
|
for source_start, (dest_start, range_length) in category_map.items():
|
||||||
if source_start <= seed < source_start + range_length:
|
if source_start <= seed < source_start + range_length:
|
||||||
return dest_start + (seed - source_start)
|
return dest_start + (seed - source_start)
|
||||||
return seed
|
return seed
|
||||||
|
|
||||||
def process_seed_ranges(seed_ranges, category_mappings):
|
def process_category(file, ranges):
|
||||||
"""Processes seed ranges through categories to find the minimum location."""
|
"""Processes seed ranges through a single category based on the file lines."""
|
||||||
# Processing each seed range through categories
|
category_map = {}
|
||||||
min_location = float('inf')
|
for i, line in file:
|
||||||
for start, end in seed_ranges:
|
line = line.strip()
|
||||||
print(f"Processing seed range {start} to {end}")
|
if not line or ':' in line: # End of the current category map
|
||||||
|
break
|
||||||
|
dest_start, source_start, range_length = map(int, line.split())
|
||||||
|
category_map[source_start] = (dest_start, range_length)
|
||||||
|
print(f"parsed line {i} in file")
|
||||||
|
|
||||||
# If min_location is still inf, then iterate through the range (fallback)
|
# Process each seed through the category map
|
||||||
if min_location == float('inf'):
|
mapped_seeds = set()
|
||||||
for seed in range(start, end):
|
for start, length in ranges:
|
||||||
location = seed
|
print(f"process range {start} with length {length}")
|
||||||
for category_mapping in category_mappings:
|
for i in range(length):
|
||||||
location = get_mapped_seed(location, category_mapping)
|
seed = start + i
|
||||||
min_location = min(min_location, location)
|
mapped_seed = map_single_seed_through_category(seed, category_map)
|
||||||
return min_location
|
mapped_seeds.add(mapped_seed)
|
||||||
|
|
||||||
def process_file(file_path, is_test=False):
|
return mapped_seeds
|
||||||
|
|
||||||
|
def process_file(file_path):
|
||||||
"""Processes the file to find the lowest location number for the seed ranges."""
|
"""Processes the file to find the lowest location number for the seed ranges."""
|
||||||
try:
|
try:
|
||||||
with open(file_path, 'r') as file:
|
with open(file_path, 'r') as file:
|
||||||
lines = file.readlines()
|
ranges = parse_ranges(file.readline().split(':')[1])
|
||||||
# Parsing the seed ranges
|
|
||||||
seed_line = lines[0].strip()
|
|
||||||
parts = seed_line.split(':')[1].split()
|
|
||||||
seed_ranges = [(int(parts[i]), int(parts[i]) + int(parts[i + 1]))
|
|
||||||
for i in range(0, len(parts), 2)]
|
|
||||||
# print(f"Seed Ranges: {seed_ranges}")
|
|
||||||
|
|
||||||
# Preparing category mappings
|
while True:
|
||||||
category_mappings = [[] for _ in range(7)] # 7 categories
|
line = file.readline()
|
||||||
current_category = -1
|
if not line: # End of file
|
||||||
for line in lines[1:]:
|
break
|
||||||
# print(f"processing line {line}")
|
if ':' in line: # Start of a new category map
|
||||||
if ':' in line:
|
ranges = {(seed, 1) for seed in process_category(file, ranges)}
|
||||||
current_category += 1
|
|
||||||
elif line.strip():
|
|
||||||
category_mappings[current_category].append(line.strip())
|
|
||||||
|
|
||||||
# Processing each seed range through categories
|
lowest_location = min(ranges)[0]
|
||||||
min_location = process_seed_ranges(seed_ranges, category_mappings)
|
|
||||||
|
|
||||||
print(f"Lowest location from {file_path}: {min_location}")
|
return lowest_location
|
||||||
return min_location
|
|
||||||
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print(f"Error: File '{file_path}' not found.")
|
print(f"Error: File '{file_path}' not found.")
|
||||||
@ -66,7 +66,7 @@ def test():
|
|||||||
"""Run tests using the test.txt file."""
|
"""Run tests using the test.txt file."""
|
||||||
print("Starting test")
|
print("Starting test")
|
||||||
expected_result = 46 # Updated expected result for the new puzzle
|
expected_result = 46 # Updated expected result for the new puzzle
|
||||||
result = process_file('../test.txt', is_test=True)
|
result = process_file('../test.txt')
|
||||||
assert result == expected_result, f"Test failed, expected 46 but got {result}"
|
assert result == expected_result, f"Test failed, expected 46 but got {result}"
|
||||||
print(f"Test passed: {result}")
|
print(f"Test passed: {result}")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user