# sergio1990's solution

## to Spiral Matrix in the Elixir Track

Published at Jan 04 2019 · 0 comments
Instructions
Test suite
Solution

Given the size, return a square matrix of numbers in spiral order.

The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples:

###### Spiral matrix of size 3
``````1 2 3
8 9 4
7 6 5
``````
###### Spiral matrix of size 4
`````` 1  2  3 4
12 13 14 5
11 16 15 6
10  9  8 7
``````

## Running tests

Execute the tests with:

``````\$ elixir spiral_matrix_test.exs
``````

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.

For example:

``````# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
``````

Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the test suite.

``````# ExUnit.configure exclude: :pending, trace: true
``````

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

## Source

Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension. https://www.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### spiral_test.exs

``````if !System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("spiral.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(trace: true, exclude: :pending)

defmodule SpiralTest do
use ExUnit.Case

# @tag :pending
test "empty spiral" do
assert Spiral.matrix(0) == []
end

@tag :pending
test "trivial spiral" do
assert Spiral.matrix(1) == [[1]]
end

@tag :pending
test "spiral of side length 2" do
assert Spiral.matrix(2) == [
[1, 2],
[4, 3]
]
end

@tag :pending
test "spiral of side length 3" do
assert Spiral.matrix(3) == [
[1, 2, 3],
[8, 9, 4],
[7, 6, 5]
]
end

@tag :pending
test "spiral of side length 4" do
assert Spiral.matrix(4) == [
[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]
]
end

@tag :pending
test "spiral of size 5" do
assert Spiral.matrix(5) == [
[1, 2, 3, 4, 5],
[16, 17, 18, 19, 6],
[15, 24, 25, 20, 7],
[14, 23, 22, 21, 8],
[13, 12, 11, 10, 9]
]
end
end``````
``````defmodule Spiral do
@doc """
Given the dimension, return a square matrix of numbers in clockwise spiral order.
"""
@spec matrix(dimension :: integer) :: list(list(integer))
def matrix(dimension) do
dimension
|> init_base_numbers_list
|> make_spiral(dimension)
end

defp init_base_numbers_list(0), do: []
defp init_base_numbers_list(1), do: [1]
defp init_base_numbers_list(dimension) do
numbers_count = dimension * dimension
range = Range.new(1, numbers_count)
Enum.map(range, fn i -> i end)
end

defp make_spiral([], _), do: []
defp make_spiral([1], _), do: [[1]]
defp make_spiral(numbers_list, dimension) do
spiral = gen_empty_spiral(dimension)
fill_spiral(spiral, numbers_list, {0, 0}, :right)
end

defp gen_empty_spiral(dimension) do
range = Range.new(1, dimension)
Enum.map(range, fn _ ->
Enum.map(range, fn _ -> 0 end)
end)
end

defp fill_spiral(spiral, [], _, _), do: spiral
defp fill_spiral(spiral, numbers_list, position, direction) do
positions = positions_to_fill(spiral, position, direction)
{numbers_to_fill, left_numbers_list} = Enum.split(numbers_list, length(positions))
new_spiral = fill_numbers(spiral, positions, numbers_to_fill)
next_position = List.last(positions)
fill_spiral(new_spiral, left_numbers_list, next_position, next_direction_after(direction))
end

defp fill_numbers(spiral, positions, numbers) do
Enum.reduce(0..length(positions)-1, spiral, fn i, acc ->
pos = Enum.at(positions, i)
number = Enum.at(numbers, i)
set_cell_value(acc, pos, number)
end)
end

defp positions_to_fill(spiral, {row, col}, :right) do
dim = length(spiral)
Enum.reduce(col..(dim - 1), [], fn new_col, acc ->
if cell_value(spiral, row, new_col) == 0, do: List.insert_at(acc, -1, {row, new_col}), else: acc
end)
end
defp positions_to_fill(spiral, {row, col}, :down) do
dim = length(spiral)
Enum.reduce(row..(dim - 1), [], fn new_row, acc ->
if cell_value(spiral, new_row, col) == 0, do: List.insert_at(acc, -1, {new_row, col}), else: acc
end)
end
defp positions_to_fill(spiral, {row, col}, :left) do
Enum.reduce(col..0, [], fn new_col, acc ->
if cell_value(spiral, row, new_col) == 0, do: List.insert_at(acc, -1, {row, new_col}), else: acc
end)
end
defp positions_to_fill(spiral, {row, col}, :up) do
Enum.reduce(row..0, [], fn new_row, acc ->
if cell_value(spiral, new_row, col) == 0, do: List.insert_at(acc, -1, {new_row, col}), else: acc
end)
end

defp cell_value(matrix, row, col) do
Enum.at(Enum.at(matrix, row), col)
end

defp set_cell_value(matrix, {row, col}, value) do
row_list = Enum.at(matrix, row)
new_row_list = List.replace_at(row_list, col, value)
List.replace_at(matrix, row, new_row_list)
end

defp next_direction_after(:right), do: :down
defp next_direction_after(:down), do: :left
defp next_direction_after(:left), do: :up
defp next_direction_after(:up), do: :right
end``````

## Community comments

Find this solution interesting? Ask the author a question to learn more.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?
• Are there new concepts here that you could read more about to improve your understanding?