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
|
#include "aoc.h"
namespace aoc2022 {
rock::three rock::t3;
int fall1(cave& cv, int dx) {
bool fall{true};
int n{0};
while (fall) {
auto p = cv.drop(cv.to({500,0}, dx));
rock::pos ns[] = {{p.x, p.y + 1}, {p.x - 1, p.y + 1}, {p.x + 1, p.y + 1}};
for (int i = 0; i < 3; i++) {
if (!cv.valid(ns[i])) {
fall = false;
}
}
if (fall) {
cv.get(p) = 'o';
n++;
}
}
return n;
}
int fall2(cave& cv, int dx) {
bool fall{true};
int n{0};
while (fall) {
auto p0 = cv.to({500,0}, dx);
auto p = cv.drop(p0);
cv.get(p) = 'o';
n++;
if (p.x == p0.x && p.y == p0.y) {
fall = false;
}
}
return n;
}
std::pair<int, int> day14(line_view file) {
std::vector<rock> rocks;
per_line(file, [&rocks](line_view lv){
rocks.emplace_back(lv);
return true;
});
// printf("%d %d %d\n", rock::t3.minx, rock::t3.maxx, rock::t3.maxy);
cave cv1(rock::t3.maxx - rock::t3.minx, rock::t3.maxy);
cave cv2(2 * (rock::t3.maxy + 2), rock::t3.maxy + 2);
int dx = (rock::t3.maxy + 2) - (500 - rock::t3.minx);
for(auto& r: rocks) {
// r.print();
cv1.mark(r);
cv2.mark(r, dx);
}
auto n1 = fall1(cv1, 0);
for(int i = 0; i < cv2.width; i++) {
cv2.get({i, cv2.height - 1}) = '#';
}
auto n2 = fall2(cv2, dx);
// cv2.print();
return {n1, n2};
}
}
|