advent2024

Advent of Code 2024
git clone git://bsandro.tech/advent2024
Log | Files | Refs

commit 328ed172dcc9451f8ab3bb440f2374666af52432
parent 4343c0b16e751b1ec4e02f3e2c45ccf6dafe4d77
Author: bsandro <email@bsandro.tech>
Date:   Thu, 26 Dec 2024 09:13:17 +0200

Day 12 p2

Diffstat:
Mday12/main.cpp | 60+++++++++---------------------------------------------------
1 file changed, 9 insertions(+), 51 deletions(-)

diff --git a/day12/main.cpp b/day12/main.cpp @@ -49,10 +49,8 @@ public: Vec2 pd(p.first+dir.first, p.second+dir.second); if (this->has(pd)) { cnt--; - //std::printf("[%d,%d] has neighbor [%d,%d]\n", p.first, p.second, pd.first, pd.second); } } - //std::printf("(%ld)\n", cnt); per+=cnt; } return per; @@ -60,52 +58,16 @@ public: int64_t sides() const { int64_t cnt = 0; - std::vector<Vec2> n,e,s,w; for (const auto &p : *this) { - if (!this->has(Vec2(p.first+1, p.second))) e.push_back(p); - if (!this->has(Vec2(p.first-1, p.second))) w.push_back(p); - if (!this->has(Vec2(p.first, p.second+1))) s.push_back(p); - if (!this->has(Vec2(p.first, p.second-1))) n.push_back(p); - } - std::sort(n.begin(), n.end(), [](auto &p1, auto &p2){ return p1.second<p2.second; }); - std::sort(s.begin(), s.end(), [](auto &p1, auto &p2){ return p1.second<p2.second; }); - std::sort(e.begin(), e.end(), [](auto &p1, auto &p2){ return p1.first<p2.first; }); - std::sort(w.begin(), w.end(), [](auto &p1, auto &p2){ return p1.first<p2.first; }); - - /*std::printf("east:"); - for (auto &p:e) { - std::printf("[%d,%d]", p.first, p.second); - } - std::printf("\n");*/ + if (!this->has(Vec2(p.first+1, p.second))&&!this->has(Vec2(p.first, p.second-1))) cnt++; + if (!this->has(Vec2(p.first+1, p.second))&&!this->has(Vec2(p.first, p.second+1))) cnt++; + if (!this->has(Vec2(p.first-1, p.second))&&!this->has(Vec2(p.first, p.second+1))) cnt++; + if (!this->has(Vec2(p.first-1, p.second))&&!this->has(Vec2(p.first, p.second-1))) cnt++; - int v=-1; - int prev=-1; - for (auto &p : n) { - if (p.second!=v || std::abs(prev-p.first)>1) { cnt++; } - v=p.second; - prev=p.first; - } - v=-1; - prev=-1; - for (auto &p : s) { - if (p.second!=v || std::abs(prev-p.first)>1) cnt++; - v=p.second; - prev=p.first; - } - v=-1; - prev=-1; - for (auto &p : e) { - if (p.first!=v || std::abs(prev-p.second)>1) cnt++; - v=p.first; - prev=p.second; - } - //std::printf("north+south+east count:%ld\n", cnt); - v=-1; - prev=-1; - for (auto &p : w) { - if (p.first!=v || std::abs(prev-p.second)>1) cnt++; - v=p.first; - prev=p.second; + if (!this->has(Vec2(p.first+1, p.second-1))&&this->has(Vec2(p.first+1, p.second))&&this->has(Vec2(p.first, p.second-1))) cnt++; + if (!this->has(Vec2(p.first+1, p.second+1))&&this->has(Vec2(p.first+1, p.second))&&this->has(Vec2(p.first, p.second+1))) cnt++; + if (!this->has(Vec2(p.first-1, p.second+1))&&this->has(Vec2(p.first-1, p.second))&&this->has(Vec2(p.first, p.second+1))) cnt++; + if (!this->has(Vec2(p.first-1, p.second-1))&&this->has(Vec2(p.first-1, p.second))&&this->has(Vec2(p.first, p.second-1))) cnt++; } return cnt; } @@ -116,8 +78,7 @@ public: } } }; -// 1046941 too high -// 918855 too high + template<typename T> T read_file(const std::string &path) { std::ifstream ifs(path, std::ios::binary); @@ -152,9 +113,6 @@ std::pair<int64_t, int64_t> solve(Data &input [[ maybe_unused ]]) { if (!processed.has(Vec2(x, y))) { Plot p = findPlot(input, Vec2(x,y)); processed.insert(p.begin(), p.end()); - std::printf("-------------\nfound plot (%c):\n", input[y][x]); - p.print(); - std::printf("\nsides: %ld, area: %ld\n", p.sides(), p.area()); p1sum += p.area() * p.perimeter(); p2sum += p.area() * p.sides(); }