1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <tuple>
#include <vector>
#include "util.h"
using SeedRange = std::tuple<int,long,long>;
using Seeds = std::set<SeedRange>;
using Map = std::tuple<long,long,long>;
using Step = std::vector<Map>;
using Almanac = std::vector<Step>;
constexpr char SEEDS[] = "seeds:";
std::pair<Seeds,Almanac> parse_input()
{
Seeds seeds;
Almanac almanac;
std::ifstream input{ "resources/input.txt" };
if (input.is_open())
{
std::string line;
long from, to, size;
/* Seeds */
std::getline(input,line);
std::istringstream sline{ line };
sline >> util::skip<SEEDS>;
while (sline >> from >> size)
{
seeds.insert(std::make_tuple(0, from, from + size));
}
/* Mappings */
Step step;
while (not std::getline(input,line).eof())
{
if (line.empty()) continue;
if (std::isdigit(line[0]))
{
std::istringstream{ line } >> 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));
}
Seeds map_to_range(int step, const SeedRange& seed_range, const Map& map)
{
int s;
long rbegin, rend;
long mbegin, mend, diff;
std::tie(s, rbegin, rend) = seed_range;
std::tie(mbegin, mend, diff) = map;
if (mbegin <= rbegin)
{
if (mend >= rend)
{
return {
std::make_tuple(step + 1, rbegin + diff, rend + diff)
};
}
else if (mend > rbegin)
{
return {
std::make_tuple(step + 1, rbegin + diff, mend + diff),
std::make_tuple(step, mend, rend),
};
}
}
else if (mbegin < rend)
{
if (mend >= rend)
{
return {
std::make_tuple(step, rbegin, mbegin),
std::make_tuple(step + 1, mbegin + diff, rend + diff),
};
}
else
{
return {
std::make_tuple(step, rbegin, mbegin),
std::make_tuple(step + 1, mbegin + diff, mend + diff),
std::make_tuple(step, mend, rend),
};
}
}
return { seed_range };
}
int main(void)
{
Seeds seeds;
Almanac almanac;
std::tie(seeds, almanac) = parse_input();
for (int s = 0; s < almanac.size(); ++s)
{
const auto& step = almanac[s];
for (const auto& map : step)
{
std::set<SeedRange> new_seeds;
for(auto& seed : seeds)
{
if (std::get<0>(seed) <= s)
{
auto res = map_to_range(s, seed, map);
new_seeds.insert(res.begin(), res.end());
}
else
{
new_seeds.insert(seed);
}
}
seeds = std::move(new_seeds);
}
}
auto min = std::min_element(seeds.cbegin(), seeds.cend(),
[](auto s1, auto s2) { return std::get<1>(s1) < std::get<1>(s2); });
std::cout << std::get<1>(*min) << std::endl;
return 0;
}
|