aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/2022/day22/aoc.cpp27
-rw-r--r--src/2022/day22/aoc.h42
-rw-r--r--test/test_2022.cpp2
3 files changed, 56 insertions, 15 deletions
diff --git a/src/2022/day22/aoc.cpp b/src/2022/day22/aoc.cpp
index 6133492..ba3ff0d 100644
--- a/src/2022/day22/aoc.cpp
+++ b/src/2022/day22/aoc.cpp
@@ -3,6 +3,7 @@
namespace aoc2022 {
std::vector<monkey_map> maps;
+std::vector<trace_map> tmaps;
std::vector<route> routes;
struct person {
@@ -36,12 +37,18 @@ int find_next(int x, int y, const std::vector<monkey_map>& ms, yf f) {
}
}
+void mark(int x, int y, char c) {
+ auto& r = tmaps[y];
+ *(r.p + x) = c;
+}
+
void go_up(int s, person* p, const std::vector<monkey_map>& ms) {
if (s > 0) {
int ax = absolute_x(p->x, ms[p->y]);
int y = find_next(p->x, p->y, ms, prev_y);
const monkey_map& r = ms[y];
if (*(r.line.line + ax - r.start) == '.') {
+ mark(ax, p->y, '^');
p->y = y;
p->x = ax - r.start;
go_up(s - 1, p, ms);
@@ -55,6 +62,7 @@ void go_down(int s, person* p, const std::vector<monkey_map>& ms) {
int y = find_next(p->x, p->y, ms, next_y);
const monkey_map& r = ms[y];
if (*(r.line.line + ax - r.start) == '.') {
+ mark(ax, p->y, 'v');
p->y = y;
p->x = ax - r.start;
go_down(s - 1, p, ms);
@@ -68,6 +76,7 @@ void go_left(int s, person* p, const std::vector<monkey_map>& ms) {
int x = p->x - 1 >= 0 ? p->x - 1 : m.line.length - 1;
char c = *(m.line.line + x);
if (c == '.') {
+ mark(m.start + p->x, p->y, '<');
p->x = x;
go_left(s - 1, p, ms);
}
@@ -80,6 +89,7 @@ void go_right(int s, person* p, const std::vector<monkey_map>& ms) {
int x = p->x + 1 < (int) m.line.length ? p->x + 1 : 0;
char c = *(m.line.line + x);
if (c == '.') {
+ mark(m.start + p->x, p->y, '>');
p->x = x;
go_right(s - 1, p, ms);
}
@@ -87,9 +97,12 @@ void go_right(int s, person* p, const std::vector<monkey_map>& ms) {
}
void travel(person* p, const std::vector<monkey_map>& ms, const route& r) {
+ char fs[] = {'>', 'v', '<', '^'};
if (r.is_step == false) {
+ printf("%c", fs[r.ins.f]);
p->f = r.ins.f;
} else {
+ printf("%d\n", r.ins.steps);
switch (p->f) {
case up:
go_up(r.ins.steps, p, ms);
@@ -122,9 +135,9 @@ std::pair<int64_t, int64_t> day22(line_view file) {
return true;
});
- // for(auto&m : maps) {
- // m.print();
- // }
+ for(auto&m : maps) {
+ tmaps.emplace_back(m);
+ }
// facing f = right;
// for(auto& r: routes) {
@@ -135,9 +148,13 @@ std::pair<int64_t, int64_t> day22(line_view file) {
for (auto& r : routes) {
travel(&p, maps, r);
}
+
+ for(auto&tm : tmaps) {
+ tm.print();
+ }
- char fs[] = {'>', 'v', '<', '^'};
- printf("person is at x[%d] y[%d] facing[%c]\n", p.x, p.y, fs[p.f]);
+ // char fs[] = {'>', 'v', '<', '^'};
+ // printf("person is at x[%d] y[%d] facing[%c]\n", p.x + 1, p.y + 1, fs[p.f]);
int64_t n1 = (p.y + 1) * 1000 + 4 * (p.x + 1) + (int) p.f;
return {n1, 0};
diff --git a/src/2022/day22/aoc.h b/src/2022/day22/aoc.h
index 5d8d70e..0c7ea22 100644
--- a/src/2022/day22/aoc.h
+++ b/src/2022/day22/aoc.h
@@ -31,6 +31,30 @@ struct monkey_map {
}
};
+struct trace_map {
+ char *p = nullptr;
+
+ trace_map(const monkey_map& m) {
+ p = (char*) malloc(m.start + m.line.length + 1);
+ for (int i = 0; i < m.start; i++) {
+ *(p + i) = ' ';
+ }
+ for (size_t i = 0; i < m.line.length; i++) {
+ *(p + m.start + i) = *(m.line.line + i);
+ }
+ *(p + m.start + m.line.length) = '\n';
+ }
+
+ void print() const noexcept {
+ char *px = p;
+ while (true) {
+ printf("%c", *px);
+ if (*px == '\n') break;
+ px++;
+ }
+ }
+};
+
// Facing is 0 for right (>), 1 for down (v), 2 for left (<), and 3 for up (^)
enum facing {
right,
@@ -53,9 +77,9 @@ struct route {
else {
switch (*f) {
case right: printf("%c", ins.f == up ? 'L' : 'R'); break;
+ case down: printf("%c", ins.f == left ? 'R' : 'L'); break;
case left: printf("%c", ins.f == up ? 'R' : 'L'); break;
case up: printf("%c", ins.f == left ? 'L' : 'R'); break;
- case down: printf("%c", ins.f == left ? 'R' : 'L'); break;
}
*f = ins.f;
}
@@ -78,6 +102,10 @@ struct route {
*ff = up;
ins.f = up;
break;
+ case down:
+ *ff = right;
+ ins.f = right;
+ break;
case left:
*ff = down;
ins.f = down;
@@ -86,10 +114,6 @@ struct route {
*ff = left;
ins.f = left;
break;
- case down:
- *ff = right;
- ins.f = right;
- break;
}
} break;
case 'R': {
@@ -98,6 +122,10 @@ struct route {
*ff = down;
ins.f = down;
break;
+ case down:
+ *ff = left;
+ ins.f = left;
+ break;
case left:
*ff = up;
ins.f = up;
@@ -106,10 +134,6 @@ struct route {
*ff = right;
ins.f = right;
break;
- case down:
- *ff = left;
- ins.f = left;
- break;
}
} break;
default:
diff --git a/test/test_2022.cpp b/test/test_2022.cpp
index e2872d9..dc5a9a7 100644
--- a/test/test_2022.cpp
+++ b/test/test_2022.cpp
@@ -177,7 +177,7 @@ TEST_CASE("Monkey Math", "[2022]") {
}
TEST_CASE("Monkey Map", "[2022]") {
- line_view lv = load_file("../src/2022/day22/input");
+ line_view lv = load_file("../src/2022/day22/input0");
auto p = aoc2022::day22(lv);
REQUIRE(0 == p.first);
REQUIRE(0 == p.second);