Perl Weekly Challenge 076

I did this week's Perl Weekly Challenge in both Perl and Prolog. The Prolog solutions were good practice in shaking the rust off my logic programming but I won't discuss them here, to keep things short. The code for the Prolog solutions for Part 1 and Part 2 are on GitHub. 

Part 1

You are given a number $N. Write a script to find the minimum number of prime numbers required, whose summation gives you $N. For the sake of this task, please assume 1 is not a prime number.

For this solution I used a pre-computed list of the first 1000 prime numbers. Larger pre-computed lists are available and of course computing them directly is always an option too! For the purposes of this challenge it seemed a pre-computed list would be OK.

Code (with list of pre-computed primes cropped) (with list of pre-computed primes cropped)

Sample Run

$ perl perl/ 9
7 + 2 = 9

$ perl perl/ 87
83 + 2 + 2 = 87

Part 2

Write a script that takes two file names. The first file would contain word search grid as shown below. The second file contains list of words, one word per line. You could even use local dictionary file.

Print out a list of all words seen on the grid, looking both orthogonally and diagonally, backwards as well as forwards.

I opted to hard code the grid and load a small dictionary file. This file was obtained from and contains 60,000 of the most common english words. Ultimately this yield a lot of awkward looking two and three letter words which, frankly, I do not personally consider all that common. I used the full dictionary but filter the results to only words with 4 or more letters. (with some utility functions cropped) (with some utility functions cropped)

The approach is straightforward:

  • Create arrays of all diagonals, columns, rows, etc of the grid.
  • Search for all dictionary words (forward and reverse) from all the grid arrays.
  • Filter out words with less than 4 letters. Many of these are very uncommon.
  • Sort and display the found words.

Sample Run

$ perl perl/
Found the following words: align,alls,ante,arare,aras,aras,argos,baas,bide,blunt,bosc,broad,buff,butea,cart,cess,cold,cord,demi,depart,departed,doth,dust,ebro,enter,etna,eves,filch,garlic,gila,goat,gram,grieve,grit,hani,hazard,heed,laic,lien,luge,lune,mali,malign,malignant,mall,mein,mero,midst,need,oats,ough,ought,ovary,part,parte,parted,quash,rape,rara,rare,rast,road,roccus,ruse,sara,sara,shed,shrine,slag,slug,social,spasm,spasmodic,succor,succors,theorem,togo,trap,tsar,vary,virus,visa,wigged,zing

Comments for this post were locked by the author