-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
84 lines (71 loc) · 2.62 KB
/
main.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
#include "file.h"
#include "utilities.h"
#include <iostream>
#include <vector>
#include <numeric>
namespace aoc2021_day20 {
int solve(std::string_view path, int steps) {
const int N = 1000;
std::vector<std::string> input = file::readFileAsArrayString(path);
std::vector<std::vector<bool>> map(N, std::vector<bool>(N, false));
int middle = N / 2 - input[2].size() / 2;
utils::point start {middle, middle};
int size = input[2].size();
for (int i = 2; i < input.size(); ++i) {
for (int j = 0; j < input[i].size(); ++j) {
map[start.x + i - 2][start.y + j] = (input[i][j] == '#');
}
}
for (int step = 0; step < steps; ++step) {
int newSol = 0;
std::vector<std::vector<bool>> newMap(N, std::vector<bool>(N, false));
utils::point oldStart = start;
int oldSize = size;
start.x -= 1;
start.y -= 1;
size += 2;
for (int x = start.x; x < start.x + size; ++x) {
for (int y = start.y; y < start.y + size; ++y) {
std::bitset<9> mask;
int k = 0;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
if ((x + i >= oldStart.x && x + i < oldStart.x + oldSize &&
y + j >= oldStart.y && y + j < oldStart.y + oldSize) ||
((input[0][0] != '#'))) { // the zero value doesn't change the state
mask[8 - k] = map[x + i][y + j];
}
else {
mask[8 - k] = (step % 2 != 0);
}
k++;
}
}
newMap[x][y] = input[0][mask.to_ulong()] == '#';
if (newMap[x][y]) {
newSol++;
}
}
}
map = newMap;
}
int sol = 0;
for (const auto& line : map) {
sol += std::count(line.begin(), line.end(), true);
}
return sol;
}
int part_1(std::string_view path) {
return solve(path, 2);
}
int part_2(std::string_view path) {
return solve(path, 50);
}
}
#ifndef TESTING
int main() {
std::cout << "Part 1: " << aoc2021_day20::part_1("../2021/day20/input.in") << std::endl;
std::cout << "Part 2: " << aoc2021_day20::part_2("../2021/day20/input.in") << std::endl;
return 0;
}
#endif