This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve20.cc
96 lines (80 loc) · 1.78 KB
/
solve20.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <cctype>
#include <cassert>
#include <vector>
#include <string>
using namespace std;
unsigned char the_map[512];
unsigned W = 0, H = 0, phase = 0;
vector<vector<unsigned char> > grid[2];
bool bgFill = false; // Grrr!!!
void rd()
{
string s;
cin >> s;
assert(!cin.eof() && s.size()==512);
for(unsigned i = 0; i < 512; ++i) {
assert(s[i]=='.' || s[i]=='#');
the_map[i] = (s[i]=='#');
}
unsigned n = 0;
for(;;) {
s.clear();
cin >> s;
if (cin.eof()) break;
assert(s.size() > 0 && (W == 0 || W == s.size()));
W = s.size();
grid[0].emplace_back(vector<unsigned char>(W));
for(unsigned i = 0; i < W; ++i) {
assert(s[i]=='.' || s[i]=='#');
grid[0][H][i] = (s[i]=='#') ? 1 : 0;
if (s[i]=='#') ++n;
}
H++;
}
cout << "Read W=" << W << ", H=" << H << ", num=" << n << endl;
}
unsigned enhance()
{
W += 2;
H += 2;
phase = !phase;
grid[phase].resize(H);
for(unsigned i = 0; i < H; ++i) {
grid[phase][i].resize(W, 0);
}
unsigned n = 0;
for(unsigned i = 0; i < H; ++i) {
for(unsigned j = 0; j < W; ++j) {
unsigned ctx = 0;
for(unsigned ii = 0; ii < 3; ++ii) {
for(unsigned jj = 0; jj < 3; ++jj) {
ctx = 2*ctx;
if (i+ii >= 2 && i+ii < H && j+jj >= 2 && j+jj < W) {
ctx += ((grid[1-phase][i+ii-2][j+jj-2]) ? 1 : 0);
}
else if (bgFill) {
ctx++;
}
}
}
assert(ctx >= 0 && ctx < 512);
if (the_map[ctx]) n++;
assert(i < H && j < W);
grid[phase][i][j] = the_map[ctx];
}
}
bgFill = the_map[bgFill ? 511 : 0];
return n;
}
int main()
{
rd();
enhance();
unsigned n = enhance();
cout << "Task 1: " << n << endl;
for(unsigned i = 0; i < 48; ++i)
n = enhance();
cout << "Task 2: " << n << endl;
return 0;
}