This commit is contained in:
Thomas Muller 2024-06-19 21:02:45 -04:00
parent 9b767695c3
commit cfd74a292a
3 changed files with 74 additions and 72 deletions

View file

@ -20,6 +20,8 @@ func read_map(lines, i):
entry.dst_start = int(map_entry_string[0]) entry.dst_start = int(map_entry_string[0])
entry.src_start = int(map_entry_string[1]) entry.src_start = int(map_entry_string[1])
entry.range = int(map_entry_string[2]) entry.range = int(map_entry_string[2])
entry.dst_end = entry.dst_start + entry.range - 1
entry.src_end = entry.src_start + entry.range - 1
map.append(entry) map.append(entry)
@ -104,90 +106,81 @@ func _on_part1():
print("Closest location: " + str(overall_closest)) print("Closest location: " + str(overall_closest))
func _smallest_dst(a, b): func remap_range(map, initial_range):
return a.dst_start < b.dst_start var unchopped = [initial_range]
var chopped = []
func _find_ranges(map, range): for i in range(len(map)):
# Determine what input ranges can yeild the given input range var remap = map[i]
var ranges = []
# Sort dest ranges from lowest->higest since thats what we care about
var entries = map.duplicate()
entries.sort_custom(_smallest_dst)
# Define search range
# This will get bumped to the right as each new range is found
var search_range_start = range.src_start
var search_range_len = range.range
for entry in map:
#print('Trying entry')
#print(entry)
#print('Range is')
#print(range)
# Input range is before entry, shift it to where the entry starts var new_ranges = []
var nd_start = entry.dst_start - search_range_start for range_ in unchopped:
if entry.dst_start > search_range_start and nd_start < search_range_len: if not (range_.start <= remap.src_end and remap.src_start <= range_.end):
search_range_start += nd_start new_ranges.append(range_)
search_range_len -= nd_start continue
#print("Range was before entry. New range is")
#print(str(search_range_start) + " (" + str(search_range_len) + ")")
#print(entry)
var d_start = search_range_start - entry.dst_start # Case 1
# Search range is contained within entry if range_.start < remap.src_start:
if entry.dst_start <= search_range_start and d_start < entry.range: var range = SeedRange.new()
var new_entry = MapEntry.new() range.start = range_.start
# New range's destination is the start of the search range range.end = remap.src_start - 1
# This was given new_ranges.append(range)
new_entry.dst_start = search_range_start
# New range's source is the reverse apply of this mapentry # Case 2
new_entry.src_start = entry.apply_rev(search_range_start) var bump = remap.dst_start - remap.src_start
# New range's range is either the end of the search range var range = SeedRange.new()
# or the end of entry, whichever is less range.start = max(range_.start, remap.src_start) + bump
new_entry.range = min(entry.range - d_start, search_range_len) range.end = min(range_.end, remap.src_end) + bump
ranges.append(new_entry) chopped.append(range)
# Bump search range past this new entry # Case 3
search_range_start += new_entry.range if range_.end > remap.src_end:
search_range_len -= new_entry.range range = SeedRange.new()
range.start = remap.src_end + 1
if len(ranges) == 0: range.end = range_.end
ranges.append(range) new_ranges.append(range)
return ranges unchopped = Array(new_ranges)
return unchopped + chopped
func do_thing(in_ranges, map):
var out_ranges = []
for seed in in_ranges:
var remapped = remap_range(map, seed)
out_ranges += remapped
return out_ranges
func _on_part2(): func _on_part2():
var input = $TextEdit.text + '\n' # Safety newline :) var input = $TextEdit.text + '\n' # Safety newline :)
var seeds_maps = parse_input(input) var seeds_maps = parse_input(input)
var seeds = seeds_maps[0] var seed_ints = seeds_maps[0]
var maps = seeds_maps[1] var maps = seeds_maps[1]
var seeds = []
for i in range(len(seed_ints) / 2):
var start = seed_ints[i * 2 + 0]
var length = seed_ints[i * 2 + 1]
var seed = SeedRange.new()
seed.start = start
seed.end = start + length - 1
seeds.append(seed)
print(seeds) print(seeds)
print(maps)
# Sort by closest locations var soils = do_thing(seeds, maps.seed2soil)
var closest_locs = maps.seed2soil.duplicate() var fertilizers = do_thing(soils, maps.soil2fertilizer)
closest_locs.sort_custom(_smallest_dst) var waters = do_thing(fertilizers, maps.fertilizer2water)
var lights = do_thing(waters, maps.water2light)
var temperatures = do_thing(lights, maps.light2temperature)
var humidities = do_thing(temperatures, maps.temperature2humidity)
var locations = do_thing(humidities, maps.humidity2location)
print('Closest Humidities') print(locations)
print(closest_locs)
# for loc in closest_locs: var lowest = locations[0].start
var light_ranges = _find_ranges(maps.light2temperature, closest_locs[1]) for loc in locations:
print('Light ranges') lowest = min(lowest, loc.start)
print(light_ranges)
var water_ranges = _find_ranges(maps.water2light, light_ranges[0]) print(lowest)
print('Water ranges')
print(water_ranges)
var search_maps = [
maps.humidity2location,
maps.temperature2humidity,
maps.light2temperature,
maps.water2light,
maps.fertilizer2water,
maps.soil2fertilizer,
maps.seed2soil
]

View file

@ -2,6 +2,8 @@ class_name MapEntry
var src_start := 0 var src_start := 0
var dst_start := 0 var dst_start := 0
var src_end := 0
var dst_end := 0
var range := 0 var range := 0
func _to_string(): func _to_string():

7
day_5/SeedRange.gd Normal file
View file

@ -0,0 +1,7 @@
class_name SeedRange
var start := 0
var end := 0
func _to_string():
return str(self.start) + " -> " + str(self.end)