D5: Made Godot scripts runnable from the command line

This commit is contained in:
Logan G 2023-12-06 01:23:40 -07:00
parent b9a86c612c
commit 9a2564986b
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 59 additions and 55 deletions

View file

@ -1,15 +1,16 @@
#!/usr/bin/godot -s #!/usr/bin/godot -s
#d5p1.gd #d5p1.gd
extends Node3D #extends Node3D
extends SceneTree
enum Maps { enum Maps {
seeds, seeds,
seedToSoil, seedToSoil,
soilToFertilizer, soilToFertilizer,
fertilizerToWater, fertilizerToWater,
waterToLight, waterToLight,
lightToTemp, lightToTemp,
temptoHumidity, temptoHumidity,
humidityToLocation humidityToLocation
} }
@ -28,7 +29,7 @@ func strarr_to_arr(input):
var new = [] var new = []
for i in range(0, input.size()): for i in range(0, input.size()):
new.append(input[i]) new.append(input[i])
return new return new
func input_clean(input): func input_clean(input):
@ -43,18 +44,18 @@ func input_format(input):
for i in range(0, input.size()): for i in range(0, input.size()):
var temp = strarr_to_arr(input[i].split("\n")) var temp = strarr_to_arr(input[i].split("\n"))
var temp4 = [] var temp4 = []
for j in range(0, temp.size()): for j in range(0, temp.size()):
var temp2 = strarr_to_arr(temp[j].split(" ")) var temp2 = strarr_to_arr(temp[j].split(" "))
var temp3 = [] var temp3 = []
for k in range(0, temp2.size()): for k in range(0, temp2.size()):
temp3.append(int(temp2[k])) temp3.append(int(temp2[k]))
temp4.append(temp3) temp4.append(temp3)
new.append(temp4) new.append(temp4)
return new return new
func is_mapped(map, input): func is_mapped(map, input):
@ -62,7 +63,7 @@ func is_mapped(map, input):
#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 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]: if input >= v[Column.origin] && input <= v[Column.origin]+v[Column.range]:
return v[Column.destination] + (input-v[Column.origin]) return v[Column.destination] + (input-v[Column.origin])
return input return input
func map_input(data, seed): func map_input(data, seed):
@ -70,30 +71,31 @@ func map_input(data, seed):
for map in range(Maps.seedToSoil, Maps.humidityToLocation+1): for map in range(Maps.seedToSoil, Maps.humidityToLocation+1):
temp = is_mapped(data[map], temp) temp = is_mapped(data[map], temp)
#print("Output: %s %s" % [map, temp]) #print("Output: %s %s" % [map, temp])
return temp return temp
func _init(): func _init():
print("Hello!") print("Hello!")
var temp = file_load("res://input.txt").split("\n\n") var temp = file_load(OS.get_cmdline_user_args()[0]).split("\n\n")
input_clean(temp) input_clean(temp)
var input = input_format(temp) var input = input_format(temp)
#print(input[Maps.seeds]) #print(input[Maps.seeds])
#print(input[Maps.seedToSoil]) #print(input[Maps.seedToSoil])
temp.clear() temp.clear()
var seeds = input[0][0] var seeds = input[0][0]
print(seeds) print(seeds)
var minval = 2**32 var minval = 2**32
for seed in seeds: for seed in seeds:
var location = map_input(input, seed) var location = map_input(input, seed)
if location < minval: if location < minval:
minval = location minval = location
print("Answer: %s" % minval) print("Answer: %s" % minval)
quit()

View file

