-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
134 lines (121 loc) · 4.12 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include "boost/spirit/home/x3.hpp"
#include "utils.h"
enum class Position {
Floor,
Empty,
Occupied,
};
struct Position_ : boost::spirit::x3::symbols<Position> {
Position_() { add(".", Position::Floor)("L", Position::Empty)("#", Position::Occupied); }
};
std::vector<std::vector<Position>> parseFile()
{
using namespace boost::spirit::x3;
const auto buffer = AOC::readFile("input.txt");
auto position_ = Position_();
auto row_ = +position_;
std::vector<std::vector<Position>> values;
auto result = parse(buffer.begin(), buffer.end(), row_ % eol, values);
assert(result);
std::cout << "Found " << values.size() << " rows" << std::endl;
return values;
}
auto ruleSet1(const std::vector<std::vector<Position>> &data, size_t n)
{
auto current = data;
const auto countAdjacents = [](auto &input, int i, int j, int n) {
int total = 0;
const auto l = 0, r = (n - 1);
const auto key = Position::Occupied;
total += (i > l && input[i - 1][j] == key);
total += (j > l && input[i][j - 1] == key);
total += (i < r && input[i + 1][j] == key);
total += (j < r && input[i][j + 1] == key);
total += (i > l && j > l && input[i - 1][j - 1] == key);
total += (i > l && j < r && input[i - 1][j + 1] == key);
total += (i < r && j > l && input[i + 1][j - 1] == key);
total += (i < r && j < r && input[i + 1][j + 1] == key);
return total;
};
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
switch (data[i][j]) {
case Position::Empty: {
if (countAdjacents(data, i, j, n) == 0) current[i][j] = Position::Occupied;
break;
}
case Position::Occupied: {
if (countAdjacents(data, i, j, n) >= 4) current[i][j] = Position::Empty;
break;
}
case Position::Floor:
break;
}
}
}
return current;
}
auto ruleSet2(const std::vector<std::vector<Position>> &data, size_t n)
{
auto current = data;
const auto countAdjacents = [](const auto &input, int i, int j, int size) {
int total = 0;
const int l = 0;
const int r = (size - 1);
const std::vector<std::pair<int, int>> axes = {
std::pair { 1, 0 },
std::pair { 0, 1 },
std::pair { -1, 0 },
std::pair { 0, -1 },
std::pair { 1, 1 },
std::pair { 1, -1 },
std::pair { -1, 1 },
std::pair { -1, -1 },
};
for (auto [moff, noff] : axes) {
for (int m = i + moff, n = j + noff; ((m >= l) && (m <= r)) && ((n >= l) && (n <= r)); m += moff, n += noff) {
if (input[m][n] != Position::Floor) {
total += input[m][n] == Position::Occupied;
break;
}
}
}
return total;
};
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
switch (data[i][j]) {
case Position::Empty: {
if (countAdjacents(data, i, j, n) == 0) current[i][j] = Position::Occupied;
break;
}
case Position::Occupied: {
if (countAdjacents(data, i, j, n) >= 5) current[i][j] = Position::Empty;
break;
}
case Position::Floor:
break;
}
}
}
return current;
}
template <typename RuleSet> int f12(const std::vector<std::vector<Position>> &data, size_t n, const RuleSet &ruleSet)
{
auto current = data;
auto next = ruleSet(data, n);
while (next != current) { current = std::exchange(next, ruleSet(next, n)); }
auto totalOccupied = 0;
for (const auto &row : current) {
for (const auto &item : row) { totalOccupied += item == Position::Occupied; }
}
return totalOccupied;
}
int main()
{
const auto data = parseFile();
const auto n = data.size();
std::cout << f12(data, n, ruleSet1) << std::endl;
std::cout << f12(data, n, ruleSet2) << std::endl;
}