defmodule Main do defmodule Prime do defstruct value: 2 def next(prime \\ %__MODULE__{value: 2}) def next(%__MODULE__{value: 2}), do: %__MODULE__{value: 3} def next(%__MODULE__{value: n} = prime) do if is_prime(n + 2), do: %{prime | value: n + 2}, else: next(%{prime | value: n + 2}) end def is_prime(n) def is_prime(n) when n < 2, do: false def is_prime(n) when n in [2, 3], do: true def is_prime(n) when 3 < n do if rem(n, 2) == 0, do: false, else: is_prime(n, 3) end defp is_prime(n, i) when i * i <= n do if rem(n, i) == 0, do: false, else: is_prime(n, i + 2) end defp is_prime(_, _), do: true defimpl Enumerable do def count(_), do: {:error, __MODULE__} def member?(_, _), do: {:error, __MODULE__} def slice(_), do: {:error, __MODULE__} def reduce(_prime, {:halt, acc}, _fun), do: {:halted, acc} def reduce(prime, {:suspend, acc}, fun), do: {:suspended, acc, &reduce(prime, &1, fun)} def reduce(%{value: v} = prime, {:cont, acc}, fun) do reduce(Prime.next(prime), fun.(v, acc), fun) end end end def main do IO.read(:line) |> String.trim() |> String.to_integer() |> solve() |> if do "Win" else "Lose" end |> IO.puts() end def solve(n) do primes = Enum.reduce_while(%Prime{}, [], fn prime, primes when n < prime -> {:halt, Enum.reverse(primes)} prime, primes -> {:cont, [prime | primes]} end) memo = 2..n |> Enum.reduce(%{1 => false}, fn i, memo -> flag = primes |> Enum.reduce_while(false, fn _p, true -> {:halt, true} p, flag when i < p -> {:halt, flag} p, flag when i - p < 2 -> {:halt, flag} p, _flag -> {:cont, not(memo[i - p])} end) Map.put(memo, i, flag) end) memo[n] end end