How to resolve the algorithm Word wheel step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Word wheel step by step in the Picat programming language

Table of Contents

Problem Statement

A "word wheel" is a type of word game commonly found on the "puzzle" page of newspapers. You are presented with nine letters arranged in a circle or 3×3 grid. The objective is to find as many words as you can using only the letters contained in the wheel or grid. Each word must contain the letter in the centre of the wheel or grid. Usually there will be a minimum word length of 3 or 4 characters. Each letter may only be used as many times as it appears in the wheel or grid.

Write a program to solve the above "word wheel" puzzle. Specifically:

A "word" is defined to be any string contained in the file located at   http://wiki.puzzlers.org/pub/wordlists/unixdict.txt. If you prefer to use a different dictionary,   please state which one you have used. Word wheel puzzles usually state that there is at least one nine-letter word to be found. Using the above dictionary, find the 3x3 grids with at least one nine-letter solution that generate the largest number of words of three or more letters.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Word wheel step by step in the Picat programming language

Source code in the picat programming language

main =>
  MinLen = 3,
  MaxLen = 9,
  Chars = "ndeokgelw",
  MustContain = 'k',

  WordList = "unixdict.txt",
  Words = read_file_lines(WordList),
  Res = word_wheel(Chars,Words,MustContain,MinLen, MaxLen),
  println(Res),
  println(len=Res.len),
  nl.

word_wheel(Chars,Words,MustContain,MinLen,MaxLen) = Res.reverse => 
  Chars := to_lowercase(Chars),
  D = make_hash(Chars), 
  Res = [],
  foreach(W in Words, W.len >= MinLen, W.len <= MaxLen, membchk(MustContain,W))
    WD = make_hash(W),
    Check = true,    
    foreach(C in keys(WD), break(Check == false))
      if not D.has_key(C) ; WD.get(C,0) > D.get(C,0) then
        Check := false
      end
    end,
    if Check == true then
      Res := [W|Res]
    end
  end.

% Returns a map of the elements and their occurrences
% in the list L.
make_hash(L) = D => 
  D = new_map(),
  foreach(E in L)
    D.put(E,D.get(E,0)+1)
  end.

main =>
  WordList = "unixdict.txt",
  MinLen = 3,
  MaxLen = 9,
  Words = [Word : Word in read_file_lines(WordList), Word.len >= MinLen, Word.len <= MaxLen],
  TargetWords = [Word : Word in Words, Word.len == MaxLen],
  MaxResWord = [],
  MaxResLen = 0,
  foreach(Word in TargetWords)
    foreach(MustContain in Word.remove_dups)
       Res = word_wheel(Word,Words,MustContain,MinLen, MaxLen),
       Len = Res.len,
       if Len >= MaxResLen then
         if Len == MaxResLen then
            MaxResWord := MaxResWord ++ [[word=Word,char=MustContain]]
         else
            MaxResWord := [[word=Word,char=MustContain]],
            MaxResLen := Len
         end
       end
    end
  end,
  println(maxLResen=MaxResLen),  
  println(maxWord=MaxResWord).

  

You may also check:How to resolve the algorithm Zeckendorf number representation step by step in the bc programming language
You may also check:How to resolve the algorithm Determine if a string is numeric step by step in the ERRE programming language
You may also check:How to resolve the algorithm Checkpoint synchronization step by step in the Rust programming language
You may also check:How to resolve the algorithm Price fraction step by step in the BASIC programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the IS-BASIC programming language