-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathday_12a.cpp
More file actions
75 lines (70 loc) · 2.29 KB
/
day_12a.cpp
File metadata and controls
75 lines (70 loc) · 2.29 KB
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
#include <charconv>
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
struct Shape {
int id;
std::vector<std::string> display;
int area;
};
struct Region {
int dim1;
int dim2;
std::vector<int> count;
};
int main(int argc, char* argv[])
{
std::string input = "../input/day_12_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::vector<Shape> shapes;
std::vector<Region> regions;
int shape = -1;
while(std::getline(file, line)) {
if (line.back() == ':') {
shapes.emplace_back();
shapes.back().id = std::stoi(line.substr(0, line.size() - 1));
} else if (line[0] == '.' || line[0] == '#') {
shapes.back().display.push_back(line);
} else if (line.find('x') != std::string::npos) {
auto x_idx = line.find('x');
auto colon_idx = line.find(':', x_idx + 1);
regions.emplace_back();
regions.back().dim1 = std::stoi(line.substr(0, x_idx));
regions.back().dim2 = std::stoi(line.substr(x_idx+1, colon_idx - x_idx - 1));
regions.back().count = line.substr(colon_idx+2, line.size() - colon_idx - 2)
| std::views::split(' ')
| std::views::transform([](const auto& view) {
int value;
std::from_chars(view.data(), view.data() + view.size(), value);
return value;
})
| std::ranges::to<std::vector<int>>();
}
}
for (auto& s : shapes) {
s.area = 0;
for (const auto& row : s.display) {
for (const auto ele : row) {
if (ele == '#') s.area++;
}
}
}
int n_regions_fit_presents = 0;
for (const auto& r : regions) {
int total_used_area = 0;
for (auto [idx, n] : std::views::enumerate(r.count)) {
total_used_area += (shapes[idx].area * n);
}
if ((r.dim1 * r.dim2) - total_used_area >= 0) {
n_regions_fit_presents++;
}
}
std::cout << n_regions_fit_presents << '\n';
return 0;
}