Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

Boggle Solver

boggle-solver.livemd

Boggle Solver

Solver Module

defmodule Solver do
  def boggle(board, _wordlist) do
    cellmap = fn row, col ->
      %{row: row, col: col}
    end

    neighbors = fn cell ->
      [
        # TOP
        cellmap.(cell.row - 1, cell.col),
        # TOP RIGHT
        cellmap.(cell.row - 1, cell.col + 1),
        # RIGHT
        cellmap.(cell.row, cell.col + 1),
        # BOTTOM RIGHT
        cellmap.(cell.row + 1, cell.col + 1),
        # BOTTOM
        cellmap.(cell.row + 1, cell.col),
        # BOTTOM LEFT
        cellmap.(cell.row + 1, cell.col - 1),
        # LEFT
        cellmap.(cell.row, cell.col - 1),
        # TOP LEFT
        cellmap.(cell.row - 1, cell.col - 1)
      ]
    end

    in_range? = fn cell ->
      size = length(Tuple.to_list(board))

      cell.row >= 0 and
        cell.col >= 0 and
        cell.row <= size and
        cell.col <= size
    end

    match_char = fn cell, char ->
      if in_range?.(cell) do
        value = elem(elem(board, cell.row), cell.col)

        if value == char do
          cell
        end
      end
    end

    check_neighbors = fn cell, char ->
      Enum.each(neighbors.(cell), fn neighbor ->
        neighbor
        |> match_char.(char)
        |> IO.inspect()
      end)
    end

    check_neighbors.(cellmap.(0, 0), "s")

    # words = Enum.map(wordlist, fn word -> String.to_charlist(word) end)
    # Enum.each(words, fn w -> check_board.(w) end)
  end
end

board = {
  {"i", "s", "u", "o"},
  {"o", "s", "v", "e"},
  {"n", "e", "p", "a"},
  {"n", "t", "s", "u"}
}

words = [
  "is",
  "son",
  "spa",
  "sent"
]

Solver.boggle(board, words)

Setup & Solve

# board1 = {
#   {"i", "s", "u", "o"},
#   {"o", "s", "v", "e"},
#   {"n", "e", "p", "a"},
#   {"n", "t", "s", "u"}
# }

# words1 = [
#   "is", "son", "spa", "sent", "vast", "issue",
#   "so", "sap", "ape", "oven", "nice", "events",
#   "us", "ten", "east", "nose", "save", "sonnet",
#   "up", "vet", "nest", "tens", "pause", "session"
# ]

# board2 = {
#   {"e", "a"},
#   {"s", "t"}
# }

# words2 = [
#   "as", "ate", "set", "eats", "seat",
#   "at", "eat", "sat", "east", "sate"
# ]

# Solver.boggle(board1, words1)
# Boggle.boggle(board2, words2)