From e521f241cbb33419f82271e7b4fafa9ec105b0ac Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:53 -0400 Subject: [PATCH] try out nx for day 3 part 1 --- 2021/config/config.exs | 2 ++ 2021/lib/2021/3.ex | 26 ++++++++++++++++++++++++++ 2021/mix.exs | 5 ++++- 2021/mix.lock | 6 ++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/2021/config/config.exs b/2021/config/config.exs index 99094ca..b87fdd0 100644 --- a/2021/config/config.exs +++ b/2021/config/config.exs @@ -1,5 +1,7 @@ import Config +config :nx, :default_defn_options, compiler: EXLA, client: :cuda + config :advent_of_code_utils, session: System.get_env("ADVENT_OF_CODE_SESSION"), auto_compile?: true diff --git a/2021/lib/2021/3.ex b/2021/lib/2021/3.ex index 610abda..7cafcc1 100644 --- a/2021/lib/2021/3.ex +++ b/2021/lib/2021/3.ex @@ -2,6 +2,32 @@ import AOC aoc 2021, 3 do def p1 do + sums = + input_stream() + |> Stream.map(fn w -> + String.split(w, "", trim: true) + |> Enum.map(&if &1 == "0", do: "-1", else: &1) + |> Enum.map(&String.to_integer/1) + end) + |> Enum.to_list() + |> Nx.tensor(type: {:s, 8}) + |> Nx.sum(axes: [0]) + |> Nx.to_flat_list() + + for digit <- sums, reduce: {"", ""} do + {gamma, epsilon} -> + if digit > 0 do + {gamma <> "1", epsilon <> "0"} + else + {gamma <> "0", epsilon <> "1"} + end + end + |> then(fn {gamma, epsilon} -> + String.to_integer(gamma, 2) * String.to_integer(epsilon, 2) + end) + end + + def p1_ do [first] = Stream.take(input_stream(), 1) |> Enum.to_list() bit_length = String.length(first) diff --git a/2021/mix.exs b/2021/mix.exs index cad5b4b..25c1fd7 100644 --- a/2021/mix.exs +++ b/2021/mix.exs @@ -25,7 +25,10 @@ defmodule AdventOfCode.MixProject do # Run "mix help deps" to learn about dependencies. defp deps do [ - {:advent_of_code_utils, "~> 1.0"} + {:advent_of_code_utils, "~> 1.0"}, + {:benchee, "~> 1.0", only: :dev}, + {:exla, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "exla"}, + {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx", override: true} ] end end diff --git a/2021/mix.lock b/2021/mix.lock index 780f685..c21d716 100644 --- a/2021/mix.lock +++ b/2021/mix.lock @@ -1,14 +1,20 @@ %{ "advent_of_code_utils": {:hex, :advent_of_code_utils, "1.0.0", "e9ce8be22988e095991168e9d2dde802afc213aaae64491e9fde84dd877db41c", [:mix], [], "hexpm", "1fa5217ccb95a38ce2312dd7ea6057b0d0002a3eba7490feab704df4b8a94038"}, + "benchee": {:hex, :benchee, "1.0.1", "66b211f9bfd84bd97e6d1beaddf8fc2312aaabe192f776e8931cb0c16f53a521", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}], "hexpm", "3ad58ae787e9c7c94dd7ceda3b587ec2c64604563e049b2a0e8baafae832addb"}, "castore": {:hex, :castore, "0.1.13", "ccf3ab251ffaebc4319f41d788ce59a6ab3f42b6c27e598ad838ffecee0b04f9", [:mix], [], "hexpm", "a14a7eecfec7e20385493dbb92b0d12c5d77ecfd6307de10102d58c94e8c49c0"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, + "deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"}, + "elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"}, + "exla": {:git, "https://github.com/elixir-nx/nx.git", "e23a678bf0ebcbbafe03f1b5ed78623f052ad486", [sparse: "exla"]}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, "finch": {:hex, :finch, "0.9.0", "8b772324aebafcaba763f1dffaa3e7f52f8c4e52485f50f48bbb2f42219a2e87", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a93bfcad9ca50fa3cb2d459f27667d9a87cfbb7fecf9b29b2e78a50bc2ab445d"}, "mint": {:hex, :mint, "1.4.0", "cd7d2451b201fc8e4a8fd86257fb3878d9e3752899eb67b0c5b25b180bde1212", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "10a99e144b815cbf8522dccbc8199d15802440fc7a64d67b6853adb6fa170217"}, "mix_test_interactive": {:hex, :mix_test_interactive, "1.1.0", "e3e048ea8b56637c8e5614044483e11d78f81f70e9afb811c06ad3db69704c32", [:mix], [{:file_system, "~> 0.2", [hex: :file_system, repo: "hexpm", optional: false]}, {:typed_struct, "~> 0.2.1", [hex: :typed_struct, repo: "hexpm", optional: false]}], "hexpm", "2288a2edc735ca4879a7c17b9fd9fd4b8b84a57a775c3228db4655806a0a4799"}, "nimble_options": {:hex, :nimble_options, "0.3.7", "1e52dd7673d36138b1a5dede183b5d86dff175dc46d104a8e98e396b85b04670", [:mix], [], "hexpm", "2086907e6665c6b6579be54ef5001928df5231f355f71ed258f80a55e9f63633"}, "nimble_pool": {:hex, :nimble_pool, "0.2.4", "1db8e9f8a53d967d595e0b32a17030cdb6c0dc4a451b8ac787bf601d3f7704c3", [:mix], [], "hexpm", "367e8071e137b787764e6a9992ccb57b276dc2282535f767a07d881951ebeac6"}, + "nx": {:git, "https://github.com/elixir-nx/nx.git", "e23a678bf0ebcbbafe03f1b5ed78623f052ad486", [sparse: "nx"]}, "progress_bar": {:hex, :progress_bar, "2.0.1", "7b40200112ae533d5adceb80ff75fbe66dc753bca5f6c55c073bfc122d71896d", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm", "2519eb58a2f149a3a094e729378256d8cb6d96a259ec94841bd69fdc71f18f87"}, "telemetry": {:hex, :telemetry, "1.0.0", "0f453a102cdf13d506b7c0ab158324c337c41f1cc7548f0bc0e130bbf0ae9452", [:rebar3], [], "hexpm", "73bc09fa59b4a0284efb4624335583c528e07ec9ae76aca96ea0673850aec57a"}, "typed_struct": {:hex, :typed_struct, "0.2.1", "e1993414c371f09ff25231393b6430bd89d780e2a499ae3b2d2b00852f593d97", [:mix], [], "hexpm", "8f5218c35ec38262f627b2c522542f1eae41f625f92649c0af701a6fab2e11b3"}, + "xla": {:hex, :xla, "0.2.0", "689887888afb22587168d461f0e9ff83d7b06040273ea7082dbf9ff7eca33dcc", [:make, :mix], [{:elixir_make, "~> 0.4", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "a2e7b81413db49a159eabfb12dbd784a7c04b5c68c7b4057238d5ec9b110f2ec"}, }