Skip to content

Commit 0f8a54b

Browse files
committed
(refactor grid api)
1 parent 5355651 commit 0f8a54b

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

lib/aoc/grid.ex

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,33 @@ defmodule AoC.Grid do
6666
{min_x(keys), max_x(keys), min_y(keys), max_y(keys)}
6767
end
6868

69+
@deprecated "use format/2"
6970
def format_map(map, print_char \\ &self_char/1) do
70-
{xl, xh, yl, yh} = bounds(map)
71+
format(map, print_char)
72+
end
73+
74+
def format(grid, print_char \\ &self_char/1) do
75+
{xl, xh, yl, yh} = bounds(grid)
7176

7277
for y <- yl..yh do
7378
[
7479
"\n",
7580
for x <- xl..xh do
76-
print_char.(Map.get(map, {x, y}))
81+
print_char.(Map.get(grid, {x, y}))
7782
end
7883
]
7984
end
8085
end
8186

87+
@deprecated "use print/2"
8288
def print_map(map, print_char \\ &self_char/1) do
83-
IO.puts(format_map(map, print_char))
89+
print(map, print_char)
90+
end
91+
92+
def print(grid, print_char \\ &self_char/1) do
93+
IO.puts(format(grid, print_char))
8494

85-
map
95+
grid
8696
end
8797

8898
defp self_char(nil), do: " "
@@ -143,12 +153,13 @@ defmodule AoC.Grid do
143153
]
144154
end
145155

146-
def translate({x, y}, :n), do: {x, y - 1}
147-
def translate({x, y}, :ne), do: {x + 1, y - 1}
148-
def translate({x, y}, :nw), do: {x - 1, y - 1}
149-
def translate({x, y}, :s), do: {x, y + 1}
150-
def translate({x, y}, :se), do: {x + 1, y + 1}
151-
def translate({x, y}, :sw), do: {x - 1, y + 1}
152-
def translate({x, y}, :w), do: {x - 1, y}
153-
def translate({x, y}, :e), do: {x + 1, y}
156+
def translate(xy, direction, n \\ 1)
157+
def translate({x, y}, :n, n), do: {x, y - n}
158+
def translate({x, y}, :ne, n), do: {x + n, y - n}
159+
def translate({x, y}, :nw, n), do: {x - n, y - n}
160+
def translate({x, y}, :s, n), do: {x, y + n}
161+
def translate({x, y}, :se, n), do: {x + n, y + n}
162+
def translate({x, y}, :sw, n), do: {x - n, y + n}
163+
def translate({x, y}, :w, n), do: {x - n, y}
164+
def translate({x, y}, :e, n), do: {x + n, y}
154165
end

test/rect_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ defmodule AoC.RectTest do
1616
ExUnit.CaptureIO.capture_io(fn ->
1717
rectangles
1818
|> to_grid(f)
19-
|> Grid.print_map(fn
19+
|> Grid.print(fn
2020
nil -> "⋅"
2121
char when is_binary(char) -> char
2222
n when is_integer(n) -> Integer.to_string(n)

0 commit comments

Comments
 (0)