Anagram Cracker

Post your working scripts, libraries and tools
User avatar
rommmcek
Posts: 1124
Joined: 15 Aug 2014, 15:18

Anagram Cracker

19 Oct 2020, 22:30

This code uses RegEx. Advantages are:
  • Simple code
  • Fast loading
  • Low memory usage
  • Speed for longer inputs
Hotkeys:
  • Esc - Stop search
  • ^Esc - Exit App
When Input Edit is focused:
  • Up - Decrease anagrams output length by 1
  • Down - Increase anagrams output length by 1 (last is "All")
  • PgUp - Decrease anagrams output length by 5
  • PgDn - Increase anagrams output length by 5 (last is "All")

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

SetBatchLines, -1

Gui -DPIScale
Gui, Font,, Consolas
Gui Add, Edit, x15 w455 vaInput gFirstAnagram
GuiControl, Focus, aInput
Gui Add, Text, x15 ym+38 w380 vaInfo, Loading Words...
Gui, Add, ComboBox, x410 ym+32 w60 r25 vaLen gComboAnagram ; AltSubmit 
Gui Add, Edit, x15 yp+30 w455 r20 vaOutput
Gui show,, Anagram Cracker
FileRead, wl, Words.txt
loop, Parse, wl, `n, `r
    (l:= StrLen(A_LoopField))? (WordsLen_%l%.= A_LoopField "`n", mLen<l? mLen:=l: ""): ""
wl:= l:= ""
GuiControl,, aInfo, Ready!
Return

FirstAnagram:
    SetTimer, Execute, -0
Return

Execute:
    Gui Submit, NoHide
    aEditInput:= 1, aLen:= StrLen(aInput)
    Goto Search

ComboAnagram:
    ;br:= 1
    SetTimer, Execute2, -0
Return

Execute2:
    Gui Submit, NoHide

Search:
s:= A_TickCount, aLengths:= "", frSp:= SubStr("      ", Strlen(StrLen(aInput))), toLett:= toLettLIn:= ""
GuiControl,, aInfo, % "Input Len.: " StrLen(aInput) frSp "Anag. Found: ...       Time: ..."
GuiControl,, aOutPut

Loop Parse, aInput
    InStr(toLettLin, A_LoopField)? "": toLettLin.= A_LoopField
Bin:= "", st:= 0, lett:= {}, p:= 1

loop Parse, toLettLin
    StrReplace(aInput, A_LoopField, A_LoopField, cnt), lett[A_LoopField]:= cnt

if (aEditInput, ml:=aLen>mLen? mLen: aLen) {
    Loop, % (ml, aEditInput:="")
        aLengths .= "|" A_Index, (A_Index=ml? aLengths.= "||All": "")
    GuiControl,, aLen, % aLengths
}   GuiControlGet, ns,, aLen

if (ns="All")
    loop, % mLen
        aInd:= mLen-(A_Index-1), tl.= WordsLen_%aInd% ; assigning to wl (not cleared before) isn't significantly faster
else tl:= WordsLen_%ns%

while (p:= RegExMatch(tl, "i`am)^[" toLettLin "]+$", m, p+StrLen(m))) {
    for i, j in % lett {
        StrReplace(m, i, i, cnt)
        if (cnt>jj:=j)
            Break
    }   cnt>jj? "": (Bin.= m "`n", st++)
    if Br
        Break
}   Br:= ""

GuiControl,, aOutPut, % (Bin, tl:="")
frSp:= SubStr("      ", Strlen(StrLen(aInput))), seSp:= SubStr("         ", StrLen(st))
GuiControl,, aInfo
 , % "Input Len.: " StrLen(aInput) frSp "Anag. Found: " st seSp "Time: " Round((A_TickCount-s)/1000, 2) "s"
Return

#IfWinActive, Anagram Cracker
Up::
    GuiControlGet, cF, Focus
    if cf=Edit1
        ControlSend, Edit2, {Up}, Anagram Cracker
    Else Send {Up}
Return
Down:: 
    GuiControlGet, cF, Focus
    if cf=Edit1
        ControlSend, Edit2, {Down}, Anagram Cracker
    Else Send {Down}
Return
PgUp:: 
    GuiControlGet, cF, Focus
    if cf=Edit1
        ControlSend, Edit2, {Up 5}, Anagram Cracker
    Else Send {PgUp}
Return
PgDn:: 
    GuiControlGet, cF, Focus
    if cf=Edit1
        ControlSend, Edit2, {Down 5}, Anagram Cracker
    Else Send {PgDn}
Return
^Esc::
GuiClose:
    ExitApp
Esc:: br:= 1
Not the fastest for short inputs, however fast enough to be non the less considered as real time output.
The hardest task is to retrieve words having each character only once.
Input word1: abcdefghijklmnopqrstuvwxyz'
Input word2: abcdefghijklmnopqrstuvwxyzéóçíáèêüäîïâåöûñôëàøúÅÖÜ' (depends on your word list)
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: Anagram Cracker

20 Oct 2020, 07:17

Hi @rommmcek
Can you link/upload words.txt?
User avatar
rommmcek
Posts: 1124
Joined: 15 Aug 2014, 15:18

Re: Anagram Cracker

20 Oct 2020, 08:09

Here you go: Direct download of a large wordlist and
many different lists.

P.s.: Your post reminds me on one ambiguous test (&& vs. *). I think I've got to the bottom. I'll post it soon!
Last edited by rommmcek on 21 Oct 2020, 10:29, edited 2 times in total.
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: Anagram Cracker

20 Oct 2020, 09:23

Thanks, Nicely done. :)
Tried "Petal"

Return to “Scripts and Functions”

Who is online

Users browsing this forum: AlexProof, Bing [Bot] and 24 guests