#include #include #include #include #include #include "util.h" using Seeds = std::vector; using Step = std::vector>; using Almanac = std::vector; constexpr char SEEDS[] = "seeds:"; std::pair parse_input() { Seeds seeds; Almanac almanac; std::ifstream input{ "resources/input.txt" }; if (input.is_open()) { std::string line; /* Seeds */ long seed; std::getline(input,line); std::istringstream sline{ line }; sline >> util::skip; while (sline >> seed) { seeds.push_back(seed); } /* Mappings */ Step step; long from, to, size; while (not std::getline(input,line).eof()) { if (line.empty()) continue; if (std::isdigit(line[0])) { std::istringstream sline{ line }; sline >> to >> from >> size; step.push_back(std::make_tuple(from, from + size, to - from)); } else if (not step.empty()) { almanac.push_back(std::move(step)); step = {}; } } almanac.push_back(std::move(step)); } input.close(); return std::make_pair(std::move(seeds), std::move(almanac)); } int main(void) { Seeds seeds; Almanac almanac; std::tie(seeds, almanac) = parse_input(); for (const auto& step : almanac) { for (auto& seed : seeds) { for (const auto& map : step) { if (std::get<0>(map) <= seed and seed < std::get<1>(map)) { seed += std::get<2>(map); break; // mappings in a step don't overlap } } } } std::cout << *std::min_element(seeds.begin(), seeds.end()) << std::endl; return 0; }