This commit is contained in:
Ben Harris 2018-12-03 08:50:31 -05:00
parent 7d9b68597b
commit 1fb359f483
2 changed files with 1415 additions and 0 deletions

40
day3.exs Normal file
View File

@ -0,0 +1,40 @@
defmodule Day3 do
def input() do
File.stream!("input/day3.in")
|> Stream.map(fn x ->
Regex.scan(~r/\d+/, x)
|> Enum.map(&String.to_integer(hd(&1)))
end)
end
def plots(), do: Enum.reduce(input(), %{}, &stake_claim/2)
def part1() do
plots()
|> Enum.count(fn {_k, {v, _}} -> v > 1 end)
end
def part2() do
hd(Enum.find(input(), &intact?(&1, plots())))
end
def stake_claim([id, x, y, w, h], claims) do
coords = for wide <- 0..(w - 1), high <- 0..(h - 1), do: {wide, high}
Enum.reduce(coords, claims, fn {wide, high}, acc ->
Map.update(acc, {x + wide, y + high}, {1, [id]}, fn {count, claims} ->
{count + 1, [id | claims]}
end)
end)
end
def intact?([id, _x, _y, _w, _h], plot) do
Enum.all?(plot, fn {_coord, {_count, claims}} ->
not Enum.member?(claims, id) || claims == [id]
end)
end
end
IO.puts(Day3.part1())
IO.puts(Day3.part2())

1375
input/day3.in Normal file

File diff suppressed because it is too large Load Diff