-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.ex
100 lines (83 loc) · 2.47 KB
/
8.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
defmodule AoC8 do
def create_matrix(x,y,char \\ ".") when is_integer(x) and is_integer(y) and is_binary(char) do
row = List.duplicate(char, x)
rows = List.duplicate(row, y)
rows
end
def print_matrix([hd|_] = list) when is_list(list) and is_list(hd) do
list
|> Enum.each(&print_matrix(&1))
end
def print_matrix([hd|_] = list) when is_list(list) and is_binary(hd) do
list
|> Enum.each(&IO.write(&1))
IO.write("\n")
end
def rect(list, a, b) when is_list(list) and a > 0 and b > 0 do
_rect(list, a, b, 0, [])
end
def rect(list, a, b) when is_list(list) do
list
end
def _rect([hd|tl], a, b, current_row, new_list) when current_row < b do
new_row = (List.duplicate("#", a) ++ Enum.drop(hd, a))
|> Enum.take(length(hd))
_rect(tl, a, b, current_row + 1, [new_row|new_list])
end
def _rect([hd|tl] , a, b, current_row, new_list) do
_rect(tl, a, b, current_row + 1, [hd|new_list])
end
def _rect([] , a, b, current_row, new_list) do
new_list
|> Enum.reverse
end
def rotate_row_right(list, y, b) when is_list(list) and y >= 0 and y < length(list)do
old_row = Enum.at(list, y)
b = rem(b, length(old_row))
new_row = Enum.take(old_row, -b) ++ Enum.drop(old_row, -b)
List.replace_at(list, y, new_row)
end
def rotate_row_right(list, y, b) do
list
end
def rotate_column_down(list, x, b) do
list
|> List.zip
|> Enum.map(&Tuple.to_list/1)
|> rotate_row_right(x, b)
|> List.zip
|> Enum.map(&Tuple.to_list/1)
end
def parse_command("rect " <> <<rest::binary>>) do
[a, b] = rest
|> String.trim
|> String.split("x")
|> Enum.map(&String.to_integer/1)
fun = fn list -> rect(list, a, b) end
end
def parse_command("rotate row y=" <> <<rest::binary>>) do
[a, b] = rest
|> String.trim
|> String.split(" by ")
|> Enum.map(&String.to_integer/1)
fun = fn list -> rotate_row_right(list, a, b) end
end
def parse_command("rotate column x=" <> <<rest::binary>>) do
[a, b] = rest
|> String.trim
|> String.split(" by ")
|> Enum.map(&String.to_integer/1)
fun = fn list -> rotate_column_down(list, a, b) end
end
def run1 do
init_matrix = create_matrix(50,6, " ")
matrix = File.stream!("input8.txt")
|> Stream.map(&parse_command/1)
|> Enum.reduce(init_matrix, fn (fun, acc) -> fun.(acc) end)
print_matrix(matrix)
matrix
|> List.flatten
|> Enum.filter(fn x -> x == "#" end)
|> Enum.count
end
end