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
#d5p1.gd
extends Node3D
#extends Node3D
extends SceneTree
enum Maps {
seeds,
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemp,
temptoHumidity,
seeds,
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemp,
temptoHumidity,
humidityToLocation
}
@ -28,7 +29,7 @@ func strarr_to_arr(input):
var new = []
for i in range(0, input.size()):
new.append(input[i])
return new
func input_clean(input):
@ -43,18 +44,18 @@ func input_format(input):
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):
@ -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 >= v[Column.origin] && input <= v[Column.origin]+v[Column.range]:
return v[Column.destination] + (input-v[Column.origin])
return input
func map_input(data, seed):
@ -70,30 +71,31 @@ func map_input(data, 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!")
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)
var input = input_format(temp)
#print(input[Maps.seeds])
#print(input[Maps.seedToSoil])
temp.clear()
var seeds = input[0][0]
print(seeds)
var minval = 2**32
for seed in seeds:
var location = map_input(input, seed)
if location < minval:
minval = location
print("Answer: %s" % minval)
quit()

View file

@ -1,15 +1,16 @@
#!/usr/bin/godot -s
#d5p2.gd
extends Node3D
#extends Node3D
extends SceneTree
enum Maps {
seeds,
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemp,
temptoHumidity,
seeds,
seedToSoil,
soilToFertilizer,
fertilizerToWater,
waterToLight,
lightToTemp,
temptoHumidity,
humidityToLocation
}
@ -28,7 +29,7 @@ func strarr_to_arr(input):
var new = []
for i in range(0, input.size()):
new.append(input[i])
return new
func input_clean(input):
@ -43,18 +44,18 @@ func input_format(input):
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):
@ -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 >= v[Column.origin] && input <= v[Column.origin]+v[Column.range]:
return v[Column.destination] + (input-v[Column.origin])
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])
return input
func map_input(data, seed):
@ -77,7 +78,7 @@ func map_input(data, 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 revmap_input(data, location):
@ -85,25 +86,25 @@ func revmap_input(data, location):
for map in range(Maps.humidityToLocation, Maps.seedToSoil-1, -1):
temp = is_revmapped(data[map], temp)
#print("Output: %s %s" % [map, temp])
return temp
func _init():
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)
var input = input_format(temp)
#print(input[Maps.seeds])
#print(input[Maps.seedToSoil])
temp.clear()
var seeds = input[0][0]
print(seeds)
var testlocation = 0
while true:
if testlocation % 10000 == 0:
@ -113,10 +114,11 @@ func _init():
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()
return
testlocation+=1