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 /src/2016/day24/aoc.cpp | |
parent | d80c80bb4827ed81428b317a510f437ea3bf2ace (diff) | |
download | advent-of-code-75ad85fc0a60392f2d5671670c6657ffd4bace74.tar.gz advent-of-code-75ad85fc0a60392f2d5671670c6657ffd4bace74.zip |
2016 day24 part1
Diffstat (limited to 'src/2016/day24/aoc.cpp')
-rw-r--r-- | src/2016/day24/aoc.cpp | 76 |
1 files changed, 75 insertions, 1 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 |