diff options
author | kaiwu <kaiwu2004@gmail.com> | 2023-01-28 15:28:35 +0800 |
---|---|---|
committer | kaiwu <kaiwu2004@gmail.com> | 2023-01-28 15:28:35 +0800 |
commit | 75ad85fc0a60392f2d5671670c6657ffd4bace74 (patch) | |
tree | a9e41322691cb5342a821f840651d862cb4fc949 | |
parent | d80c80bb4827ed81428b317a510f437ea3bf2ace (diff) | |
download | advent-of-code-75ad85fc0a60392f2d5671670c6657ffd4bace74.tar.gz advent-of-code-75ad85fc0a60392f2d5671670c6657ffd4bace74.zip |
2016 day24 part1
-rw-r--r-- | src/2016/day24/aoc.cpp | 76 | ||||
-rw-r--r-- | src/2016/day24/aoc.h | 48 | ||||
-rw-r--r-- | test/test_2016.cpp | 4 |
3 files changed, 124 insertions, 4 deletions
diff --git a/src/2016/day24/aoc.cpp b/src/2016/day24/aoc.cpp index 3a5dc38..6642db2 100644 --- a/src/2016/day24/aoc.cpp +++ b/src/2016/day24/aoc.cpp @@ -1,6 +1,80 @@ #include "aoc.h" +#include <deque> +#include <map> namespace aoc2016 { -std::pair<int64_t, int64_t> day24(line_view) { return {0, 0}; } +void bfs(maze::pos p, maze& m, std::map<maze::pos, int>& ds) { + ds.insert({p, 0}); + std::deque<maze::pos> q; + q.push_back(p); + + while (!q.empty()) { + auto s = q.size(); + while (s-- > 0) { + maze::pos p0 = q.front(); + q.pop_front(); + + maze::pos ns[4] = { + {p0.x - 1, p0.y}, + {p0.x + 1, p0.y}, + {p0.x, p0.y - 1}, + {p0.x, p0.y + 1}, + }; + auto& d = ds[p0]; + for (auto& n : ns) { + if (m.get(n.x, n.y) != '#') { + auto px = ds.insert({n, d + 1}); + if (px.second) { + q.push_back(n); + } + if (!px.second && px.first->second > d + 1) { + px.first->second = d + 1; + q.push_back(n); + } + } + } + } + } + + auto& ns = m.numbers; + for (auto it = ds.begin(); it != ds.end();) { + if (ns.find(it->first) == ns.end()) { + it = ds.erase(it); + } else { + it++; + } + } +} + +std::pair<int64_t, int64_t> day24(line_view file) { + // maze mz{179, 43}; // input + maze mz{11, 5}; // demo + int r{0}; + per_line(file, [&r, &mz](line_view lv) { + mz.load(r++, lv); + return true; + }); + + std::vector<std::map<maze::pos, int>> vds; + vds.resize(5); // demo + for (auto &n: mz.numbers) { + int i = mz.get(n.x, n.y) - '0'; + auto& ds = vds[i]; + bfs(n, mz, ds); + } + + // for (auto& ds : vds) { + // for (auto& kv : ds) { + // printf("(%c: %d) ", mz.get(kv.first.x, kv.first.y), kv.second); + // } + // printf("\n"); + // } + + // mz.print(); + // for (auto& p : mz.numbers) { + // printf("%c: (%d, %d)\n", mz.get(p.x, p.y), p.x, p.y); + // } + return {0, 0}; +} } // namespace aoc2016 diff --git a/src/2016/day24/aoc.h b/src/2016/day24/aoc.h index 302152f..23e3b08 100644 --- a/src/2016/day24/aoc.h +++ b/src/2016/day24/aoc.h @@ -1,7 +1,53 @@ #pragma once #include "common.h" +#include <set> #include <vector> namespace aoc2016 { + +struct maze { + struct pos { + int x; + int y; + + friend bool operator<(pos p1, pos p2) { return p1.x < p2.x ? true : p1.x > p2.x ? false : p1.y < p2.y; } + friend bool operator==(pos p1, pos p2) { return p1.x == p2.x && p1.y == p2.y; } + }; + + char* ps = nullptr; + int width; + int height; + std::set<pos> numbers; + + char& get(int x, int y) { return *(ps + width * y + x); } + + void load(int r, line_view lv) { + const char* p = lv.line; + int x{0}; + while (p < lv.line + lv.length - 1) { + get(x, r) = *p; + if (*p >= '0' && *p <= '9') { + numbers.insert({x, r}); + } + x++; + p++; + } + } + + void print() { + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + printf("%c", get(x, y)); + } + printf("\n"); + } + } + + maze(int mx, int my) : width(mx), height(my) { + ps = (char*)malloc(width * height); + memset(ps, 0, width * height); + } +}; + std::pair<int64_t, int64_t> day24(line_view); -} +} // namespace aoc2016 diff --git a/test/test_2016.cpp b/test/test_2016.cpp index 76bf2e9..040f96c 100644 --- a/test/test_2016.cpp +++ b/test/test_2016.cpp @@ -212,8 +212,8 @@ TEST_CASE("Safe Cracking", "[2016]") { } -TEST_CASE("", "[2016]") { - line_view lv = load_file("../src/2016/day24/input"); +TEST_CASE("Air Duct Spelunking", "[2016]") { + line_view lv = load_file("../src/2016/day24/input0"); auto p = aoc2016::day24(lv); REQUIRE(0 == p.first); REQUIRE(0 == p.second); |