-
Notifications
You must be signed in to change notification settings - Fork 0
/
main14.cpp
188 lines (143 loc) · 5.51 KB
/
main14.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <cassert>
typedef std::map<std::string, long> map_si;
void read_file(const std::string & path,
std::map<std::string, map_si> & reaction_map,
map_si & product_map, map_si & leftover_map)
{
assert(reaction_map.empty());
std::ifstream in_file(path);
if (in_file.is_open()) {
std::string line;
std::string arrow = " => ";
std::string sep = ", ";
while (getline(in_file, line, '\n')) {
std::istringstream lineStream(line);
std::string subline;
map_si ingreds;
size_t pos_space;
size_t pos_sep = line.find(sep);
while (pos_sep != std::string::npos) {
subline = line.substr(0, pos_sep);
pos_space = subline.find(' ');
ingreds.insert({subline.substr(pos_space+1),
std::stoi(subline.substr(0, pos_space))});
line.erase(0, pos_sep + sep.size());
pos_sep = line.find(sep);
}
size_t pos_arrow = line.find(arrow);
subline = line.substr(0, pos_arrow);
pos_space = subline.find(' ');
ingreds.insert({subline.substr(pos_space+1),
std::stoi(subline.substr(0, pos_space))});
subline = line.substr(pos_arrow + arrow.size());
pos_space = subline.find(' ');
product_map.insert({subline.substr(pos_space+1),
std::stoi(subline.substr(0, pos_space))});
leftover_map.insert({subline.substr(pos_space+1), 0});
reaction_map.insert({subline.substr(pos_space+1), ingreds});
}
} else {
std::cout << "Error: could not open file " << path << "\n";
}
}
void reset_leftover(map_si & leftover) {
for (auto & x: leftover) {
x.second = 0;
}
}
long get_total_ore_req(const std::map<std::string, map_si> & reaction_map,
const map_si & product_map,
const std::string & prod, const long & quantity,
map_si & leftover)
{
std::map<std::string, map_si>::const_iterator prod_it = reaction_map.find(prod);
long n = quantity / product_map.find(prod)->second;
if ((quantity % product_map.find(prod)->second) > leftover[prod]) {
n += (quantity % product_map.find(prod)->second != 0);
leftover[prod] += product_map.find(prod)->second - (quantity % product_map.find(prod)->second);
} else {
leftover[prod] -= (quantity % product_map.find(prod)->second);
}
long ore_count = 0;
map_si::const_iterator ingred_it = prod_it->second.begin();
while (ingred_it != prod_it->second.end()) {
if (ingred_it->first == "ORE") {
ore_count += n*(ingred_it->second);
} else {
ore_count += get_total_ore_req(reaction_map, product_map,
ingred_it->first,
n*(ingred_it->second),
leftover);
}
ingred_it++;
}
return ore_count;
}
long get_max_fuel(const std::map<std::string, map_si> & reaction_map,
const map_si & product_map,
const long & tot_ore,
const map_si & leftover_init)
{
map_si leftover(leftover_init);
long ore_count = get_total_ore_req(reaction_map, product_map,
"FUEL", 1, leftover);
reset_leftover(leftover);
long max_fuel = tot_ore / ore_count; // initial guess
long step = max_fuel;
while (step != 1) {
reset_leftover(leftover);
if (step > 2){
step = step / 2 + (step % 2 != 0);
} else {
step = 1;
}
if (ore_count > tot_ore) {
max_fuel -= step;
} else if (ore_count <= tot_ore) {
max_fuel += step;
}
ore_count = get_total_ore_req(reaction_map, product_map,
"FUEL", max_fuel, leftover);
}
while (ore_count > tot_ore) {
reset_leftover(leftover);
max_fuel -= step;
ore_count = get_total_ore_req(reaction_map, product_map,
"FUEL", max_fuel, leftover);
}
return max_fuel;
}
// ----------------
// - - - MAIN - - -
// ----------------
int main() {
std::string data_path = "./input_files/in14.txt";
std::map<std::string, map_si> reaction_map;
map_si product_map;
map_si leftover;
map_si quant_map;
read_file(data_path, reaction_map, product_map, leftover);
{
long ore_count = get_total_ore_req(reaction_map, product_map,
"FUEL", 1, leftover);
std::cout << "\n - - - PART I - - -\n";
std::cout << "Total required ORE for 1 FUEL: " << ore_count << "\n";
}
{
reset_leftover(leftover);
long tot_ore_collected = 1e12;
long max_fuel = get_max_fuel(reaction_map, product_map,
tot_ore_collected, leftover);
std::cout << "\n - - - PART II - - -\n";
std::cout << "Total amount of FUEL from collected ORE: " << max_fuel << "\n";
long ore_count = get_total_ore_req(reaction_map, product_map,
"FUEL", max_fuel, leftover);
std::cout << "using " << ore_count << " ORE\n";
}
}