1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include "aoc.h"
#include <deque>
#include <map>
namespace aoc2016 {
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
|