#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 day14(line_view file) { std::vector 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}; } }