Added Day 5 Part 2
This commit is contained in:
parent
4b601b56a5
commit
6b928eab64
1 changed files with 122 additions and 0 deletions
122
day5/d5p2.gd
Normal file
122
day5/d5p2.gd
Normal file
|
@ -0,0 +1,122 @@
|
|||
#!/usr/bin/godot -s
|
||||
#d5p2.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:
|
||||
#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):
|
||||
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 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])
|
||||
|
||||
return temp
|
||||
|
||||
func _init():
|
||||
print("Hello!")
|
||||
|
||||
var temp = file_load("res://input.txt").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:
|
||||
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]])
|
||||
return
|
||||
|
||||
testlocation+=1
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in a new issue