From e944bd3cbb994f330ba9f3004720e10de42b28a7 Mon Sep 17 00:00:00 2001 From: daviddoji Date: Mon, 6 Sep 2021 20:25:53 +0200 Subject: [PATCH] Module 17: Write Words to the Grid --- a.c | 80 ++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/a.c b/a.c index b6b0e26..82977d5 100644 --- a/a.c +++ b/a.c @@ -8,6 +8,8 @@ using namespace std; // For compiling C++ code // g++ a.c -o a +// with optimizations +// g++ a.c -O2 -o a string ToUpper(string s) { string s2; @@ -139,14 +141,13 @@ public: while (f.is_open() && !f.eof()) { // check for the file! string line; getline(f, line); - // cout << line << "\n"; if (!line.empty()) { line = ToUpper(line); int len = line.length(); if (line[len - 1] == '\r') { line = line.substr(0, len - 1); } - if (len <= max_size) { + if (line.length() <= max_size) { Word* w = new Word(line); words_.push_back(w); // Word would be allocated on the heap CreatePatternHash(w); @@ -202,6 +203,10 @@ struct Grid { assert(in_bounds(p)); return lines[p.row][p.col]; } + void write_box(const Point& p, char c) { + assert(in_bounds(p)); + lines[p.row][p.col] = c; + } // Returns true if point p is a '.' "block" in the grid // 'p' must be in bounds bool is_block(const Point& p) const { @@ -234,6 +239,14 @@ struct Grid { } return temp; } + void WriteString(const Span& s, const string& t) { + int len = s.len; + assert(t.length() == len); + for (int i=0; iPrint(); - Loop(); + grid.Print(); + Loop(grid, 0); } private: - void Loop() { + void Loop(Grid grid, int depth) { // full copy of the grid to allow recursion + depth++; + // if (depth > 3) { + // cout << "Aborting loop because depth=" << depth << "\n"; + // return; + // } Slots empty_slots; // these are the ones we want to work on Slots partial_slots; Slots full_slots; - for (const Span& s : grid_->spans) { + for (const Span& s : grid.spans) { Attr attr; - string temp = grid_->GetString(s, attr); + string temp = grid.GetString(s, attr); if (attr.is_empty()) { empty_slots.push_back(Slot(s, temp)); } else if (attr.is_partial()) { @@ -385,31 +403,43 @@ class Solver { int num_empty = empty_slots.size(); int num_partial = partial_slots.size(); int num_full = full_slots.size(); - cout << "empty = " << num_empty << "\n"; - cout << "partial = " << num_partial << "\n"; - cout << "full = " << num_full << "\n"; + // cout << "empty = " << num_empty << "\n"; + // cout << "partial = " << num_partial << "\n"; + // cout << "full = " << num_full << "\n"; + + // need to check that all words so far are valid! + for (const Slot& s : full_slots) { + // cout << "CHECKING " << s.pattern << " if it is a word\n"; + if (!lib.IsWord(s.pattern)) { + // cout << " --> NOT! ABORT\n"; + return; + } + } if (num_partial == 0 && num_empty == 0) { cout << "SOLUTION!!\n"; - // FIX what do we do here? + grid.Print(); + return; } assert(num_partial > 0); - CommitSlot(partial_slots[0]); + CommitSlot(grid, partial_slots[0], depth); } - void CommitSlot(const Slot& slot) { - cout << "COMMIT slot " << slot << "\n"; - cout << "Possible word choices for this slot are:\n"; + void CommitSlot(Grid& grid, const Slot& slot, int depth) { + // cout << "COMMIT slot " << slot << "\n"; + // cout << "Possible word choices for this slot are:\n"; const Words* words = lib.FindWord(slot.pattern); if (words) { for (const Word* w : *words) { - cout << " " << w->word; - } - cout << "\n"; - } else { - cout << "NO MATCHES to pattern\n"; + // cout << " Committing '" << w->word << "'!\n"; + grid.WriteString(slot.span, w->word); + // cout << "New grid is:\n"; + // grid.Print(); + Loop(grid, depth); // Recursion } + } else { + // cout << "NO MATCHES to pattern\n"; + } } - Grid* grid_; }; // -------------------------------------------------------------------------------- @@ -422,6 +452,6 @@ int main() { lib.ReadFromFile("top_12000.txt", grid.max_size()); - Solver solver(&grid); // address pointer to the grid - solver.Solve(); + Solver solver; + solver.Solve(grid); } \ No newline at end of file