DEV Community

Discussion on: Daily Challenge #8 - Scrabble Word Calculator

Collapse
 
n8chz profile image
Lorraine Lee

Elixir. Admittedly my Exercism solution with a bunch of String.replace calls to handle the more sophisticated way the problem is stated here.

defmodule Scrabble do
  @spec slw?(String.t()) :: boolean
  def slw?(word) do
    word
    |> String.replace(~r/\(.*/, "")
    |> String.replace("*", "")
    |> String.length
    |> Kernel.==(7)
  end

  @spec score(String.t()) :: non_neg_integer
  def score(word) do
    word
    |> String.replace(~r/(.)\*\*/, "\\1\\1\\1")
    |> String.replace(~r/(.)\*/, "\\1\\1")
    |> String.replace(~r/^(.*)\(t\)$/, "\\1\\1\\1")
    |> String.replace(~r/^(.*)\(d\)$/, "\\1\\1")
    |> String.downcase
    |> String.to_charlist
    |> Enum.reduce(if(slw?(word), do: 50, else: 0), fn c, acc ->
      acc + cond do
        c == ?^ -> 0
        c in 'urtoenails' -> 1
        c in 'dg' -> 2
        c in 'bcmp' -> 3
        c in 'fhvwy' -> 4
        c == ?k -> 5
        c in 'jx' -> 8
        c in 'qz' -> 10
        true -> 0
      end
    end)
  end
end