Skip to content

Commit

Permalink
Add surrounding 4 directions
Browse files Browse the repository at this point in the history
  • Loading branch information
code-shoily committed Dec 23, 2023
1 parent 33616c2 commit 0635acc
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/algorithms/grid.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,23 @@ defmodule AdventOfCode.Algorithms.Grid do
def surrounding8({x, y}) do
Enum.map(
[{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}],
fn {dx, dy} -> {x + dx, y + dy} end
&add(&1, {x, y})
)
end

@doc """
Gets the co-ordinates of 4 surrounding neighbours in a 2D grid.
## Example
iex> Grid.surrounding4({0, 0})
[{1, 0}, {-1, 0}, {0, 1}, {0, -1}]
"""
def surrounding4({x, y}) do
Enum.map(
[{1, 0}, {-1, 0}, {0, 1}, {0, -1}],
&add(&1, {x, y})
)
end

Expand Down

0 comments on commit 0635acc

Please sign in to comment.