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 new_ranges = []
var entries = map.duplicate() for range_ in unchopped:
entries.sort_custom(_smallest_dst) if not (range_.start <= remap.src_end and remap.src_start <= range_.end):
new_ranges.append(range_)
continue
# Define search range # Case 1
# This will get bumped to the right as each new range is found if range_.start < remap.src_start:
var search_range_start = range.src_start var range = SeedRange.new()
var search_range_len = range.range range.start = range_.start
range.end = remap.src_start - 1
new_ranges.append(range)
for entry in map: # Case 2
#print('Trying entry') var bump = remap.dst_start - remap.src_start
#print(entry) var range = SeedRange.new()
#print('Range is') range.start = max(range_.start, remap.src_start) + bump
#print(range) range.end = min(range_.end, remap.src_end) + bump
chopped.append(range)
# Input range is before entry, shift it to where the entry starts # Case 3
var nd_start = entry.dst_start - search_range_start if range_.end > remap.src_end:
if entry.dst_start > search_range_start and nd_start < search_range_len: range = SeedRange.new()
search_range_start += nd_start range.start = remap.src_end + 1
search_range_len -= nd_start range.end = range_.end
#print("Range was before entry. New range is") new_ranges.append(range)
#print(str(search_range_start) + " (" + str(search_range_len) + ")")
#print(entry)
var d_start = search_range_start - entry.dst_start unchopped = Array(new_ranges)
# 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)
# Bump search range past this new entry return unchopped + chopped
search_range_start += new_entry.range
search_range_len -= new_entry.range
if len(ranges) == 0: func do_thing(in_ranges, map):
ranges.append(range) 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(): 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('Water ranges')
print(water_ranges)
var search_maps = [ print(lowest)
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)