Skip to content
Permalink
Browse files

Use printf instead of cout in native solver

  • Loading branch information...
bobbymcr committed May 27, 2019
1 parent 09c7356 commit 8f52b7846a86bf566e8aced994a8a4bd76db758d
@@ -1,57 +1,57 @@
#include "LetterBoxStrSearch.h"
#include "LetterBoxStrWords.h"
#include "Stopwatch.h"
#include <iomanip>
#include <fstream>
#include <sstream>
#include <cstdarg>

using namespace std;
using namespace Words;

void Log(const char* line)
void Log(const char* format, ...)
{
static Stopwatch watch;

cout << "[" << watch.elapsed() << "] " << line << "\r\n";
printf("[%07.3f] ", watch.elapsed());

va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);

printf("\r\n");
}

int main(int argc, const char** argv)
{
if (argc != 3)
{
cout << "Please specify a Letter Boxed puzzle and a word list file.\r\n";
printf("Please specify a Letter Boxed puzzle and a word list file.\r\n");
return 1;
}

cout << setiosflags(ios::fixed) << setprecision(3);
LetterBoxStr box(argv[1]);

Log("Loading trie...");
ifstream file(argv[2]);
if (file.fail())
{
cout << "Could not open file '" << argv[2] << "'.\r\n";
printf("Could not open file '%s'.\r\n", argv[2]);
return 1;
}

StrTrie trie(file);

stringstream ss;
ss << "Loaded " << trie.size() << " words.";
Log(ss.str().c_str());
Log("Loaded %d words.", trie.size());

LetterBoxStrSearch search(trie, box);
LetterBoxStrWords words;

Log("Finding valid words...");
search.run([&words](Str w, Vertices v) { words.insert(w, v); });
ss = stringstream();
ss << "Found " << words.size() << " valid words.";
Log(ss.str().c_str());
ss.clear();
Log("Found %d valid words.", words.size());

Log("Finding solutions...");
words.find([](Str w1, Str w2) { cout << w1 << "-" << w2 << "\r\n"; });
words.find([](Str w1, Str w2) { printf("%s-%s\r\n", w1.str().c_str(), w2.str().c_str()); });

Log("Done.");

@@ -88,13 +88,20 @@ size_t Str::hash_code() const
return data_;
}

ostream& Words::operator<<(ostream& os, const Str& s)
string Str::str() const
{
for (uint8_t i = 0; i < s.length(); ++i)
char chars[13];
memset(chars, 0, sizeof(chars));
for (uint8_t i = 0; i < length(); ++i)
{
char c = 'A' - 1 + s[i];
os << c;
chars[i] = 'A' - 1 + operator[](i);
}

return string(chars);
}

ostream& Words::operator<<(ostream& os, const Str& s)
{
os << s.str();
return os;
}
@@ -30,6 +30,8 @@ namespace Words

size_t hash_code() const;

std::string str() const;

private:
uint64_t data_;

@@ -438,6 +438,7 @@ namespace Words
{
stringstream ss;
ss << s;
Assert::AreEqual(str, s.str().c_str());
Assert::AreEqual(str, ss.str().c_str());
Assert::AreEqual(uint8_t(strnlen_s(str, 12)), s.length());
}

0 comments on commit 8f52b78

Please sign in to comment.
You can’t perform that action at this time.