day5 pt 2 optimizations
This commit is contained in:
parent
0c2d682398
commit
5a20c55b4c
4 changed files with 44 additions and 13 deletions
8
bench/day5_bench.exs
Normal file
8
bench/day5_bench.exs
Normal file
|
@ -0,0 +1,8 @@
|
|||
{_, content} = File.read("./input/2023_5.txt")
|
||||
input = content |> String.split("\n") |> Mix.Tasks.Day5.parse()
|
||||
Benchee.run(
|
||||
%{
|
||||
"day5_part2" => fn -> input |> Mix.Tasks.Day5.part2() end
|
||||
}
|
||||
)
|
||||
|
|
@ -110,15 +110,14 @@ defmodule Mix.Tasks.Day5 do
|
|||
transposed = RangeUtil.from_start(transpose_start, intersection |> Range.size())
|
||||
|
||||
{
|
||||
[transposed | transposed_ranges] |> Enum.reject(&(&1 == ..)),
|
||||
[transposed | transposed_ranges],
|
||||
leftover
|
||||
|> Enum.flat_map(fn r -> RangeUtil.difference(r, intersection) end)
|
||||
|> Enum.reject(&(&1 == ..))
|
||||
|> Enum.flat_map(fn r -> RangeUtil.difference(r, intersection) end)
|
||||
}
|
||||
end
|
||||
|
||||
domain = leftover ++ transposed
|
||||
domain |> Stream.flat_map(&get_location(dest, &1, maps))
|
||||
domain |> Stream.reject(&(&1 == ..)) |> Stream.flat_map(&get_location(dest, &1, maps))
|
||||
end
|
||||
|
||||
def part1(state) do
|
||||
|
@ -128,14 +127,34 @@ defmodule Mix.Tasks.Day5 do
|
|||
|> Enum.min()
|
||||
end
|
||||
|
||||
def part2(state) do
|
||||
for range <- state.ranges,
|
||||
reduce: nil do
|
||||
acc ->
|
||||
min(
|
||||
acc,
|
||||
get_location(:seed, range, state.maps) |> Stream.map(fn u -> u.first end) |> Enum.min()
|
||||
)
|
||||
def loop_receive(max_len, acc) do
|
||||
cond do
|
||||
acc |> Enum.count() == max_len ->
|
||||
acc |> Enum.min()
|
||||
true ->
|
||||
receive do
|
||||
n -> loop_receive(max_len, [n | acc])
|
||||
end
|
||||
end
|
||||
end
|
||||
def part2(state) do
|
||||
current = self()
|
||||
|
||||
pids =
|
||||
for range <- state.ranges do
|
||||
spawn_link(fn ->
|
||||
send(
|
||||
current,
|
||||
get_location(:seed, range, state.maps)
|
||||
|> Stream.map(fn u -> u.first end)
|
||||
|> Enum.min()
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
loop_receive(
|
||||
pids |> Enum.count(),
|
||||
[]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
3
mix.exs
3
mix.exs
|
@ -24,7 +24,8 @@ defmodule Aoc.MixProject do
|
|||
# {:dep_from_hexpm, "~> 0.3.0"},
|
||||
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
|
||||
{:mix_test_watch, "~> 1.0", only: [:dev, :test], runtime: false},
|
||||
{:httpoison, "~> 2.0"}
|
||||
{:httpoison, "~> 2.0"},
|
||||
{:benchee, "~> 1.0", only: :dev}
|
||||
]
|
||||
end
|
||||
end
|
||||
|
|
3
mix.lock
3
mix.lock
|
@ -1,5 +1,7 @@
|
|||
%{
|
||||
"benchee": {:hex, :benchee, "1.2.0", "afd2f0caec06ce3a70d9c91c514c0b58114636db9d83c2dc6bfd416656618353", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "ee729e53217898b8fd30aaad3cce61973dab61574ae6f48229fe7ff42d5e4457"},
|
||||
"certifi": {:hex, :certifi, "2.12.0", "2d1cca2ec95f59643862af91f001478c9863c2ac9cb6e2f89780bfd8de987329", [:rebar3], [], "hexpm", "ee68d85df22e554040cdb4be100f33873ac6051387baf6a8f6ce82272340ff1c"},
|
||||
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
|
||||
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
|
||||
"hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"},
|
||||
"httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"},
|
||||
|
@ -9,5 +11,6 @@
|
|||
"mix_test_watch": {:hex, :mix_test_watch, "1.1.1", "eee6fc570d77ad6851c7bc08de420a47fd1e449ef5ccfa6a77ef68b72e7e51ad", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}], "hexpm", "f82262b54dee533467021723892e15c3267349849f1f737526523ecba4e6baae"},
|
||||
"parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"},
|
||||
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
|
||||
"statistex": {:hex, :statistex, "1.0.0", "f3dc93f3c0c6c92e5f291704cf62b99b553253d7969e9a5fa713e5481cd858a5", [:mix], [], "hexpm", "ff9d8bee7035028ab4742ff52fc80a2aa35cece833cf5319009b52f1b5a86c27"},
|
||||
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue