diff --git a/python/README.md b/python/README.md index d5d3feb..4655f6f 100644 --- a/python/README.md +++ b/python/README.md @@ -1,10 +1,6 @@ ToDo ===== -- Day 1 Part 2 - -- Day 5 Part 2 - - Day 7 Part 2 - Day 10 both parts \ No newline at end of file diff --git a/python/day5.py b/python/day5.py index 5911e1c..bc8ef7d 100644 --- a/python/day5.py +++ b/python/day5.py @@ -68,15 +68,15 @@ def seed_is_valid(seed: int, seed_ranges: list[tuple]) -> bool: return False def rev_traverse(loc: int) -> int: - map_names = list(maps.keys()).reverse() + map_names = list(maps.keys())[::-1] for name in map_names: map_applied = False for l in maps[name]: if not map_applied: - if seed in range(l[0],l[0]+l[2]): - seed = seed - l[0] + l[1] + if loc in range(l[0],l[0]+l[2]): + loc = loc - l[0] + l[1] map_applied = True - return seed + return loc maps = parsed_maps(lines) @@ -84,78 +84,13 @@ seeds = parsed_seeds(lines) seed_ranges = parsed_seed_ranges(lines) print(f"Solution Part 1: {min(seeds_to_locations(seeds, maps))}") -print(f"Solution Part 2: {min(min_loc_ranges)}") - - """ yikes +def find_lowest_valid_location(seed_ranges: list[tuple]) -> int: + for i in range(100000000): + seed = rev_traverse(i) + if seed_is_valid(seed, seed_ranges): + return i + return None -def parsed_seed_ranges(lines: list[str]) -> list[tuple]: - seeds = [int(x) for x in lines[0].strip("\n")[6:].split()] - seed_ranges = [] - for i in range(0, len(seeds), 2): - seed_ranges.append((seeds[i], seeds[i] + seeds[i+1])) - return seed_ranges - -def map_seed_range(seed_range: tuple, mapping: list[int]) -> tuple: - #returns transformed range for one map and remaining range(s) that were not affected - def apply_map(tup: tuple, mapping): - x = tup[0] - y = tup[1] - result = (x + l[0] - l[1], y + l[0] - l[1]) - #print(f"mapped {tup, result}") - return result - - mapped = [] - remaining = seed_range - for l in mapping: - not_mapped = [] - #print(f"l: {l}") - #print(f"source range from {l[1]} to {l[1] + l[2]}") - while len(remaining) > 0: - r = remaining.pop() - #print(f"-----r: {r}-----") - if r[0] == r[1]: - pass - #print("just delete, empty range") - else: - if r[1] <= l[1]: - #print("not mapped") - not_mapped.append(r) - elif r[0] >= l[1] + l[2]: - #print("not mapped") - not_mapped.append(r) - elif r[0] >= l[1] and r[1] <= l[1] + l[2]: - mapped.append(apply_map(r, l)) - elif r[0] < l[1] and r[1] <= l[1] + l[2]: - mapped.append(apply_map((l[1], r[1]), l)) - not_mapped.append((r[0], l[1])) - #print(f"remained: {(r[0], l[1])}") - elif r[0] >= l[1] and r[1] > l[1] + l[2]: - mapped.append(apply_map((r[0], l[1] + l[2]), l)) - not_mapped.append((l[1] + l[2], r[1])) - #print(f"remained: {(l[1] + l[2], r[1])}") - elif r[0] < l[1] and r[1] > l[1] + l[2]: - not_mapped.append(apply_map((l[1], l[1] + l[2]), l)) - remaining.append((r[0], l[1])) - #print(f"remained: {(r[0], l[1])}") - remaining.append((l[1] + l[2], r[1])) - #print(f"remained: {(l[1] + l[2], r[1])}") - #print(not_mapped) - remaining = list(set(not_mapped)) - #print(f"remaining {remaining}") - return mapped + remaining - - -def seed_range_to_loc_ranges(seed_ranges: list[tuple], maps: dict) -> list[tuple]: - # single seed range going through maps - #print(seed_ranges) - map_names = list(maps.keys()) - for name in map_names: - #print(f"apply {name}") - seed_ranges = map_seed_range(seed_ranges, maps[name]) - #print(seed_ranges) - return seed_ranges - -#loc_ranges = seed_range_to_loc_ranges(parsed_seed_ranges(lines), maps) -#min_loc_ranges = [x for x, y in loc_ranges] - """ \ No newline at end of file +print(f"Solution to Part 2 takes a couple of minutes.") +print(f"Solution Part 2: {find_lowest_valid_location(seed_ranges)}")