diff options
Diffstat (limited to 'src/2022/day24/aoc.h')
-rw-r--r-- | src/2022/day24/aoc.h | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/src/2022/day24/aoc.h b/src/2022/day24/aoc.h index 920f69c..455079f 100644 --- a/src/2022/day24/aoc.h +++ b/src/2022/day24/aoc.h @@ -1,7 +1,95 @@ #include "common.h" #include <vector> +#include <map> 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<blizzard> blz; + + valley(int w, int h): width(w), height(h) { + pixel = (char*) malloc(width * height); + } + + 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) = '.'; + blz.emplace_back(blizzard{(int) i, h, c}); + } + } + } + + void next() { + std::vector<blizzard> n{blz.size()}; + for(size_t i = 0; i < blz.size(); i++) { + n[i] = blz[i].next(height, width); + } + blz = n; + } + + void print() { + std::map<blizzard, int> m; + 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<int, int> day24(line_view); } |