1
1
Fork 0
aoc2018/day3.exs

87 lines
2.2 KiB
Elixir
Raw Permalink Normal View History

2018-12-03 08:04:07 +00:00
defmodule DayThree do
def parse_claim(claim) do
pattern = ~r/#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/
2018-12-03 12:23:00 +00:00
[[_ | match]] = Regex.scan(pattern, claim)
[id, x, y, xwidth, ywidth] = Enum.map(match, &String.to_integer(&1))
2018-12-03 08:04:07 +00:00
{id, x, y, xwidth, ywidth}
end
def part_one(input) do
2018-12-03 12:23:00 +00:00
claims =
String.split(input, "\n")
2018-12-03 08:04:07 +00:00
|> Enum.filter(&(&1 != ""))
|> Enum.map(&DayThree.parse_claim/1)
2018-12-03 12:23:00 +00:00
2018-12-03 08:04:07 +00:00
dupes = new_doubles(claims)
2018-12-03 12:23:00 +00:00
area_with =
dupes
2018-12-03 08:04:07 +00:00
|> Enum.map(fn {_, {_, _, dx, dy}} -> dx * dy end)
|> List.foldl(0, &+/2)
2018-12-03 12:23:00 +00:00
2018-12-03 08:04:07 +00:00
area_with
end
def new_doubles(claims) do
Enum.map(claims, fn {id1, x1, y1, dx1, dy1} ->
Enum.map(claims, fn {id2, x2, y2, dx2, dy2} ->
2018-12-03 12:23:00 +00:00
{{:id, id1, id2}, intersect({id1, x1, y1, dx1, dy1}, {id2, x2, y2, dx2, dy2})}
end)
2018-12-03 08:04:07 +00:00
|> Enum.filter(fn {_, square} -> square != nil end)
2018-12-03 12:23:00 +00:00
end)
|> List.flatten()
|> Enum.filter(fn {_, square} -> square != nil end)
2018-12-03 08:04:07 +00:00
end
2018-12-03 12:23:00 +00:00
def intersect({id1, x1, y1, dx1, dy1}, {id2, x2, y2, dx2, dy2}) do
if id1 == id2 do
nil
else
intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2}, true)
end
end
def intersect({x1, y1, dx1, dy1}, {x2, y2, dx2, dy2}, ignore \\ false) do
if {x1, y1, dx1, dy1} == {x2, y2, dx2, dy2} && not ignore do
2018-12-03 08:04:07 +00:00
nil
else
xe1 = x1 + dx1
xe2 = x2 + dx2
ye1 = y1 + dy1
2018-12-03 12:23:00 +00:00
2018-12-03 08:04:07 +00:00
if x2 <= xe1 && y2 <= ye1 && x2 >= x1 && y2 >= y1 do
{y2, x2, xe1 - x2, ye1 - y2}
2018-12-03 12:23:00 +00:00
else
if xe2 >= x1 && y2 <= ye1 && x2 <= x1 && y2 >= y1 do
{x1, y2, xe2 - x1, ye1 - y2}
2018-12-03 08:04:07 +00:00
else
nil
end
end
end
end
def part_two(input) do
2018-12-03 12:23:00 +00:00
claims =
String.split(input, "\n")
2018-12-03 08:04:07 +00:00
|> Enum.filter(&(&1 != ""))
|> Enum.map(&DayThree.parse_claim/1)
2018-12-03 12:23:00 +00:00
dupes =
new_doubles(claims)
|> Enum.dedup()
2018-12-03 08:04:07 +00:00
|> Enum.map(fn {{:id, id1, id2}, _} -> [id1, id2] end)
2018-12-03 12:23:00 +00:00
|> List.flatten()
2018-12-03 08:04:07 +00:00
claim_id = Enum.map(claims, fn {id, _, _, _, _} -> id end)
[id] = Enum.filter(claim_id, &(not Enum.member?(dupes, &1)))
id
end
end
2018-12-03 12:23:00 +00:00
{:ok, content} = File.read("input3.txt")
2018-12-03 08:04:07 +00:00
2018-12-03 12:23:00 +00:00
IO.puts("Part 1 (most likely incorrect): #{DayThree.part_one(content)}")
IO.puts("Part 2: #{DayThree.part_two(content)}")