-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge31.cpp
159 lines (142 loc) · 3.47 KB
/
challenge31.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
// AquaQ Challenge Hub
// Challenge 31: Brandless Combination Cubes
// https://challenges.aquaq.co.uk/challenge/31
#include <array>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#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;
}
using Face = std::array<std::array<uint8_t, 3>, 3>;
using RotateFunction = std::function<void(void)>;
Face F{{{1, 1, 1}, {1, 1, 1}, {1, 1, 1}}};
Face U{{{2, 2, 2}, {2, 2, 2}, {2, 2, 2}}};
Face L{{{3, 3, 3}, {3, 3, 3}, {3, 3, 3}}};
Face R{{{4, 4, 4}, {4, 4, 4}, {4, 4, 4}}};
Face D{{{5, 5, 5}, {5, 5, 5}, {5, 5, 5}}};
Face B{{{6, 6, 6}, {6, 6, 6}, {6, 6, 6}}};
inline void rotateFace(Face& f)
{
for (size_t i = 0; i < 2; ++i) {
const auto tmp{f[0][i]};
f[0][i] = f[2 - i][0];
f[2 - i][0] = f[2][2 - i];
f[2][2 - i] = f[i][2];
f[i][2] = tmp;
}
}
void rotateF()
{
rotateFace(F);
for (size_t i = 0; i < 3; ++i) {
const auto tmp{D[0][i]};
D[0][i] = R[2 - i][0];
R[2 - i][0] = U[2][2 - i];
U[2][2 - i] = L[i][2];
L[i][2] = tmp;
}
}
void rotateU()
{
rotateFace(U);
for (size_t i = 0; i < 3; ++i) {
const auto tmp{F[0][i]};
F[0][i] = R[0][i];
R[0][i] = B[2][2 - i];
B[2][2 - i] = L[0][i];
L[0][i] = tmp;
}
}
void rotateL()
{
rotateFace(L);
for (size_t i = 0; i < 3; ++i) {
const auto tmp{D[2 - i][0]};
D[2 - i][0] = F[2 - i][0];
F[2 - i][0] = U[2 - i][0];
U[2 - i][0] = B[2 - i][0];
B[2 - i][0] = tmp;
}
}
void rotateR()
{
rotateFace(R);
for (size_t i = 0; i < 3; ++i) {
const auto tmp{D[i][2]};
D[i][2] = B[i][2];
B[i][2] = U[i][2];
U[i][2] = F[i][2];
F[i][2] = tmp;
}
}
void rotateD()
{
rotateFace(D);
for (size_t i = 0; i < 3; ++i) {
const auto tmp{B[0][i]};
B[0][i] = R[2][2 - i];
R[2][2 - i] = F[2][2 - i];
F[2][2 - i] = L[2][2 - i];
L[2][2 - i] = tmp;
}
}
void rotateB()
{
rotateFace(B);
const auto tmp{U[0]};
for (size_t i = 0; i < 3; ++i) {
U[0][i] = R[i][2];
R[i][2] = D[2][2 - i];
D[2][2 - i] = L[2 - i][0];
L[2 - i][0] = tmp[i];
}
}
uint64_t product(const Face& f)
{
uint64_t prod{1};
for (const auto& row : f) {
for (const auto v : row) {
prod *= v;
}
}
return prod;
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines{};
if (argc == 2) {
if (!readFile(argv[1], lines)) {
return EXIT_FAILURE;
}
}
const std::map<char, RotateFunction> rotateFunc{
{{'F', rotateF}, {'U', rotateU}, {'L', rotateL}, {'R', rotateR}, {'D', rotateD}, {'B', rotateB}}};
RotateFunction rot = []() {};
for (const auto r : lines[0]) {
if ('\'' == r) {
// Lazy implementation: Treat anticlockwise rotation as triple clockwise rotation
rot();
} else {
rot = rotateFunc.at(r);
}
rot();
}
std::cout << product(F) << std::endl;
return EXIT_SUCCESS;
}