-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_03_2.cc
77 lines (70 loc) · 1.79 KB
/
puzzle_03_2.cc
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
#include <cmath>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <boost/functional/hash.hpp>
struct Coord {
int x = 0;
int y = 0;
bool operator== (const Coord& o) const {
return x == o.x && y == o.y;
}
};
struct pair_hash {
size_t operator()(const Coord& c) const {
size_t seed = 0;
boost::hash_combine(seed, c.x);
boost::hash_combine(seed, c.y);
return seed;
}
};
using Map = std::unordered_map<Coord, uint64_t, pair_hash>;
Map gMap;
void report(const Coord& c) {
std::cout << "X: " << c.x << ", Y: " << c.y << ", V: " << gMap[c] << "\n";
}
uint64_t sumNeigh(const Coord& c) {
static Coord null{0, 0};
if (c == null) {
return 1;
}
return gMap[{c.x, c.y + 1}]
+ gMap[{c.x, c.y - 1}]
+ gMap[{c.x + 1, c.y + 1}]
+ gMap[{c.x + 1, c.y - 1}]
+ gMap[{c.x - 1, c.y + 1}]
+ gMap[{c.x - 1, c.y - 1}]
+ gMap[{c.x - 1, c.y}]
+ gMap[{c.x + 1, c.y}];
}
void Spiral(int X, int Y, int target) {
int x, y, dx, dy;
x = y = dx = 0;
dy = -1;
int t = std::max(X, Y);
int maxI = t * t;
for (int i = 0; i < maxI; i++) {
if ((-X / 2 <= x) && (x <= X / 2) && (-Y / 2 <= y) && (y <= Y / 2)) {
Coord c{x, y};
auto V = gMap[c] = sumNeigh(c);
if (V > target) {
report(c);
return;
}
}
if ((x == y) || ((x < 0) && (x == -y)) || ((x > 0) && (x == 1 - y))) {
t = dx;
dx = -dy;
dy = t;
}
x += dx;
y += dy;
}
}
int main() {
std::vector<int> inputs{325489};
for (auto n : inputs) {
std::cout << "input: " << n << "; ";
Spiral(800, 800, n);
}
}