Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way waaaaaay faster algorithmโ€ฆ takes seconds instead of minutes/hours #3

Merged
merged 8 commits into from Feb 1, 2022

Conversation

dlenski
Copy link
Owner

@dlenski dlenski commented Jan 15, 2022

I've been thinking about this a lot, and suddenly figured out how to do it while out for a run.

๐Ÿƒ๐Ÿป๐Ÿ’ญ๐Ÿง ๐Ÿ’ก๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป๐ŸŒƒโ€ฆ๐Ÿ˜ฎโฉ๐Ÿ’†๐Ÿปโ€โ™‚๏ธ

I thought we needed O(_N_ร—_M_ยฒ) runtime to figure out the best next guess (O(_N_ยณ) for the first guess). But no!
It's possible to figure out the best next guess in O(N_ร—_M) (O(_N_ยฒ) for the first guess).

The way to do it isโ€ฆ actually amazingly simple and beautiful.

  • See 8128325 for the change to the algorithm.
  • See 154c36f for a detailed explanation in the comment.

@HenkPoley
Copy link
Contributor

Technically calloc() and realloc() should use the sizeof() of a **.

% clang --analyze best_guess.c
best_guess.c:252:18: warning: Result of 'calloc' is converted to a pointer of type 'char *', which is incompatible with sizeof operand type 'char **' [unix.MallocSizeof]
    char **buf = calloc(bufsize, sizeof(*output));
    ~~~~~~~      ^~~~~~          ~~~~~~~~~~~~~~~
best_guess.c:293:19: warning: Result of 'realloc' is converted to a pointer of type 'char *', which is incompatible with sizeof operand type 'char **' [unix.MallocSizeof]
            buf = realloc(buf, bufsize * sizeof(*output));
                  ^~~~~~~                ~~~~~~~~~~~~~~~
2 warnings generated.

dlenski added a commit that referenced this pull request Jan 16, 2022
I was allocating too much memory here, by a factor of sizeof(char
*)/sizeof(char).  Thanks @HenkPoley for pointing this out.
#3 (comment)
@dlenski
Copy link
Owner Author

dlenski commented Jan 16, 2022

Thank you, @HenkPoley, fixed that dumb mistake. ๐Ÿคฆโ€โ™‚๏ธ

dlenski added a commit that referenced this pull request Jan 16, 2022
I was allocating too much memory here, by a factor of sizeof(char
*)/sizeof(char).  Thanks @HenkPoley for pointing this out.
#3 (comment)
Where we're going, we don't need these. ๐Ÿ’๐Ÿปโ€โ™‚๏ธ
Say hello to my blazingly fast new O(N^2) algorithm. ๐Ÿƒ๐Ÿป๐Ÿ’ญ๐Ÿง ๐Ÿ’ก๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป๐ŸŒƒโ€ฆ๐Ÿ˜ฎโฉ๐Ÿ’†๐Ÿปโ€โ™‚๏ธ

Old/tired:

- Took 1/2 hour to churn through the 4,595 5-letter words in
  /usr/share/dict/american-english, on one core of my i7-8665U,
  even after some tricky micro-optimization for how to store
  the guesses.
- Took many hours for the 12,972 5-letter words in the
  CSW2019 (Collins Scrabble Wordlist 2019).

New/wired:

- Takes <1 second to for /u/s/d/american-english
- Takes <3 seconds for CSW2019
I was allocating too much memory here, by a factor of sizeof(char
*)/sizeof(char).  Thanks @HenkPoley for pointing this out.
#3 (comment)
As explained in the comment at top already, there are actually only (3^L -
L) possible "clunique" categories, not (3^L), because having (L-1)
RightPosition clues and 1 WrongPosition clue is logically impossible by the
pigeonhole principle.

We should report this accurately in the statistics.
@dlenski dlenski merged commit 5b54522 into main Feb 1, 2022
dlenski added a commit that referenced this pull request Feb 1, 2022
I was allocating too much memory here, by a factor of sizeof(char
*)/sizeof(char).  Thanks @HenkPoley for pointing this out.
#3 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants