Repository navigation
Expand file tree
/
Copy path14b.cpp
More file actions
154 lines (134 loc) · 3.3 KB
/
Copy path14b.cpp
File metadata and controls
154 lines (134 loc) · 3.3 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
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
#include <assert.h>
#include <fstream>
#include <iostream>
#include <ranges>
#include <sstream>
#include <vector>
const std::string INPUT_FILE = "./data/14.txt";
typedef std::vector<std::vector<bool>> Grid;
typedef std::vector<std::vector<std::tuple<int, int>>> Input;
class Cave {
public:
Grid grid;
int max_x;
int min_x;
int max_y;
int min_y;
size_t grains = 0;
Cave(const Input &input) {
max_x = 500;
min_x = 500;
max_y = 0;
min_y = 0;
for (auto &structure : input) {
for (auto &[a, b] : structure) {
max_x = std::max(max_x, a);
min_x = std::min(min_x, a);
max_y = std::max(max_y, b);
min_y = std::min(min_y, b);
}
}
assert(min_y == 0);
grid = Grid(max_y - min_y + 1, std::vector<bool>(max_x - min_x + 1));
for (auto &structure : input) {
auto [xx, yy] = structure.front();
for (const auto &[a, b] : structure | std::views::drop(1)) {
if (xx == a) {
for (int i = std::min(yy, b); i <= std::max(yy, b); ++i) {
grid[i - min_y][a - min_x] = true;
}
} else {
assert(yy == b);
for (int i = std::min(xx, a); i <= std::max(xx, a); ++i) {
grid[b - min_y][i - min_x] = true;
}
}
xx = a;
yy = b;
}
}
}
bool drop() {
size_t x = 500 - min_x;
size_t y = 0;
if (grid[y][x]) {
return false;
}
while (0 <= x && x < grid[0].size() && y < grid.size()) {
// std::cout << "x : " << x << " , y : " << y << std::endl;
if (y == grid.size() - 1 || !grid[y + 1][x]) {
++y;
continue;
}
if (x == 0 || !grid[y + 1][x - 1]) {
++y;
--x;
continue;
}
if (x == grid[0].size() - 1 || !grid[y + 1][x + 1]) {
++y;
++x;
continue;
}
grid[y][x] = true;
return true;
}
return false;
}
size_t simulate() {
while (drop())
++grains;
return grains;
}
void print() const {
for (const auto &row : grid) {
std::cout << std::endl;
for (const auto &val : row) {
std::cout << ((val) ? "#" : ".");
}
}
std::cout << std::endl;
}
};
Input read() {
std::ifstream infile(INPUT_FILE);
Input input;
std::string line;
while (std::getline(infile, line)) {
std::stringstream iss(line);
std::string chunk;
input.push_back({});
while (std::getline(iss, chunk, ' ')) {
std::stringstream chunkss(chunk);
int a, b;
char _;
chunkss >> a >> _ >> b;
// std::cout << a << " " << b << std::endl;
input.back().emplace_back(a, b);
std::getline(iss, chunk, ' ');
}
}
return input;
}
int main() {
Input input = read();
auto max_y = 0;
for (auto &structure : input) {
for (auto &[a, b] : structure) {
max_y = std::max(max_y, b);
}
}
const auto WIDTH = 164;
const auto infinite_floor = std::vector<std::tuple<int, int>>({
{500 - WIDTH, max_y + 2}, {500 + WIDTH, max_y + 2}
});
input.emplace_back(infinite_floor);
Cave cave(input);
std::cout << cave.max_x << " " << cave.min_x << std::endl;
std::cout << cave.max_y << " " << cave.min_y << std::endl;
cave.print();
auto res = cave.simulate();
cave.print();
std::cout << "Result: " << res << std::endl;
return 0;
}