-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_14a.cpp
115 lines (105 loc) · 3.76 KB
/
day_14a.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
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
long long OreRequired(
const long long fuel,
std::unordered_map<
std::string,
std::pair<long long, std::unordered_map<std::string, long long>>>&
reactions) {
std::unordered_map<std::string, long long> to_create{{"FUEL", fuel}};
std::unordered_map<std::string, long long> spares;
long long ore_units = 0;
while (!to_create.empty()) {
const auto product = std::begin(to_create)->first;
if (to_create[product] < spares[product]) {
spares[product] -= to_create[product];
to_create.erase(product);
continue;
}
const auto units_needed = to_create[product] - spares[product];
spares[product] = 0;
to_create.erase(product);
const auto units_produced_per_reaction = reactions[product].first;
long long num_reactions = 0;
if (units_needed % units_produced_per_reaction == 0) {
num_reactions = units_needed / units_produced_per_reaction;
} else {
num_reactions = (units_needed / units_produced_per_reaction) + 1;
}
spares[product] +=
(num_reactions * units_produced_per_reaction) - units_needed;
for (const auto& [reactant, reactant_units] : reactions[product].second) {
if (reactant == "ORE") {
ore_units += reactant_units * num_reactions;
} else {
to_create[reactant] += reactant_units * num_reactions;
}
}
}
return ore_units;
}
std::tuple<std::string, int> GetChemicalAndQuantity(const std::string& input) {
const auto space_ind = input.find(' ');
const auto quantity = std::stoi(input.substr(0, space_ind));
const auto ingredient = input.substr(space_ind + 1, input.size() - space_ind);
return {ingredient, quantity};
}
std::unordered_map<
std::string,
std::pair<long long, std::unordered_map<std::string, long long>>>
ParseInput(const std::vector<std::string>& input_file) {
std::unordered_map<
std::string,
std::pair<long long, std::unordered_map<std::string, long long>>>
reactions;
for (const auto& input_str : input_file) {
const std::string delimiter1 = " => ";
const std::size_t delim_ind = input_str.find(delimiter1);
const auto output =
input_str.substr(delim_ind + delimiter1.size(),
input_str.size() - delim_ind - delimiter1.size());
const auto out_chem_q = GetChemicalAndQuantity(output);
reactions[std::get<0>(out_chem_q)] = {};
reactions[std::get<0>(out_chem_q)].first = std::get<1>(out_chem_q);
const auto inputs = input_str.substr(0, delim_ind);
const std::string delimiter2 = ", ";
size_t start = 0;
size_t end = inputs.find(delimiter2);
while (end != std::string::npos) {
const auto in_chem_q =
GetChemicalAndQuantity(inputs.substr(start, end - start));
reactions[std::get<0>(out_chem_q)].second.insert(
{std::get<0>(in_chem_q), std::get<1>(in_chem_q)});
start = end + delimiter2.size();
end = inputs.find(delimiter2, start);
}
const auto in_chem_q =
GetChemicalAndQuantity(inputs.substr(start, end - start));
reactions[std::get<0>(out_chem_q)].second.insert(
{std::get<0>(in_chem_q), std::get<1>(in_chem_q)});
}
return reactions;
}
int main(int argc, char* argv[]) {
// Get input
std::string input = "../input/day_14_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string input_line;
std::vector<std::string> input_file;
while (std::getline(file, input_line)) {
input_file.push_back(input_line);
}
// Solve
auto reactions = ParseInput(input_file);
const auto ore_for_1_fuel = OreRequired(1, reactions);
std::cout << ore_for_1_fuel << '\n';
return ore_for_1_fuel;
}