-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart 2.cpp
40 lines (37 loc) · 1.22 KB
/
Part 2.cpp
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
#include <iostream>
const int input = 325489;
const int sidesize = 11;
int getNear(int (&tab)[sidesize][sidesize], int sidesize, int x, int y) {
int sum = 0;
for(int i = -1; i < 2; ++i)
for(int j = -1; j < 2; ++j)
sum += tab[x + i][y + j];
return sum;
}
int main(void) {
int tab[sidesize][sidesize] = {{0}};
int depth = 1;
int val = tab[sidesize / 2][sidesize / 2] = 1;
int xpos, ypos;
xpos = ypos = sidesize / 2;
bool found = false;
int plusOne = 1;
int minusOne = -1;
int* const moves[4][2] = {{&ypos, &minusOne}, {&xpos, &minusOne}, {&ypos, &plusOne}, {&xpos, &plusOne}};
while(!found) {
++xpos;
for(int i = 0; i < 4; ++i) {
for(int j = 0; !found && j <= depth; *(moves[i][0]) += *(moves[i][1]), ++j) {
val = tab[xpos][ypos] = getNear(tab, sidesize, xpos, ypos);
if(val > input)
found = true;
}
*(moves[i][0]) -= *(moves[i][1]);
if(i != 3)
*(moves[i + 1][0]) += *(moves[i + 1][1]);
}
depth += 2;
}
std::cout << "The first number written that is larger than input is " << val << std::endl;
return 0;
}