-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge36.cpp
236 lines (211 loc) · 6.11 KB
/
challenge36.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// AquaQ Challenge Hub
// Challenge 36: Tetonor Terror
// https://challenges.aquaq.co.uk/challenge/36
#include <array>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <gsl/util>
static bool readFile(const std::string& fileName, std::vector<std::string>& lines)
{
std::ifstream in{fileName};
if (!in) {
std::cerr << "Cannot open file " << fileName << std::endl;
return false;
}
auto closeStream = gsl::finally([&in] { in.close(); });
std::string str;
while (std::getline(in, str)) {
lines.push_back(str);
}
return true;
}
constexpr bool verbose{true};
using Grid = std::array<uint16_t, 16>;
using Pair = std::pair<uint16_t, uint16_t>;
using Pairs = std::vector<Pair>;
using OccMap = std::map<uint16_t, uint8_t>;
std::ostream& operator<<(std::ostream& stream, const Grid& grid)
{
std::string sep{""};
for (const auto n : grid) {
stream << sep << n;
sep = " ";
}
return stream;
}
std::ostream& operator<<(std::ostream& stream, const Pairs& pairs)
{
std::string sep{""};
for (const auto [first, second] : pairs) {
stream << sep << "(" << first << "," << second << ")";
sep = ", ";
}
return stream;
}
Grid toArray(std::string& line)
{
std::for_each(line.begin(), line.end(), [](auto& c) {
if (c == '*') {
c = '0';
}
});
Grid grid;
std::istringstream iss{line};
iss.ignore(2);
for (size_t i = 0; i < grid.size(); ++i) {
iss >> grid[i];
}
return grid;
}
OccMap toOccMap(const Grid& grid)
{
OccMap occ;
for (const auto n : grid) {
if (auto it = occ.find(n); it != occ.end()) {
it->second++;
} else {
occ[n] = 1;
}
}
return occ;
}
bool isSolution(const OccMap& gridOcc, const Grid& input, const Pairs& guess)
{
if (guess.size() != 8) {
return false;
}
Grid guessGrid{};
for (size_t i = 0; i < 8; ++i) {
guessGrid[2 * i] = guess[i].first + guess[i].second;
guessGrid[2 * i + 1] = guess[i].first * guess[i].second;
}
if (toOccMap(guessGrid) != gridOcc) {
return false;
}
for (size_t i = 0; i < 8; ++i) {
guessGrid[2 * i] = guess[i].first;
guessGrid[2 * i + 1] = guess[i].second;
}
return toOccMap(guessGrid) == toOccMap(input);
}
std::set<Grid> insertInput(const Grid& input, uint16_t n)
{
std::set<Grid> inputs{};
size_t imin{0};
size_t imax{16};
for (size_t i = input.size(); i > 0; --i) {
if (input[i - 1] != 0 && input[i - 1] < n) {
imin = i;
break;
}
}
for (size_t i = 0; i < input.size(); ++i) {
if (input[i] > n) {
imax = i;
break;
}
}
for (size_t i = imin; i < imax; ++i) {
auto nextInput{input};
nextInput[i] = n;
inputs.insert(nextInput);
}
return inputs;
}
size_t solve(const OccMap& gridOcc, const Grid& input, const Pairs& potential, const Pairs& guess,
std::set<std::pair<Grid, Pairs> >& visitor, size_t start = 0)
{
if (isSolution(gridOcc, input, guess)) {
if (verbose) {
std::cout << "i:" << input << '\n' << guess << std::endl;
}
size_t sum{};
for (const auto [first, second] : guess) {
sum += std::abs(first - second);
}
return sum;
}
const auto depth = guess.size();
if (depth == 8) {
return 0;
}
visitor.insert({input, guess});
for (size_t i = start; i < potential.size() - 7 + depth; ++i) {
auto nextGuess{guess};
nextGuess.push_back(potential[i]);
std::sort(nextGuess.begin(), nextGuess.end());
const auto nextInputs1 = insertInput(input, potential[i].first);
for (const auto& nextInput1 : nextInputs1) {
const auto nextInputs = insertInput(nextInput1, potential[i].second);
for (const auto& nextInput : nextInputs) {
if (visitor.find({nextInput, nextGuess}) != visitor.end()) {
continue;
}
if (const auto sum = solve(gridOcc, nextInput, potential, nextGuess, visitor, i + 1); sum > 0) {
return sum;
}
}
}
}
return 0;
}
size_t solve(const Grid& grid, const Grid& input, const Pairs& potential)
{
Pairs guess{};
std::set<std::pair<Grid, Pairs> > visitor{};
return solve(toOccMap(grid), input, potential, guess, visitor);
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines{};
if (argc == 2) {
if (!readFile(argv[1], lines)) {
return EXIT_FAILURE;
}
}
// Precalculate the potential factorizations
std::map<uint16_t, Pairs> factorizations{};
for (uint16_t i = 1; i <= 400; ++i) {
Pairs pairs{{{1, i}}};
for (uint16_t j = 2; j <= static_cast<uint16_t>(sqrt(i)); ++j) {
if (i % j == 0) {
pairs.push_back({j, i / j});
}
}
factorizations[i] = pairs;
}
size_t sum{};
for (size_t i = 0; i < lines.size(); i += 3) {
const auto grid = toArray(lines[i]);
const auto input = toArray(lines[i + 1]);
// Determine the potential pairs given the grid and the input
Pairs potential{};
for (const auto n : grid) {
const auto& factors = factorizations[n];
for (const auto& f : factors) {
if (std::find(grid.cbegin(), grid.cend(), f.first + f.second) != grid.end() &&
!insertInput(input, f.first).empty() && !insertInput(input, f.second).empty()) {
potential.push_back(f);
}
}
}
if (verbose) {
std::cout << "g:" << grid << std::endl;
}
if (potential.size() < 8) {
continue;
}
sum += solve(grid, input, potential);
if (verbose) {
std::cout << std::endl;
}
}
std::cout << sum << std::endl;
return EXIT_SUCCESS;
}