This commit is contained in:
Shadowfacts 2018-12-03 16:58:13 -05:00
parent 16f5505974
commit ad6c796cd2
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 105 additions and 83 deletions

View File

@ -11,6 +11,7 @@ defmodule Day1 do
cond do cond do
Enum.member?(seen, sum) -> Enum.member?(seen, sum) ->
sum sum
true -> true ->
first_repetition(shifts, next_index, sum, [sum | seen]) first_repetition(shifts, next_index, sum, [sum | seen])
end end
@ -21,6 +22,7 @@ defmodule Day1 do
|> Stream.cycle() |> Stream.cycle()
|> Enum.reduce_while({0, [0]}, fn shift, {sum, seen} -> |> Enum.reduce_while({0, [0]}, fn shift, {sum, seen} ->
sum = sum + shift sum = sum + shift
cond do cond do
Enum.member?(seen, sum) -> {:halt, sum} Enum.member?(seen, sum) -> {:halt, sum}
true -> {:cont, {sum, [sum | seen]}} true -> {:cont, {sum, [sum | seen]}}
@ -33,6 +35,7 @@ defmodule Day1 do
|> Stream.cycle() |> Stream.cycle()
|> Enum.reduce_while({0, MapSet.new([0])}, fn shift, {sum, seen} -> |> Enum.reduce_while({0, MapSet.new([0])}, fn shift, {sum, seen} ->
sum = sum + shift sum = sum + shift
cond do cond do
sum in seen -> {:halt, sum} sum in seen -> {:halt, sum}
true -> {:cont, {sum, MapSet.put(seen, sum)}} true -> {:cont, {sum, MapSet.put(seen, sum)}}

View File

@ -25,11 +25,13 @@ defmodule Day2 do
def diff(a, b) do def diff(a, b) do
a = String.graphemes(a) a = String.graphemes(a)
b = String.graphemes(b) b = String.graphemes(b)
Enum.zip(a, b) Enum.zip(a, b)
|> Enum.reduce({0, ""}, fn {a, b}, {diff_count, common} -> |> Enum.reduce({0, ""}, fn {a, b}, {diff_count, common} ->
cond do cond do
a == b -> a == b ->
{diff_count, common <> a} {diff_count, common <> a}
true -> true ->
{diff_count + 1, common} {diff_count + 1, common}
end end

View File

@ -1,11 +1,12 @@
defmodule Day3 do defmodule Day3 do
defmodule Rect do defmodule Rect do
defstruct [:id, :left, :top, :width, :height] defstruct [:id, :left, :top, :width, :height]
def points(nil), do: [] def points(nil), do: []
def points(rect) do def points(rect) do
xs = rect.left..(rect.left + rect.width - 1) xs = rect.left..(rect.left + rect.width - 1)
Enum.flat_map(xs, fn x -> Enum.flat_map(xs, fn x ->
ys = rect.top..(rect.top + rect.height - 1) ys = rect.top..(rect.top + rect.height - 1)
Enum.map(ys, fn y -> {x, y} end) Enum.map(ys, fn y -> {x, y} end)
@ -17,23 +18,31 @@ defmodule Day3 do
overlap_width = max(0, min(a.left + a.width, b.left + b.width) - overlap_left) overlap_width = max(0, min(a.left + a.width, b.left + b.width) - overlap_left)
overlap_top = max(a.top, b.top) overlap_top = max(a.top, b.top)
overlap_height = max(0, min(a.top + a.height, b.top + b.height) - overlap_top) overlap_height = max(0, min(a.top + a.height, b.top + b.height) - overlap_top)
case overlap_width * overlap_height do case overlap_width * overlap_height do
0 -> 0 ->
nil nil
_ -> _ ->
%Rect{left: overlap_left, top: overlap_top, width: overlap_width, height: overlap_height} %Rect{
left: overlap_left,
top: overlap_top,
width: overlap_width,
height: overlap_height
}
end end
end end
end end
def parse_rect(str) do def parse_rect(str) do
parts = Regex.run(~r/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/, str) parts = Regex.run(~r/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/, str)
%Rect{ %Rect{
id: parts |> Enum.at(1) |> String.to_integer, id: parts |> Enum.at(1) |> String.to_integer(),
left: parts |> Enum.at(2) |> String.to_integer, left: parts |> Enum.at(2) |> String.to_integer(),
top: parts |> Enum.at(3) |> String.to_integer, top: parts |> Enum.at(3) |> String.to_integer(),
width: parts |> Enum.at(4) |> String.to_integer, width: parts |> Enum.at(4) |> String.to_integer(),
height: parts |> Enum.at(5) |> String.to_integer, height: parts |> Enum.at(5) |> String.to_integer()
} }
end end
@ -51,8 +60,8 @@ defmodule Day3 do
end) end)
|> Enum.flat_map(&Rect.points/1) |> Enum.flat_map(&Rect.points/1)
end) end)
|> Enum.uniq |> Enum.uniq()
|> Enum.count |> Enum.count()
end end
def exclude_overlapping(rects) do def exclude_overlapping(rects) do
@ -82,5 +91,4 @@ defmodule Day3 do
|> Enum.map(&parse_rect/1) |> Enum.map(&parse_rect/1)
|> exclude_overlapping() |> exclude_overlapping()
end end
end end

View File

@ -3,7 +3,15 @@ defmodule Day2Test do
doctest Day2 doctest Day2
test "count characters" do test "count characters" do
assert Day2.count_characters("abcdef") == %{"a" => 1, "b" => 1, "c" => 1, "d" => 1, "e" => 1, "f" => 1} assert Day2.count_characters("abcdef") == %{
"a" => 1,
"b" => 1,
"c" => 1,
"d" => 1,
"e" => 1,
"f" => 1
}
assert Day2.count_characters("bababc") == %{"a" => 2, "b" => 3, "c" => 1} assert Day2.count_characters("bababc") == %{"a" => 2, "b" => 3, "c" => 1}
assert Day2.count_characters("abbcde") == %{"a" => 1, "b" => 2, "c" => 1, "d" => 1, "e" => 1} assert Day2.count_characters("abbcde") == %{"a" => 1, "b" => 2, "c" => 1, "d" => 1, "e" => 1}
end end

View File

@ -25,6 +25,7 @@ defmodule Day3Test do
%Rect{id: 2, left: 3, top: 1, width: 4, height: 4}, %Rect{id: 2, left: 3, top: 1, width: 4, height: 4},
%Rect{id: 3, left: 5, top: 5, width: 2, height: 2} %Rect{id: 3, left: 5, top: 5, width: 2, height: 2}
] ]
assert Day3.overlap_area(rects) == 4 assert Day3.overlap_area(rects) == 4
end end
@ -34,9 +35,9 @@ defmodule Day3Test do
%Rect{id: 2, left: 3, top: 1, width: 4, height: 4}, %Rect{id: 2, left: 3, top: 1, width: 4, height: 4},
%Rect{id: 3, left: 5, top: 5, width: 2, height: 2} %Rect{id: 3, left: 5, top: 5, width: 2, height: 2}
] ]
res = Day3.exclude_overlapping(rects) res = Day3.exclude_overlapping(rects)
assert Enum.count(res) == 1 assert Enum.count(res) == 1
assert Enum.at(res, 0).id == 3 assert Enum.at(res, 0).id == 3
end end
end end