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

View File

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

View File

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

View File

@ -3,7 +3,15 @@ defmodule Day2Test do
doctest Day2
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("abbcde") == %{"a" => 1, "b" => 2, "c" => 1, "d" => 1, "e" => 1}
end

View File

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