@ -1,15 +1,16 @@
#!/usr/bin/godot -s #!/usr/bin/godot -s
#d5p2.gd #d5p2.gd
extends Node3D #extends Node3D
extends SceneTree
enum Maps { enum Maps {
seeds, seeds,
seedToSoil, seedToSoil,
soilToFertilizer, soilToFertilizer,
fertilizerToWater, fertilizerToWater,
waterToLight, waterToLight,
lightToTemp, lightToTemp,
temptoHumidity, temptoHumidity,
humidityToLocation humidityToLocation
} }
@ -28,7 +29,7 @@ func strarr_to_arr(input):
var new = [] var new = []
for i in range(0, input.size()): for i in range(0, input.size()):
new.append(input[i]) new.append(input[i])
return new return new
func input_clean(input): func input_clean(input):
@ -43,18 +44,18 @@ func input_format(input):
for i in range(0, input.size()): for i in range(0, input.size()):
var temp = strarr_to_arr(input[i].split("\n")) var temp = strarr_to_arr(input[i].split("\n"))
var temp4 = [] var temp4 = []
for j in range(0, temp.size()): for j in range(0, temp.size()):
var temp2 = strarr_to_arr(temp[j].split(" ")) var temp2 = strarr_to_arr(temp[j].split(" "))
var temp3 = [] var temp3 = []
for k in range(0, temp2.size()): for k in range(0, temp2.size()):
temp3.append(int(temp2[k])) temp3.append(int(temp2[k]))
temp4.append(temp3) temp4.append(temp3)
new.append(temp4) new.append(temp4)
return new return new
func is_mapped(map, input): func is_mapped(map, input):
@ -62,14 +63,14 @@ func is_mapped(map, input):
#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 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]: if input >= v[Column.origin] && input <= v[Column.origin]+v[Column.range]:
return v[Column.destination] + (input-v[Column.origin]) return v[Column.destination] + (input-v[Column.origin])
return input return input
func is_revmapped(map, input): func is_revmapped(map, input):
for v in map: for v in map:
if input >= v[Column.destination] && input <= v[Column.destination]+v[Column.range]: if input >= v[Column.destination] && input <= v[Column.destination]+v[Column.range]:
return v[Column.origin] + (input-v[Column.destination]) return v[Column.origin] + (input-v[Column.destination])
return input return input
func map_input(data, seed): func map_input(data, seed):
@ -77,7 +78,7 @@ func map_input(data, seed):
for map in range(Maps.seedToSoil, Maps.humidityToLocation+1): for map in range(Maps.seedToSoil, Maps.humidityToLocation+1):
temp = is_mapped(data[map], temp) temp = is_mapped(data[map], temp)
#print("Output: %s %s" % [map, temp]) #print("Output: %s %s" % [map, temp])
return temp return temp
func revmap_input(data, location): func revmap_input(data, location):
@ -85,25 +86,25 @@ func revmap_input(data, location):
for map in range(Maps.humidityToLocation, Maps.seedToSoil-1, -1): for map in range(Maps.humidityToLocation, Maps.seedToSoil-1, -1):
temp = is_revmapped(data[map], temp) temp = is_revmapped(data[map], temp)
#print("Output: %s %s" % [map, temp]) #print("Output: %s %s" % [map, temp])
return temp return temp
func _init(): func _init():
print("Hello!") print("Hello!")
var temp = file_load("res://input.txt").split("\n\n") var temp = file_load(OS.get_cmdline_user_args()[0]).split("\n\n")
input_clean(temp) input_clean(temp)
var input = input_format(temp) var input = input_format(temp)
#print(input[Maps.seeds]) #print(input[Maps.seeds])
#print(input[Maps.seedToSoil]) #print(input[Maps.seedToSoil])
temp.clear() temp.clear()
var seeds = input[0][0] var seeds = input[0][0]
print(seeds) print(seeds)
var testlocation = 0 var testlocation = 0
while true: while true:
if testlocation % 10000 == 0: if testlocation % 10000 == 0:
@ -113,10 +114,11 @@ func _init():
if seed >= seeds[i*2] && seed <= seeds[i*2]+seeds[i*2+1]: if seed >= seeds[i*2] && seed <= seeds[i*2]+seeds[i*2+1]:
print("Answer 2: %s" % testlocation) print("Answer 2: %s" % testlocation)
print("Value is between seed %s and %s" % [seeds[i*2], seeds[i*2]+seeds[i*2+1]]) print("Value is between seed %s and %s" % [seeds[i*2], seeds[i*2]+seeds[i*2+1]])
quit()
return return
testlocation+=1 testlocation+=1