1
1
Fork 0

Day 3. Part 1 broken

This commit is contained in:
g 2018-12-03 16:04:07 +08:00
parent a08c86ded5
commit 365d30bb63
No known key found for this signature in database
GPG Key ID: 60F3D48C2F3C0415
2 changed files with 1329 additions and 0 deletions

70
day3.exs Normal file
View File

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

1259
input3.txt Normal file

File diff suppressed because it is too large Load Diff