diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2019/day10/aoc.cpp | 106 | ||||
-rw-r--r-- | src/2019/day10/aoc.h | 13 | ||||
-rw-r--r-- | src/2022/day5/input | 2 |
3 files changed, 108 insertions, 13 deletions
diff --git a/src/2019/day10/aoc.cpp b/src/2019/day10/aoc.cpp index fbcb049..60de3ea 100644 --- a/src/2019/day10/aoc.cpp +++ b/src/2019/day10/aoc.cpp @@ -1,8 +1,72 @@ #include "aoc.h" #include <algorithm> +#include <vector> namespace aoc2019 { +struct posd { + belt::pos p; + belt::distance d; + int count = 0; +}; + +enum dim { + xe0yl0, + xg0yl0, + xg0ye0, + xg0yg0, + xe0yg0, + xl0yg0, + xl0ye0, + xl0yl0 +}; + +dim dimention(belt::distance d) { + if (d.dx == 0 && d.dy < 0) return xe0yl0; + if (d.dx > 0 && d.dy < 0) return xg0yl0; + if (d.dx > 0 && d.dy == 0) return xg0ye0; + if (d.dx > 0 && d.dy > 0) return xg0yg0; + if (d.dx == 0 && d.dy > 0) return xe0yg0; + if (d.dx < 0 && d.dy > 0) return xl0yg0; + if (d.dx < 0 && d.dy == 0) return xl0ye0; + return xl0yl0; +} + +bool dimcmp(dim m, belt::distance d1, belt::distance d2) { + int x1 = std::abs(d1.dx); + int y1 = std::abs(d1.dy); + int x2 = std::abs(d2.dx); + int y2 = std::abs(d2.dy); + + switch (m) { + case xe0yl0: return y1 < y2; + case xg0yl0: return y1 > y2 ? true : y1 < y2 ? false : x1 < x2; + case xg0ye0: return x1 < x2; + case xg0yg0: return y1 < y2 ? true : y1 > y2 ? false : x1 < x2; + case xe0yg0: return y1 < y2; + case xl0yg0: return y1 > y2 ? true : y1 < y2 ? false : x1 < x2; + case xl0ye0: return x1 < x2; + case xl0yl0: return y1 < y2 ? true : y1 > y2 ? false : x1 < x2; + } + return false; +} + +belt::pos vaporize(belt b, belt::pos& m, std::vector<posd>& ps, int* count) { + belt bx = b; + for (auto& dp : ps) { + if (dp.count == 0 && !bx.blocked(dp.p, m)) { + dp.count = 1; + b.get(dp.p) = '.'; + *count += 1; + printf("%d asteroid to be vaporized is at (%d, %d)\n", *count, dp.p.x, dp.p.y); + if (*count == 200) { + return dp.p; + } + } + } + return vaporize(b, m, ps, count); +} + std::pair<int, int> day10(line_view file) { belt b; int r{0}; @@ -11,22 +75,42 @@ std::pair<int, int> day10(line_view file) { return true; }); - // b.print(); + std::vector<posd> ps; + b.iterate([&ps, &b](belt::pos p){ + if (b.get(p) == '#') { + posd d; + d.p = p; + b.count(p, &d.count); + ps.push_back(d); + } + }); + + std::sort(ps.begin(), ps.end(), [](const posd& d1, const posd& d2){ + return d1.count > d2.count; + }); - int observe{0}; - int max{0}; + int max = ps[0].count; + belt::pos monitor = ps[0].p; - b.iterate([&observe, &max, &b](belt::pos p){ - if (b.get(p) == '#') { - observe = 0; - b.count(p, &observe); - if (observe > max) { - max = observe; + for (auto& a : ps) { + a.d = b.dist(a.p, monitor); + a.count = a.p == monitor ? 1 : 0; // mark as not lazered + } + + std::sort(ps.begin(), ps.end(), [](const posd& d1, const posd& d2) { + dim m1 = dimention(d1.d); + dim m2 = dimention(d2.d); + if (m1 == m2) { + return dimcmp(m1, d1.d, d2.d); + } + else { + return (int) m1 < (int) m2; } - } }); - return {max, 0}; + int count{0}; + belt::pos xp = vaporize(b, monitor, ps, &count); + return {max, xp.x * 100 + xp.y}; } } // namespace aoc2019 diff --git a/src/2019/day10/aoc.h b/src/2019/day10/aoc.h index d3cdb76..02de28b 100644 --- a/src/2019/day10/aoc.h +++ b/src/2019/day10/aoc.h @@ -5,7 +5,7 @@ namespace aoc2019 { struct belt { - static constexpr int grid = 34; + static constexpr int grid = 20; struct distance { int dx; @@ -27,6 +27,13 @@ struct belt { memcpy(map, b.map, grid * grid); } + belt& operator=(const belt& b) { + if (this != &b) { + memcpy(map, b.map, grid * grid); + } + return *this; + } + belt() {} char& get(int x, int y) { @@ -50,6 +57,10 @@ struct belt { return distance {d.dx / g, d.dy / g}; } + distance dist(pos px, pos p) const noexcept { + return distance{px.x - p.x, px.y - p.y}; + } + bool valid (pos p) const noexcept { return p.x >= 0 && p.x < grid && p.y >= 0 && p.y < grid; } diff --git a/src/2022/day5/input b/src/2022/day5/input index cbdf94c..0367a49 100644 --- a/src/2022/day5/input +++ b/src/2022/day5/input @@ -6,7 +6,7 @@ [R] [H] [T] [D] [L] [D] [D] [B] [W] [T] [C] [L] [H] [Q] [J] [B] [T] [N] [G] [G] [C] [J] [P] [P] [Z] [R] [H] -1 2 3 4 5 6 7 8 9 + 1 2 3 4 5 6 7 8 9 move 3 from 4 to 3 move 3 from 8 to 6 |