Solution to Advent of Code 2018 Day 3 in Elixir

Solving Day 3 turned out to be a bit more challenging for me as I don’t usually do these kind of exercises, Nevertheless it was fun!

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

defmodule Rect do
defstruct [:id, :left, :top, :width, :height]

alias __MODULE__

def parse(spec) do
[id, dimensions] = String.split(spec, "@", trim: true)
[coords, size] = String.split(dimensions, ":", trim: true)

[left, top] = String.split(coords, ",", trim: true) |> Enum.map(&parse_number/1)

[width, height] = String.split(size, "x", trim: true) |> Enum.map(&parse_number/1)

%Rect{
id: String.trim(id),
left: left,
top: top,
width: width,
height: height
}
end

defp parse_number(str), do: str |> String.trim() |> String.to_integer()

def order_horizontal(r1, r2) do
if r1.left < r2.left do
{r1, r2}
else
{r2, r1}
end
end

def order_vertical(r1, r2) do
if r1.top < r2.top do
{r1, r2}
else
{r2, r1}
end
end

def squares(%Rect{width: w, height: h}) when w <= 0 or h <= 0, do: []

def squares(%Rect{} = r) do
for x <- r.left..(r.left + r.width - 1), y <- r.top..(r.top + r.height - 1), do: {x, y}
end
end

defmodule Overlap do
def area(spec) when is_binary(spec) do
spec
|> String.split("\n", trim: true)
|> Enum.map(&Rect.parse/1)
|> area
end

def area(rects, prev_squares \\ [])

def area([h | tl], prev_squares) do
squares =
tl
|> Enum.map(fn x -> overlap(h, x) |> Rect.squares() end)

area(tl, [squares | prev_squares])
end

def area([], squares),
do:
squares
|> List.flatten()
|> Enum.uniq()
|> Enum.count()

def find_non_overlap(spec) when is_binary(spec) do
rects =
spec
|> String.split("\n", trim: true)
|> Enum.map(&Rect.parse/1)

find_non_overlap(rects, rects)
end

def find_non_overlap([h | tl], all_rects) do
if all_rects
|> Enum.filter(fn x -> x.id != h.id end)
|> Enum.all?(fn x ->
o = overlap(h, x)
o.width <= 0 || o.height <= 0
end) do
h
else
find_non_overlap(tl, all_rects)
end
end

def find_non_overlap([], _), do: raise("Not found")

def overlap(%Rect{} = r1, %Rect{} = r2) do
{l, r} = Rect.order_horizontal(r1, r2)

width = min(l.left + l.width - r.left, r.width)

{t, b} = Rect.order_vertical(r1, r2)

height = min(t.top + t.height - b.top, b.height)

%Rect{
left: r.left,
top: b.top,
width: width,
height: height
}
end
end

defmodule OverlapTest do
use ExUnit.Case

import Overlap

test "greets the world" do
assert area("""
# 1 @ 1,3: 4x4
# 2 @ 3,1: 4x4
# 3 @ 5,5: 2x2
""") == 4

assert area("""
# 1 @ 1,3: 4x4
# 2 @ 3,1: 4x4
# 3 @ 1,3: 4x4
""") == 16

assert File.read!("input.txt") |> area == 0
end

test "overlap between 2 rects" do
assert overlap(
%Rect{id: "# 1", left: 1, top: 3, width: 4, height: 8},
%Rect{id: "# 2", left: 3, top: 1, width: 4, height: 4}
) == %Rect{id: nil, left: 3, top: 3, width: 2, height: 2}

assert overlap(
%Rect{id: "# 1", left: 1, top: 3, width: 4, height: 4},
%Rect{id: "# 3", left: 5, top: 5, width: 2, height: 2}
) == %Rect{height: 2, id: nil, left: 5, top: 5, width: 0}
end

test "find_non_overlap" do
assert find_non_overlap("""
# 1 @ 1,3: 4x4
# 2 @ 3,1: 4x4
# 3 @ 5,5: 2x2
""").id == "# 3"

assert File.read!("input.txt") |> find_non_overlap == 0
end
end

defmodule RectTest do
use ExUnit.Case

test "parse" do
assert Rect.parse("# 1 @ 1,3: 4x3") == %Rect{id: "# 1", left: 1, top: 3, width: 4, height: 3}
end

test "order_horizontal" do
{%{id: "# 1"}, %{id: "# 2"}} =
Rect.order_horizontal(
%Rect{id: "# 1", left: 1, top: 3, width: 4, height: 3},
%Rect{id: "# 2", left: 3, top: 3, width: 4, height: 3}
)

{%{id: "# 4"}, %{id: "# 3"}} =
Rect.order_horizontal(
%Rect{id: "# 3", left: 10, top: 3, width: 4, height: 3},
%Rect{id: "# 4", left: 3, top: 3, width: 4, height: 3}
)
end

test "order_vertical" do
{%{id: "# 1"}, %{id: "# 2"}} =
Rect.order_vertical(
%Rect{id: "# 1", left: 1, top: 1, width: 4, height: 1},
%Rect{id: "# 2", left: 3, top: 3, width: 4, height: 3}
)

{%{id: "# 4"}, %{id: "# 3"}} =
Rect.order_vertical(
%Rect{id: "# 3", left: 10, top: 10, width: 4, height: 10},
%Rect{id: "# 4", left: 3, top: 3, width: 4, height: 3}
)
end

test "squares" do
assert Rect.squares(%Rect{id: "# 1", left: 1, top: 3, width: 2, height: 2}) == [
{1, 3},
{1, 4},
{2, 3},
{2, 4}
]

assert Rect.squares(%Rect{id: "# 1", left: 1, top: 3, width: 0, height: 0}) == []
assert Rect.squares(%Rect{id: "# 1", left: 1, top: 3, width: 0, height: 4}) == []
assert Rect.squares(%Rect{id: "# 1", left: 1, top: 3, width: 4, height: 0}) == []
assert Rect.squares(%Rect{id: "# 1", left: 1, top: 3, width: 4, height: -4}) == []
assert Rect.squares(%Rect{id: "# 1", left: 1, top: 3, width: -4, height: 4}) == []
end
end

I am currently working on LiveForm which makes setting up contact forms on your website a breeze.