2021 day 19 pt 2
This commit is contained in:
parent
685f397493
commit
ba8a32f6e3
1 changed files with 30 additions and 16 deletions
|
@ -2,13 +2,28 @@ import AOC
|
||||||
|
|
||||||
aoc 2021, 19 do
|
aoc 2021, 19 do
|
||||||
def p1 do
|
def p1 do
|
||||||
scanners = input()
|
scans = input()
|
||||||
|
|
||||||
align_scanners(scanners)
|
{aligned, _} = align_scans(scans)
|
||||||
|> MapSet.size()
|
IO.puts("")
|
||||||
|
|
||||||
|
MapSet.size(aligned)
|
||||||
end
|
end
|
||||||
|
|
||||||
def p2 do
|
def p2 do
|
||||||
|
scans = input()
|
||||||
|
|
||||||
|
{_, origins} = align_scans(scans)
|
||||||
|
IO.puts("")
|
||||||
|
|
||||||
|
for a <- origins, b <- origins -- [a] do
|
||||||
|
manhattan_distance(a, b)
|
||||||
|
end
|
||||||
|
|> Enum.max()
|
||||||
|
end
|
||||||
|
|
||||||
|
def manhattan_distance({x0, y0, z0}, {x1, y1, z1}) do
|
||||||
|
abs(x0 - x1) + abs(y0 - y1) + abs(z0 - z1)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attempt_alignment(source, canidate) do
|
def attempt_alignment(source, canidate) do
|
||||||
|
@ -16,10 +31,9 @@ aoc 2021, 19 do
|
||||||
source
|
source
|
||||||
|> potential_transforms(canidate)
|
|> potential_transforms(canidate)
|
||||||
|> Enum.map(fn transform ->
|
|> Enum.map(fn transform ->
|
||||||
apply_transform(canidate, transform)
|
{apply_transform(canidate, transform), transform.({0, 0, 0})}
|
||||||
end)
|
end)
|
||||||
|> Enum.find(:miss, fn transformed ->
|
|> Enum.find(:miss, fn {transformed, _} ->
|
||||||
|
|
||||||
overlap(source, transformed) >= 12
|
overlap(source, transformed) >= 12
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
@ -33,24 +47,24 @@ aoc 2021, 19 do
|
||||||
|
|
||||||
def union(map_sets), do: Enum.reduce(map_sets, MapSet.new(), &MapSet.union/2)
|
def union(map_sets), do: Enum.reduce(map_sets, MapSet.new(), &MapSet.union/2)
|
||||||
|
|
||||||
def align_scanners([start | rest]) do
|
def align_scans([start | rest]) do
|
||||||
align_scanners(rest, start)
|
align_scans(rest, start, [{0, 0, 0}])
|
||||||
end
|
end
|
||||||
|
|
||||||
def align_scanners(scanners, aligned, skipped \\ [])
|
def align_scans(scans, aligned, origins, skipped \\ [])
|
||||||
def align_scanners([], aligned, []), do: aligned
|
def align_scans([], aligned, origins, []), do: {aligned, origins}
|
||||||
def align_scanners([], aligned, skipped) do
|
def align_scans([], aligned, origins, skipped) do
|
||||||
IO.puts("")
|
IO.puts("")
|
||||||
align_scanners([aligned | skipped])
|
align_scans(skipped, aligned, origins, [])
|
||||||
end
|
end
|
||||||
def align_scanners([canidate | rest], aligned, skipped) do
|
def align_scans([canidate | rest], aligned, origins, skipped) do
|
||||||
case aligned |> attempt_alignment(canidate) do
|
case aligned |> attempt_alignment(canidate) do
|
||||||
:miss ->
|
:miss ->
|
||||||
IO.write(".")
|
IO.write(".")
|
||||||
align_scanners(rest, aligned, [canidate | skipped])
|
align_scans(rest, aligned, origins, [canidate | skipped])
|
||||||
{:ok, transformed} ->
|
{:ok, {transformed, origin}} ->
|
||||||
IO.write("+")
|
IO.write("+")
|
||||||
align_scanners(rest, MapSet.union(transformed, aligned), skipped)
|
align_scans(rest, MapSet.union(transformed, aligned), [origin | origins], skipped)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue