Skip to content

Commit ca2b5cb

Browse files
committed
Bring back the visual results
1 parent f3b8884 commit ca2b5cb

3 files changed

Lines changed: 43 additions & 47 deletions

File tree

lib/2016/day_08.ex

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,81 +8,66 @@ defmodule AdventOfCode.Y2016.Day08 do
88
alias AdventOfCode.Helpers.{InputReader, Transformers}
99

1010
@on "█"
11-
@off "."
11+
@off " "
1212
@width 50
1313
@height 6
1414

1515
def input, do: InputReader.read_from_file(2016, 8)
1616

1717
def run(input \\ input()) do
18-
input = parse(input)
18+
final_grid =
19+
input
20+
|> parse()
21+
|> Enum.reduce(%{}, &operation/2)
1922

20-
process = Enum.reduce(input, empty_grid(), &operation/2)
21-
22-
{Enum.count(process, fn {_, v} -> v == @on end), display(process)}
23-
end
24-
25-
def to_dims(d) do
26-
String.split(d, "x")
27-
|> Enum.map(&to_i/1)
28-
|> List.to_tuple()
23+
{map_size(final_grid), display(final_grid)}
2924
end
3025

31-
def parse(data \\ input()) do
32-
data
26+
defp parse(input) do
27+
input
3328
|> Transformers.lines()
3429
|> Enum.map(fn line ->
35-
case Transformers.words(line) do
36-
["rect", d] -> {:rect, to_dims(d)}
37-
[_, _, "y=" <> y, _, by] -> {:row, to_i(y), to_i(by)}
38-
[_, _, "x=" <> x, _, by] -> {:column, to_i(x), to_i(by)}
30+
case String.split(line, [" ", "=", "x"], trim: true) do
31+
["rect", w, h] -> {:rect, to_i(w), to_i(h)}
32+
["rotate", "row", "y", y, "by", by] -> {:row, to_i(y), to_i(by)}
33+
["rotate", "column", x, "by", by] -> {:col, to_i(x), to_i(by)}
3934
end
4035
end)
4136
end
4237

43-
defp empty_grid do
44-
for w <- 0..(@width - 1), h <- 0..(@height - 1), into: %{} do
45-
{{w, h}, @off}
46-
end
38+
defp operation({:rect, w, h}, grid) do
39+
for x <- 0..(w - 1), y <- 0..(h - 1), into: grid, do: {{x, y}, @on}
4740
end
4841

49-
defp operation({:rect, {w, h}}, grid) do
50-
for x <- 0..(w - 1), y <- 0..(h - 1), reduce: grid do
51-
acc -> %{acc | {x, y} => @on}
42+
defp operation({:row, y, by}, grid) do
43+
keys = for x <- 0..(@width - 1), do: {x, y}
44+
45+
for x <- 0..(@width - 1), grid[{x, y}] == @on, into: Map.drop(grid, keys) do
46+
{{rem(x + by, @width), y}, @on}
5247
end
5348
end
5449

55-
defp operation({:row, at, by}, grid) do
56-
grid
57-
|> line_by_row(at)
58-
|> Map.new(fn {{x, y}, _} ->
59-
{{rem(x + by, @width), y}, grid[{x, y}]}
60-
end)
61-
|> then(&Map.merge(grid, &1))
62-
end
50+
defp operation({:col, x, by}, grid) do
51+
keys = for y <- 0..(@height - 1), do: {x, y}
6352

64-
defp operation({:column, at, by}, grid) do
65-
grid
66-
|> line_by_column(at)
67-
|> Map.new(fn {{x, y}, _} ->
68-
{{x, rem(y + by, @height)}, grid[{x, y}]}
69-
end)
70-
|> then(&Map.merge(grid, &1))
53+
for y <- 0..(@height - 1), grid[{x, y}] == @on, into: Map.drop(grid, keys) do
54+
{{x, rem(y + by, @height)}, @on}
55+
end
7156
end
7257

7358
defp display(grid) do
74-
for h <- 0..(@height - 1) do
75-
for w <- 0..(@width - 1) do
76-
IO.write(grid[{w, h}])
77-
end
59+
# credo:disable-for-next-line
60+
IO.puts("")
61+
62+
for y <- 0..(@height - 1) do
63+
for(x <- 0..(@width - 1), do: Map.get(grid, {x, y}, @off))
64+
|> Enum.join()
65+
# credo:disable-for-next-line
66+
|> IO.puts()
7867
end
7968

8069
:ok
8170
end
8271

83-
defp line_by(grid, n, xy), do: Map.filter(grid, fn {k, _} -> elem(k, xy) == n end)
84-
defp line_by_column(grid, at), do: line_by(grid, at, 0)
85-
defp line_by_row(grid, at), do: line_by(grid, at, 1)
86-
8772
defp to_i(v), do: String.to_integer(v)
8873
end

lib/2019/day_08.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,18 @@ defmodule AdventOfCode.Y2019.Day08 do
5252
end
5353

5454
defp print_image(data) do
55+
# credo:disable-for-next-line
56+
IO.puts("")
57+
5558
data
5659
|> String.replace("1", @printchar)
5760
|> String.replace("0", " ")
5861
|> String.codepoints()
5962
|> chunkify()
6063
|> List.first()
6164
|> Enum.map_join("\n", &Enum.join/1)
65+
# credo:disable-for-next-line
66+
|> IO.puts()
6267
end
6368

6469
defp chunkify(data) do

lib/2022/day_10.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ defmodule AdventOfCode.Y2022.Day10 do
4646
end
4747

4848
defp draw(cycles) do
49+
# credo:disable-for-next-line
50+
IO.puts("")
51+
4952
for r <- 0..5 do
5053
for c <- 0..39 do
5154
cycle = r * 40 + c + 1
@@ -57,6 +60,9 @@ defmodule AdventOfCode.Y2022.Day10 do
5760
IO.write("▒")
5861
end
5962
end
63+
64+
# credo:disable-for-next-line
65+
IO.puts("")
6066
end
6167

6268
:ok

0 commit comments

Comments
 (0)