Avatar of ryanplusplus

ryanplusplus's solution

to Word Search in the Lua Track

Published at Jul 13 2018 · 4 comments
Instructions
Test suite
Solution

In word search puzzles you get a square of letters and have to find specific words in them.

For example:

jefblpepre
camdcimgtc
oivokprjsm
pbwasqroua
rixilelhrs
wolcqlirpc
screeaumgr
alxhpburyi
jalaycalmp
clojurermt

There are several programming languages hidden in the above square.

Words can be hidden in all kinds of directions: left-to-right, right-to-left, vertical and diagonal.

Given a puzzle and a list of words return the location of the first and last letter of each word.

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

word-search_spec.lua

local WordSearch = require('word-search')

describe('word-search', function()
  local puzzle = {
    'jefblpepre',
    'camdcimgtc',
    'oivokprjsm',
    'pbwasqroua',
    'rixilelhrs',
    'wolcqlirpc',
    'screeaumgr',
    'alxhpburyi',
    'jalaycalmp',
    'clojurermt',
  }

  it('should find horizontal words written left-to-right', function()
    local first, last = WordSearch(puzzle).find('clojure')
    assert.same({ 1, 10 }, first)
    assert.same({ 7, 10 }, last)
  end)

  it('should find horizontal words written right-to-left', function()
    local first, last = WordSearch(puzzle).find('elixir')
    assert.same({ 6, 5 }, first)
    assert.same({ 1, 5 }, last)
  end)

  it('should find vertical words written top-to-bottom', function()
    local first, last = WordSearch(puzzle).find('ecmascript')
    assert.same({ 10, 1 }, first)
    assert.same({ 10, 10 }, last)
  end)

  it('should find vertical words written bottom-to-top', function()
    local first, last = WordSearch(puzzle).find('rust')
    assert.same({ 9, 5 }, first)
    assert.same({ 9, 2 }, last)
  end)

  it('should find diagonal words written top-left-to-bottom-right', function()
    local first, last = WordSearch(puzzle).find('java')
    assert.same({ 1, 1 }, first)
    assert.same({ 4, 4 }, last)
  end)

  it('should find diagonal upper written bottom-right-to-top-left', function()
    local first, last = WordSearch(puzzle).find('lua')
    assert.same({ 8, 9 }, first)
    assert.same({ 6, 7 }, last)
  end)

  it('should find diagonal upper written bottom-left-to-top-right', function()
    local first, last = WordSearch(puzzle).find('lisp')
    assert.same({ 3, 6 }, first)
    assert.same({ 6, 3 }, last)
  end)

  it('should find diagonal upper written top-right-to-bottom-left', function()
    local first, last = WordSearch(puzzle).find('ruby')
    assert.same({ 8, 6 }, first)
    assert.same({ 5, 9 }, last)
  end)

  it('should not find words that are not in the puzzle', function()
    local first, last = WordSearch(puzzle).find('haskell')
    assert.same(nil, first)
    assert.same(nil, last)
  end)

  it('should be able to search differently-sized puzzles', function()
    local puzzle = {
      'qwertyuiopz',
      'luamsicrexe',
      'abcdefghijk'
    }
    local first, last = WordSearch(puzzle).find('exercism')
    assert.same({ 11, 2 }, first)
    assert.same({ 4, 2 }, last)
  end)
end)
local grid_api = {}
grid_api.__index = grid_api

function grid_api:at(x, y)
  if x < 1 or x > self._width then return ' ' end
  if y < 1 or y > self._height then return ' ' end
  return self._rep[y]:sub(x, x)
end

function grid_api:locations()
  return coroutine.wrap(function()
    for x = 1, self._width do
      for y = 1, self._height do
        coroutine.yield(x, y)
      end
    end
  end)
end

local function Grid(s)
  return setmetatable({
    _rep = s,
    _width = #s[1],
    _height = #s
  }, grid_api)
end

local function find(grid, word, dx, dy)
  for x, y in grid:locations() do
    local xx, yy = x, y
    for i = 1, #word do
      if grid:at(xx, yy) ~= word:sub(i, i) then break end
      if i == #word then return { x, y }, { xx, yy } end
      xx, yy = xx + dx, yy + dy
    end
  end
end

local deltas = {
  { 1, 0 },
  { 0, 1 },
  { -1, 0 },
  { 0, -1 },
  { 1, 1 },
  { 1, -1 },
  { -1, 1 },
  { -1, -1}
}

return function(puzzle)
  return {
    find = function(word)
      for _, delta in ipairs(deltas) do
        local first, last = find(Grid(puzzle), word, delta[1], delta[2])
        if first then return first, last end
      end
    end
  }
end

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of JaeHyoLee

nice use of coroutine. (Can't think of using coroutine for this problem) Not sure how this works though. I may need take some time to understand your code.

Avatar of samdup123

whats _rep

Avatar of samdup123

what does grid_api.__index = grid_api do again?

Avatar of ryanplusplus

_rep is the internal representation of the grid.

grid_api.__index = grid_api makes it so that if you set the metatable of something to grid_api then failed lookups on the something will be forwarded to grid_api via the __index metamethod.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?