split_maps =split(all_maps, "\n\n")all_maps_names =String[];all_maps_data = DataFrame[];for each_map in split_maps map_name , map_data =split(each_map, ":\n");# get the name without explicit " map" map_name = (strip(map_name))[1:(end-4)];push!(all_maps_names, map_name); map_data = CSV.read(IOBuffer(map_data), DataFrame; header = ["dest_start", "src_start", "range"], delim =" " );sort!(map_data, :src_start); map_data =transform( map_data, [:dest_start, :range] => ((d, r) -> d + r) =>:dest_end, [:src_start, :range] => ((s, r) -> s + r) =>:src_end );push!(all_maps_data, map_data);end
Now I have all these maps: [“seed-to-soil”, “soil-to-fertilizer”, “fertilizer-to-water”, “water-to-light”, “light-to-temperature”, “temperature-to-humidity”, “humidity-to-location”].
The problem
I need to find which seed goes with which soil, which soil goes with which fertilizer, etc. And then, find the smallest location number!
functionget_dest_number(src_number::Int64, map_number::Int64) cur_map = all_maps_data[map_number]; filtered_map =subset(cur_map, [:src_start, :src_end] => (x,y) -> x .<= src_number .<= y);ifnrow(filtered_map) ==0# the destination number is the same as src numberreturn src_numberend offset = src_number - filtered_map.src_start[1];return filtered_map.dest_start[1] + offsetend