Day 5
This commit is contained in:
parent
e400e1a563
commit
959e247258
3 changed files with 320 additions and 2 deletions
87
d5/main.cxx
Normal file
87
d5/main.cxx
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <limits>
|
||||
#include <omp.h>
|
||||
|
||||
struct MapRow
|
||||
{
|
||||
long source;
|
||||
long destination;
|
||||
long range;
|
||||
};
|
||||
|
||||
long findDestination(long seed, const std::vector<std::vector<MapRow>> &maps)
|
||||
{
|
||||
long location{seed};
|
||||
for (const auto &map : maps)
|
||||
{
|
||||
for (const auto &mr : map)
|
||||
{
|
||||
if (location >= mr.source && location < mr.source + mr.range)
|
||||
{
|
||||
location = mr.destination + (location - mr.source);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream file{"../d5/input.txt"};
|
||||
std::string line, ignored;
|
||||
std::vector<std::pair<long, long>> seeds;
|
||||
std::vector<std::vector<MapRow>> maps{};
|
||||
long mapIndex{-1};
|
||||
|
||||
std::getline(file, line);
|
||||
std::stringstream seedSS(line);
|
||||
seedSS >> ignored;
|
||||
while (seedSS.tellg() != -1)
|
||||
{
|
||||
long seedStart, seedRange;
|
||||
seedSS >> seedStart >> seedRange;
|
||||
seeds.emplace_back(seedStart, seedRange);
|
||||
}
|
||||
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
std::stringstream iss(line);
|
||||
|
||||
if (isalpha(line[0]))
|
||||
{
|
||||
mapIndex += isalpha(line[0]);
|
||||
maps.push_back({});
|
||||
}
|
||||
|
||||
while (isdigit(line[0]) && iss.tellg() != -1)
|
||||
{
|
||||
MapRow mr;
|
||||
iss >> mr.destination >> mr.source >> mr.range;
|
||||
maps[mapIndex].emplace_back(mr);
|
||||
}
|
||||
}
|
||||
|
||||
long result = std::numeric_limits<long>::max();
|
||||
int i, j;
|
||||
|
||||
#pragma omp parallel for schedule(static) private(i, j) shared(seeds, result, maps)
|
||||
for (i = 0; i < seeds.size(); i++)
|
||||
{
|
||||
for (j = 0; j < seeds[i].second; j++)
|
||||
{
|
||||
long destination = findDestination(seeds[i].first + j, maps);
|
||||
result = std::min(result, destination);
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << result << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue