From b0b8a08e0ecc96b89f93606ed008ece8a44defae Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:54 -0400 Subject: [PATCH] wip 2021 22 pt 2 --- 2021/lib/2021/22.ex | 33 +++++++++++++++++++++++++++++---- 2021/mix.exs | 1 + 2021/mix.lock | 2 ++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/2021/lib/2021/22.ex b/2021/lib/2021/22.ex index 25d7039..1933fd8 100644 --- a/2021/lib/2021/22.ex +++ b/2021/lib/2021/22.ex @@ -1,6 +1,9 @@ import AOC aoc 2021, 22 do + alias :rstar, as: RStar + alias :rstar_geometry, as: RStarGeometry + def p1 do input_cuboids() |> Enum.filter(&within_range(-50..50, &1)) @@ -11,23 +14,45 @@ aoc 2021, 22 do |> MapSet.size() end - def p2 do - end - def make_set({xs, ys, zs}) do for x <- xs, y <- ys, z <- zs, into: MapSet.new(), do: {x, y, z} end def within_range(bounds, {tag, ranges}) when tag in ~w[on off]a, do: within_range(bounds, ranges) def within_range(bounds, {x, y, z}), do: - within_range(bounds, x) and within_range(bounds, y) and within_range(bounds, z) + [x, y, z] |> Enum.all?(&within_range(bounds, &1)) def within_range(bs..be, s..e), do: bs <= s and e <= be + def p2 do + input_cuboids() + |> Enum.reduce(RStar.new(3), fn + {:on, {x1..x2, y1..y2, z1..z2}}, tree -> + cuboid = RStarGeometry.new(3, [{x1, x2}, {y1, y2}, {z1, z2}], nil) + + IO.inspect(cuboid) + RStar.search_within(tree, cuboid) + |> Enum.each(fn geo -> + IO.write(" ... ") + IO.inspect(RStarGeometry.intersect(cuboid, geo)) + end) + + IO.gets("") + + RStar.insert(tree, cuboid) + _, tree -> tree + end) + end + + def intersect?({a1, b1, c1}, {a2, b2, c2}), do: + intersect?(a1, a2) or intersect?(b1, b2) or intersect?(c1, c2) + def intersect?(a, b), do: not Range.disjoint?(a, b) + def parse_cuboid_ranges(str) do ~r/x=(-?\d+)..(-?\d+),y=(-?\d+)..(-?\d+),z=(-?\d+)..(-?\d+)/ |> Regex.run(str, capture: :all_but_first) |> Enum.map(&String.to_integer/1) |> Enum.chunk_every(2) + |> Enum.map(&Enum.sort/1) |> Enum.map(&apply(Range, :new, &1)) |> List.to_tuple() end diff --git a/2021/mix.exs b/2021/mix.exs index ed4c36e..10e27d5 100644 --- a/2021/mix.exs +++ b/2021/mix.exs @@ -32,6 +32,7 @@ defmodule AdventOfCode.MixProject do {:nimble_parsec, "~> 1.0"}, {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true}, {:rexbug, ">= 1.0.0"}, + {:rstar, github: "armon/erl-rstar"}, {:timex, "~> 3.7.6"} ] end diff --git a/2021/mix.lock b/2021/mix.lock index 0c28c68..e7923d2 100644 --- a/2021/mix.lock +++ b/2021/mix.lock @@ -24,8 +24,10 @@ "nx": {:git, "https://github.com/elixir-nx/nx.git", "e23a678bf0ebcbbafe03f1b5ed78623f052ad486", [sparse: "nx"]}, "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, "progress_bar": {:hex, :progress_bar, "2.0.1", "7b40200112ae533d5adceb80ff75fbe66dc753bca5f6c55c073bfc122d71896d", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "2519eb58a2f149a3a094e729378256d8cb6d96a259ec94841bd69fdc71f18f87"}, + "proper": {:hex, :proper, "1.4.0", "89a44b8c39d28bb9b4be8e4d715d534905b325470f2e0ec5e004d12484a79434", [:rebar3], [], "hexpm", "18285842185bd33efbda97d134a5cb5a0884384db36119fee0e3cfa488568cbb"}, "redbug": {:hex, :redbug, "1.2.2", "366d8961770ddc7bb5d209fbadddfa7271005487f938c087a0e385a57abfee33", [:rebar3], [], "hexpm", "b5fe7b94e487be559cb0ec1c0e938c9761205d3e91a96bf263bdf1beaebea729"}, "rexbug": {:hex, :rexbug, "1.0.5", "e4fce59d1cb4f574b2d84181507b4782bc4b6afcb64e2cd276003c563ffef766", [:mix], [{:mix_test_watch, ">= 0.5.0", [hex: :mix_test_watch, repo: "hexpm", optional: true]}, {:redbug, "~> 1.2", [hex: :redbug, repo: "hexpm", optional: false]}], "hexpm", "13a3f180a9e490686a774725a07a21caf05735b7c012824d86960e9541aab46a"}, + "rstar": {:git, "https://github.com/armon/erl-rstar.git", "a406b2cce609029bf65b9ccfbe93a0416c0ee0cd", []}, "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, "telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"}, "timex": {:hex, :timex, "3.7.6", "502d2347ec550e77fdf419bc12d15bdccd31266bb7d925b30bf478268098282f", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.0", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "a296327f79cb1ec795b896698c56e662ed7210cc9eb31f0ab365eb3a62e2c589"},