logan_2023/day5/d5p1.gd

100 lines
2 KiB
GDScript3
Raw Normal View History

2023-12-06 00:09:39 -05:00
#!/usr/bin/godot -s
#d5p1.gd
extends Node3D
enum Maps {
seeds,
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemp,
temptoHumidity,
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])
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 = []
for j in range(0, temp.size()):
var temp2 = strarr_to_arr(temp[j].split(" "))
var temp3 = []
for k in range(0, temp2.size()):
temp3.append(int(temp2[k]))
temp4.append(temp3)
new.append(temp4)
return new
func is_mapped(map, input):
for v in map:
2023-12-06 02:55:59 -05:00
#if input in range(v[Column.origin], v[Column.origin]+v[Column.range]): # Fucking Godot runs out of memory when I use this
2023-12-06 00:29:20 -05:00
if input >= v[Column.origin] && input <= v[Column.origin]+v[Column.range]:
2023-12-06 00:09:39 -05:00
return v[Column.destination] + (input-v[Column.origin])
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])
return temp
func _init():
print("Hello!")
2023-12-06 02:55:59 -05:00
var temp = file_load("res://input.txt").split("\n\n")
2023-12-06 00:09:39 -05:00
input_clean(temp)
var input = input_format(temp)
#print(input[Maps.seeds])
#print(input[Maps.seedToSoil])
temp.clear()
var seeds = input[0][0]
print(seeds)
2023-12-06 02:55:59 -05:00
var minval = 2**32
for seed in seeds:
var location = map_input(input, seed)
if location < minval:
minval = location
2023-12-06 00:09:39 -05:00
2023-12-06 02:55:59 -05:00
print("Answer: %s" % minval)