-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8.cpp
96 lines (88 loc) · 2.32 KB
/
8.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
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 "precompiled.h"
constexpr int width = 25;
constexpr int height = 6;
using line_t = std::array<int, width>;
using layer_t = std::array<line_t, height>;
using image_t = std::vector<layer_t>;
void part_one(const image_t &image) {
int best_score = 1 << 30, answer = -1;
for (int i = 0; std::size_t(i) != std::size(image); ++i) {
int counts[3] = {0, 0, 0};
for (int y = 0; y != height; ++y) {
for (int x = 0; x != width; ++x) {
const int &pixel = image[i][y][x];
if (pixel < 3) {
++counts[pixel];
}
}
}
if (best_score > counts[0]) {
best_score = counts[0];
answer = counts[1] * counts[2];
}
}
std::cout << "Best score " << best_score << ", answer " << answer
<< std::endl;
}
void part_two(const image_t &image) {
layer_t layer;
std::memset(&layer, '\0', sizeof layer);
for (int i = 0; std::size_t(i) != std::size(image); ++i) {
int j = std::size(image) - 1 - i;
for (int y = 0; y != height; ++y) {
for (int x = 0; x != width; ++x) {
const int &pixel = image[j][y][x];
if (pixel != 2) {
layer[y][x] = pixel;
}
}
}
}
for (int y = 0; y != height; ++y) {
for (int x = 0; x != width; ++x) {
const int &pixel = layer[y][x];
switch (pixel) {
case 0:
std::cout << ' ';
break;
case 1:
std::cout << '#';
break;
default:
std::cout << '!';
break;
}
}
std::cout << "\\\n";
}
}
image_t read(const char *filename) {
std::ifstream in(filename);
std::vector<image_t> images;
std::string s;
if (std::getline(in, s)) {
auto layer_count = std::size(s) / (width * height);
image_t image(layer_count);
int n = 0;
for (std::size_t i = 0; i != layer_count; ++i) {
for (int y = 0; y != height; ++y) {
for (int x = 0; x != width; ++x) {
image[i][y][x] = s[n++] - '0';
}
}
}
return image;
}
throw "stream failed";
}
int CALLBACK _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) {
try {
auto image = read("8.data");
part_one(image);
part_two(image);
return 0;
} catch (const char *e) {
std::cout << e << std::endl;
return 1;
}
}