thomas_2023/day_5/Main.gd

109 lines
1.9 KiB
GDScript3
Raw Normal View History

2023-12-09 13:20:02 -05:00
extends Control
func _ready():
pass
func read_map(lines, i):
var map = []
while true:
var line = lines[i]
if line == '':
break
2023-12-09 14:31:28 -05:00
i += 1
2023-12-09 13:20:02 -05:00
var map_entry_string = line.split(' ')
var entry = MapEntry.new()
2023-12-09 14:31:28 -05:00
entry.dst_start = int(map_entry_string[0])
entry.src_start = int(map_entry_string[1])
entry.range = int(map_entry_string[2])
map.append(entry)
2023-12-09 13:20:02 -05:00
2023-12-09 14:31:28 -05:00
return [map, i]
2023-12-09 13:20:02 -05:00
func parse_input(input):
var lines = input.split('\n')
var i = 0
2023-12-09 14:31:28 -05:00
var maps = Maps.new()
2023-12-09 13:20:02 -05:00
# Parse seeds
var seeds_str = lines[i].split(' ').slice(1)
var seeds = []
for s in seeds_str:
seeds.append(int(s))
# Skip to seed-to-soil map
i += 3
2023-12-09 14:31:28 -05:00
var map_i = read_map(lines, i)
maps.seed2soil = map_i[0]
i = map_i[1]
# Skip to soil-to-fertilizer map
i += 2
map_i = read_map(lines, i)
maps.soil2fertilizer = map_i[0]
i = map_i[1]
# Skip to fertilizer-to-water map
i += 2
map_i = read_map(lines, i)
maps.fertilizer2water = map_i[0]
i = map_i[1]
# Skip to water-to-light map
i += 2
map_i = read_map(lines, i)
maps.water2light = map_i[0]
i = map_i[1]
# Skip to light-to-temperature map
i += 2
map_i = read_map(lines, i)
maps.light2temperature = map_i[0]
i = map_i[1]
# Skip to temperature-to-humidity map
i += 2
map_i = read_map(lines, i)
maps.temperature2humidity = map_i[0]
i = map_i[1]
# Skip to humidity-to-location map
i += 2
map_i = read_map(lines, i)
maps.humidity2location = map_i[0]
i = map_i[1]
return [seeds, maps]
2023-12-09 13:20:02 -05:00
func _on_part1():
2023-12-09 14:31:28 -05:00
var input = $TextEdit.text + '\n' # Safety newline :)
var seeds_maps = parse_input(input)
var seeds = seeds_maps[0]
var maps = seeds_maps[1]
print(seeds)
print(maps)
var overall_closest = INF
for seed in seeds:
var closest = maps.find_closest(seed)
overall_closest = min(overall_closest, closest)
print("Closest location: " + str(overall_closest))
2023-12-09 13:20:02 -05:00
func _on_part2():
var input = $TextEdit.text