File size: 821 Bytes
fa0b996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule Alice.Hasher do
  @moduledoc """
  Alice's implementation of the integer hashing challenge.
  Uses built-in crypto functions for SHA-256.
  """

  @doc """
  Computes SHA-256 hash of a string.
  """
  def sha256(input) do
    input
    |> :crypto.hash(:sha256)
    |> Base.encode16(case: :lower)
  end

  @doc """
  Sums a list of integers.
  """
  def sum_list(list) do
    Enum.reduce(list, 0, &(&1 + &2))
  end

  @doc """
  Computes factorial recursively.
  """
  def factorial(0), do: 1
  def factorial(n), do: n * factorial(n - 1)

  @doc """
  Main entry point that routes to the appropriate function.
  """
  def solve(:sha256, input), do: sha256(input)
  def solve(:sum_list, input), do: sum_list(input)
  def solve(:factorial, input), do: factorial(input)
  def solve(_challenge, _input), do: nil
end