aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/2022/day24/aoc.cpp25
-rw-r--r--src/2022/day24/aoc.h59
2 files changed, 38 insertions, 46 deletions
diff --git a/src/2022/day24/aoc.cpp b/src/2022/day24/aoc.cpp
index 1e55468..825da1e 100644
--- a/src/2022/day24/aoc.cpp
+++ b/src/2022/day24/aoc.cpp
@@ -1,6 +1,6 @@
#include "aoc.h"
-#include <set>
#include <deque>
+#include <set>
namespace aoc2022 {
@@ -12,9 +12,7 @@ struct pos {
friend bool operator<(pos p1, pos p2) {
return p1.x < p2.x ? true : p1.x > p2.x ? false : p1.y < p2.y ? true : p1.y > p2.y ? false : p1.t < p2.t;
}
- bool operator==(pos p) const noexcept {
- return x == p.x && y == p.y;
- }
+ bool operator==(pos p) const noexcept { return x == p.x && y == p.y; }
};
bool is_valid(pos p, valley& v, std::set<pos>& visited) {
@@ -27,8 +25,9 @@ bool is_valid(pos p, valley& v, std::set<pos>& visited) {
auto blz = v.at(px.t);
auto find = [&blz](blizzard b) -> bool {
- for(auto& bx : blz) {
- if (bx == b) return true;
+ for (auto& bx : blz) {
+ if (bx == b)
+ return true;
}
return false;
};
@@ -42,7 +41,7 @@ int expedition(pos p, pos target, valley& v, std::set<pos>& visited) {
std::deque<pos> q;
q.push_back(p);
- while(!q.empty()) {
+ while (!q.empty()) {
auto size = q.size();
while (size-- > 0) {
pos px = q.front();
@@ -52,11 +51,8 @@ int expedition(pos p, pos target, valley& v, std::set<pos>& visited) {
return px.t;
}
pos ps[5] = {
- {px.x, px.y+1, px.t + 1},
- {px.x+1, px.y, px.t + 1},
- {px.x, px.y-1, px.t + 1},
- {px.x-1, px.y, px.t + 1},
- {px.x, px.y, px.t + 1},
+ {px.x, px.y + 1, px.t + 1}, {px.x + 1, px.y, px.t + 1}, {px.x, px.y - 1, px.t + 1},
+ {px.x - 1, px.y, px.t + 1}, {px.x, px.y, px.t + 1},
};
for (int i = 0; i < 5; i++) {
if (is_valid(ps[i], v, visited)) {
@@ -70,7 +66,7 @@ int expedition(pos p, pos target, valley& v, std::set<pos>& visited) {
std::pair<int, int> day24(line_view file) {
// valley v{8,6}; //sample
- valley v{152,22};
+ valley v{152, 22};
int height{0};
per_line(file, [&v, &height](line_view lv) {
@@ -88,5 +84,4 @@ std::pair<int, int> day24(line_view file) {
// printf("%d %d %d\n", m1, m2, m3);
return {m1, m3};
}
-}
-
+} // namespace aoc2022
diff --git a/src/2022/day24/aoc.h b/src/2022/day24/aoc.h
index 9724324..3e7b590 100644
--- a/src/2022/day24/aoc.h
+++ b/src/2022/day24/aoc.h
@@ -1,6 +1,6 @@
#include "common.h"
-#include <vector>
#include <map>
+#include <vector>
namespace aoc2022 {
@@ -10,22 +10,23 @@ struct blizzard {
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;
+ 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;
- }
+ 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 {
@@ -35,29 +36,27 @@ struct valley {
char* pixel;
std::vector<std::vector<blizzard>> blzs;
- valley(int w, int h): width(w), height(h) {
- pixel = (char*) malloc(width * height);
+ 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);
- }
+ 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++) {
+ 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});
+ blzs[0].emplace_back(blizzard{(int)i, h, c});
}
}
}
std::vector<blizzard> at(int t) {
t %= width * height;
- while (blzs.size() < (size_t) t + 1) {
+ while (blzs.size() < (size_t)t + 1) {
auto l = blzs.size() - 1;
std::vector<blizzard> n = blzs[l];
for (size_t i = 0; i < n.size(); i++) {
@@ -69,26 +68,24 @@ struct valley {
}
void print(int t) {
- std::map<blizzard, int> m;
- auto& blz = blzs[t];
- for (auto& b: blz) {
+ std::map<blizzard, int> 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++) {
+ 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 {
+ } else {
if (it->second > 1) {
printf("%d", it->second);
- }
- else {
+ } else {
printf("%c", it->first.c);
}
}
@@ -99,4 +96,4 @@ struct valley {
};
std::pair<int, int> day24(line_view);
-}
+} // namespace aoc2022