#include "common.h" #include #include namespace aoc2022 { struct blizzard { int x; int y; char c; blizzard next(int height, int width) { switch (c) { case '>': return {x + 1 == width - 1 ? 1 : x + 1, y, c}; case '<': return {x - 1 == 0 ? width - 2 : x - 1, y, c}; case '^': return {x, y - 1 == 0 ? height - 2 : y - 1, c}; case 'v': return {x, y + 1 == height - 1 ? 1 : y + 1, c}; default: break; } return *this; } friend bool operator<(blizzard b1, blizzard b2) { return b1.x < b2.x ? true : b1.x > b2.x ? false : b1.y < b2.y; } friend bool operator==(blizzard b1, blizzard b2) { return b1.x == b2.x && b1.y == b2.y; } }; struct valley { int width; int height; char* pixel; std::vector> blzs; valley(int w, int h) : width(w), height(h) { pixel = (char*)malloc(width * height); blzs.resize(1); } char& get(int h, int w) { return *(pixel + h * width + w); } void load(int h, line_view lv) { for (size_t i = 0; i < lv.length - 1; i++) { char c = *(lv.line + i); get(h, i) = c; if (c == '<' || c == '>' || c == '^' || c == 'v') { get(h, i) = '.'; blzs[0].emplace_back(blizzard{(int)i, h, c}); } } } std::vector at(int t) { t %= width * height; while (blzs.size() < (size_t)t + 1) { auto l = blzs.size() - 1; std::vector n = blzs[l]; for (size_t i = 0; i < n.size(); i++) { n[i] = n[i].next(height, width); } blzs.push_back(n); } return blzs[t]; } void print(int t) { std::map m; auto blz = at(t); for (auto& b : blz) { auto p = m.insert({b, 1}); if (!p.second) { p.first->second += 1; } } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { auto it = m.find(blizzard{x, y, '.'}); if (it == m.end()) { printf("%c", get(y, x)); } else { if (it->second > 1) { printf("%d", it->second); } else { printf("%c", it->first.c); } } } printf("\n"); } } }; std::pair day24(line_view); } // namespace aoc2022