-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge35.cpp
115 lines (105 loc) · 3.55 KB
/
challenge35.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
// AquaQ Challenge Hub
// Challenge 35: Columns
// https://challenges.aquaq.co.uk/challenge/35
#include <cctype>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#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;
}
std::vector<std::string> dict{};
std::map<std::vector<size_t>, std::pair<std::vector<std::string>, size_t> > cache{};
std::vector<size_t> colIdx(const std::string& key)
{
const auto keyLen = key.size();
std::vector<size_t> idxSort(keyLen);
std::iota(idxSort.begin(), idxSort.end(), 0);
std::sort(idxSort.begin(), idxSort.end(), [&key](const auto a, const auto b) { return key[a] < key[b]; });
std::vector<size_t> colIdx(keyLen);
for (size_t i = 0; i < keyLen; ++i) {
colIdx[i] = std::find(idxSort.cbegin(), idxSort.cend(), i) - idxSort.begin();
}
return colIdx;
}
std::vector<std::string> decode(const std::string& line, std::vector<size_t> colIdx)
{
const auto keyLen = colIdx.size();
const auto colLen = line.size() / keyLen;
std::vector<std::string> words{};
std::ostringstream word{};
for (size_t i = 0; i < colLen; ++i) {
for (auto j : colIdx) {
if (const auto c = line[j * colLen + i]; isalpha(c)) {
word << c;
} else {
if (const auto& next = word.str(); !next.empty()) {
words.push_back(next);
}
word.str("");
word.clear();
}
}
}
if (const auto& next = word.str(); !next.empty()) {
words.push_back(next);
}
return words;
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines{};
if (argc >= 2) {
if (!readFile(argv[1], lines)) {
return EXIT_FAILURE;
}
}
dict.reserve(172820);
if (argc >= 3) {
if (!readFile(argv[2], dict)) {
return EXIT_FAILURE;
}
}
for (const auto& key : dict) {
if (key.size() == 1) {
continue;
}
const auto idx = colIdx(key);
if (auto it = cache.find(idx); it != cache.end()) {
it->second.first.push_back(key);
} else {
auto words = decode(lines[0], idx);
// Convert to lower case and count how many are found in the given dictionary
std::for_each(words.begin(), words.end(),
[](auto& word) { std::transform(word.begin(), word.end(), word.begin(), std::tolower); });
const size_t count = std::count_if(words.cbegin(), words.cend(), [&](const auto& word) {
return std::binary_search(dict.cbegin(), dict.cend(), word);
});
// Could also use a relative confidence measure instead of the absolute number
cache[idx] = std::make_pair(std::vector<std::string>{key}, count);
}
}
const auto it = std::max_element(cache.cbegin(), cache.cend(),
[](const auto& a, const auto& b) { return a.second.second < b.second.second; });
const auto& keys = it->second.first;
for (const auto& key : keys) {
std::cout << key << std::endl;
}
return EXIT_SUCCESS;
}