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

View file

@ -2,6 +2,8 @@ class_name MapEntry
var src_start := 0
var dst_start := 0
var src_end := 0
var dst_end := 0
var range := 0
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)