|
| 1 | +defmodule PiecingItTogether do |
| 2 | + @doc """ |
| 3 | + TODO: add function description and replace types in @spec |
| 4 | + """ |
| 5 | + |
| 6 | + defmodule JigsawPuzzle do |
| 7 | + @type format() :: :landscape | :portrait | :square |
| 8 | + @type t() :: %__MODULE__{ |
| 9 | + pieces: pos_integer(), |
| 10 | + rows: pos_integer(), |
| 11 | + columns: pos_integer(), |
| 12 | + format: format(), |
| 13 | + aspect_ratio: float(), |
| 14 | + border: pos_integer(), |
| 15 | + inside: pos_integer() |
| 16 | + } |
| 17 | + |
| 18 | + defstruct [:pieces, :rows, :columns, :format, :aspect_ratio, :border, :inside] |
| 19 | + end |
| 20 | + |
| 21 | + @spec jigsaw_data(jigsaw_puzzle :: JigsawPuzzle.t()) :: |
| 22 | + {:ok, JigsawPuzzle.t()} | {:error, String.t()} |
| 23 | + def jigsaw_data(%JigsawPuzzle{} = jigsaw_puzzle) do |
| 24 | + jigsaw_puzzle |
| 25 | + |> Map.from_struct() |
| 26 | + |> Enum.filter(fn {_, value} -> value end) |
| 27 | + |> Enum.sort_by(fn {key, _} -> key end) |
| 28 | + |> complete_jigsaw_data() |
| 29 | + end |
| 30 | + |
| 31 | + defp complete_jigsaw_data(aspect_ratio: aspect_ratio, pieces: pieces) do |
| 32 | + columns = round(:math.sqrt(aspect_ratio * pieces)) |
| 33 | + rows = Integer.floor_div(pieces, columns) |
| 34 | + border = 2 * (rows + columns) - 4 |
| 35 | + inside = pieces - border |
| 36 | + |
| 37 | + {:ok, |
| 38 | + %JigsawPuzzle{ |
| 39 | + pieces: pieces, |
| 40 | + rows: rows, |
| 41 | + columns: columns, |
| 42 | + format: aspect_ratio_to_format(aspect_ratio), |
| 43 | + aspect_ratio: aspect_ratio, |
| 44 | + border: border, |
| 45 | + inside: inside |
| 46 | + }} |
| 47 | + end |
| 48 | + |
| 49 | + defp complete_jigsaw_data(format: :square, rows: rows) do |
| 50 | + columns = rows |
| 51 | + pieces = rows * columns |
| 52 | + border = 2 * (rows + columns) - 4 |
| 53 | + inside = pieces - border |
| 54 | + |
| 55 | + {:ok, |
| 56 | + %JigsawPuzzle{ |
| 57 | + pieces: pieces, |
| 58 | + rows: rows, |
| 59 | + columns: columns, |
| 60 | + format: :square, |
| 61 | + aspect_ratio: 1.0, |
| 62 | + border: border, |
| 63 | + inside: inside |
| 64 | + }} |
| 65 | + end |
| 66 | + |
| 67 | + defp complete_jigsaw_data(format: _, rows: _) do |
| 68 | + {:error, "Insufficient data"} |
| 69 | + end |
| 70 | + |
| 71 | + defp complete_jigsaw_data(aspect_ratio: 1.0, inside: inside) do |
| 72 | + columns = 2 + round(:math.sqrt(inside)) |
| 73 | + rows = columns |
| 74 | + pieces = rows * columns |
| 75 | + border = pieces - inside |
| 76 | + |
| 77 | + {:ok, |
| 78 | + %JigsawPuzzle{ |
| 79 | + pieces: pieces, |
| 80 | + rows: rows, |
| 81 | + columns: columns, |
| 82 | + format: :square, |
| 83 | + aspect_ratio: 1.0, |
| 84 | + border: border, |
| 85 | + inside: inside |
| 86 | + }} |
| 87 | + end |
| 88 | + |
| 89 | + defp complete_jigsaw_data(aspect_ratio: _, inside: _) do |
| 90 | + {:error, "Insufficient data"} |
| 91 | + end |
| 92 | + |
| 93 | + defp complete_jigsaw_data(aspect_ratio: aspect_ratio, rows: rows) do |
| 94 | + columns = round(rows * aspect_ratio) |
| 95 | + pieces = rows * columns |
| 96 | + border = 2 * (rows + columns) - 4 |
| 97 | + inside = pieces - border |
| 98 | + |
| 99 | + {:ok, |
| 100 | + %JigsawPuzzle{ |
| 101 | + pieces: pieces, |
| 102 | + rows: rows, |
| 103 | + columns: columns, |
| 104 | + format: aspect_ratio_to_format(aspect_ratio), |
| 105 | + aspect_ratio: aspect_ratio, |
| 106 | + border: border, |
| 107 | + inside: inside |
| 108 | + }} |
| 109 | + end |
| 110 | + |
| 111 | + defp complete_jigsaw_data(border: border, format: :square, pieces: pieces) do |
| 112 | + complete_jigsaw_data(aspect_ratio: 1.0, inside: pieces - border) |
| 113 | + end |
| 114 | + |
| 115 | + defp complete_jigsaw_data(border: border, format: format, pieces: pieces) do |
| 116 | + center = 1 + border / 4 |
| 117 | + diff = :math.sqrt(center ** 2 - pieces) |
| 118 | + |
| 119 | + {rows, columns} = |
| 120 | + case format do |
| 121 | + :landscape -> {round(center - diff), round(center + diff)} |
| 122 | + :portrait -> {round(center + diff), round(center - diff)} |
| 123 | + end |
| 124 | + |
| 125 | + aspect_ratio = columns / rows |
| 126 | + inside = pieces - border |
| 127 | + |
| 128 | + {:ok, |
| 129 | + %JigsawPuzzle{ |
| 130 | + pieces: pieces, |
| 131 | + rows: rows, |
| 132 | + columns: columns, |
| 133 | + format: format, |
| 134 | + aspect_ratio: aspect_ratio, |
| 135 | + border: border, |
| 136 | + inside: inside |
| 137 | + }} |
| 138 | + end |
| 139 | + |
| 140 | + defp complete_jigsaw_data(columns: columns, format: format, rows: rows) do |
| 141 | + aspect_ratio = columns / rows |
| 142 | + |
| 143 | + if format == aspect_ratio_to_format(aspect_ratio) do |
| 144 | + pieces = rows * columns |
| 145 | + border = 2 * (rows + columns) - 4 |
| 146 | + inside = rows * columns - border |
| 147 | + |
| 148 | + {:ok, |
| 149 | + %JigsawPuzzle{ |
| 150 | + pieces: pieces, |
| 151 | + rows: rows, |
| 152 | + columns: columns, |
| 153 | + format: format, |
| 154 | + aspect_ratio: aspect_ratio, |
| 155 | + border: border, |
| 156 | + inside: inside |
| 157 | + }} |
| 158 | + else |
| 159 | + {:error, "Contradictory data"} |
| 160 | + end |
| 161 | + end |
| 162 | + |
| 163 | + defp complete_jigsaw_data(_) do |
| 164 | + {:error, "Insufficient data"} |
| 165 | + end |
| 166 | + |
| 167 | + defp aspect_ratio_to_format(aspect_ratio) do |
| 168 | + case aspect_ratio do |
| 169 | + aspect_ratio when aspect_ratio < 1 -> :portrait |
| 170 | + aspect_ratio when aspect_ratio > 1 -> :landscape |
| 171 | + _ -> :square |
| 172 | + end |
| 173 | + end |
| 174 | +end |
0 commit comments