logan_2023/day5/d5p2.gd

125 lines
2.6 KiB
GDScript3
Raw Normal View History

2023-12-06 02:56:31 -05:00
#!/usr/bin/godot -s
#d5p2.gd
#extends Node3D
extends SceneTree
2023-12-06 02:56:31 -05:00
enum Maps {
seeds,
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemp,
temptoHumidity,
2023-12-06 02:56:31 -05:00
humidityToLocation
}
enum Column {
destination,
origin,
range
}
func file_load(path):
var file = FileAccess.open(path, FileAccess.READ)
var content = file.get_as_text()
return content
func strarr_to_arr(input):
var new = []
for i in range(0, input.size()):
new.append(input[i])
2023-12-06 02:56:31 -05:00
return new
func input_clean(input):
for i in range(0, input.size()):
var regex = RegEx.new()
regex.compile("^(.*-to-.* map:\n)|^(seeds: )")
input[i] = regex.sub(input[i], "", true)
# bad variable names :(
func input_format(input):
var new = []
for i in range(0, input.size()):
var temp = strarr_to_arr(input[i].split("\n"))
var temp4 = []
2023-12-06 02:56:31 -05:00
for j in range(0, temp.size()):
var temp2 = strarr_to_arr(temp[j].split(" "))
var temp3 = []
2023-12-06 02:56:31 -05:00
for k in range(0, temp2.size()):
temp3.append(int(temp2[k]))
2023-12-06 02:56:31 -05:00
temp4.append(temp3)
2023-12-06 02:56:31 -05:00
new.append(temp4)
2023-12-06 02:56:31 -05:00
return new
func is_mapped(map, input):
for v in map:
#if input in range(v[Column.origin], v[Column.origin]+v[Column.range]): # Fucking Godot runs out of memory when I use this
if input >= v[Column.origin] && input <= v[Column.origin]+v[Column.range]:
return v[Column.destination] + (input-v[Column.origin])
2023-12-06 02:56:31 -05:00
return input
func is_revmapped(map, input):
for v in map:
if input >= v[Column.destination] && input <= v[Column.destination]+v[Column.range]:
return v[Column.origin] + (input-v[Column.destination])
2023-12-06 02:56:31 -05:00
return input
func map_input(data, seed):
var temp = seed
for map in range(Maps.seedToSoil, Maps.humidityToLocation+1):
temp = is_mapped(data[map], temp)
#print("Output: %s %s" % [map, temp])
2023-12-06 02:56:31 -05:00
return temp
func revmap_input(data, location):
var temp = location
for map in range(Maps.humidityToLocation, Maps.seedToSoil-1, -1):
temp = is_revmapped(data[map], temp)
#print("Output: %s %s" % [map, temp])
2023-12-06 02:56:31 -05:00
return temp
func _init():
print("Hello!")
var temp = file_load(OS.get_cmdline_user_args()[0]).split("\n\n")
2023-12-06 02:56:31 -05:00
input_clean(temp)
2023-12-06 02:56:31 -05:00
var input = input_format(temp)
#print(input[Maps.seeds])
#print(input[Maps.seedToSoil])
2023-12-06 02:56:31 -05:00
temp.clear()
2023-12-06 02:56:31 -05:00
var seeds = input[0][0]
print(seeds)
2023-12-06 02:56:31 -05:00
var testlocation = 0
while true:
if testlocation % 10000 == 0:
print(testlocation)
for i in range(0, seeds.size() / 2):
var seed = revmap_input(input, testlocation)
if seed >= seeds[i*2] && seed <= seeds[i*2]+seeds[i*2+1]:
print("Answer 2: %s" % testlocation)
print("Value is between seed %s and %s" % [seeds[i*2], seeds[i*2]+seeds[i*2+1]])
quit()
2023-12-06 02:56:31 -05:00
return
2023-12-06 02:56:31 -05:00
testlocation+=1