-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge05.cpp
74 lines (66 loc) · 1.75 KB
/
challenge05.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
// AquaQ Challenge Hub
// Challenge 5: Snake eyes
// https://challenges.aquaq.co.uk/challenge/5
#include <array>
#include <fstream>
#include <iostream>
#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 size_t front{0};
constexpr size_t left{1};
constexpr size_t top{2};
using Dice = std::array<uint16_t, 3>;
using Dices = std::array<Dice, 2>;
bool update(Dices& dices, char dir)
{
for (auto& dice : dices) {
const auto tmp{dice[front]};
if (dir == 'L') {
dice[front] = 7 - dice[left];
dice[left] = tmp;
} else if (dir == 'R') {
dice[front] = dice[left];
dice[left] = 7 - tmp;
} else if (dir == 'U') {
dice[front] = 7 - dice[top];
dice[top] = tmp;
} else if (dir == 'D') {
dice[front] = dice[top];
dice[top] = 7 - tmp;
}
}
return dices[0][front] == dices[1][front];
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines{};
if (argc == 2) {
if (!readFile(argv[1], lines)) {
return EXIT_FAILURE;
}
}
Dices dices{{{1, 2, 3}, {1, 3, 2}}};
size_t count{0};
for (size_t i = 0; i < lines[0].size(); ++i) {
if (update(dices, lines[0][i])) {
count += i;
}
}
std::cout << count << std::endl;
return EXIT_SUCCESS;
}