From 750d7dc6a859e653fd51c5e434b0794284645d65 Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Fri, 1 Dec 2023 11:05:44 -0500 Subject: [PATCH] solve 2023 1.2 --- 2023/elixir/config/config.exs | 1 - 2023/elixir/lib/2023/1.ex | 102 ++++++++++++++++++++++++++----- 2023/elixir/test/2023/1_test.exs | 9 --- README.md | 2 +- 4 files changed, 88 insertions(+), 26 deletions(-) delete mode 100644 2023/elixir/test/2023/1_test.exs diff --git a/2023/elixir/config/config.exs b/2023/elixir/config/config.exs index 536c8e8..d71e314 100644 --- a/2023/elixir/config/config.exs +++ b/2023/elixir/config/config.exs @@ -3,7 +3,6 @@ import Config config :advent_of_code_utils, auto_compile?: true, time_calls?: true, - gen_tests?: true, session: System.get_env("ADVENT_OF_CODE_SESSION") config :iex, inspect: [charlists: :as_lists] diff --git a/2023/elixir/lib/2023/1.ex b/2023/elixir/lib/2023/1.ex index 9fc0995..f00ca9c 100644 --- a/2023/elixir/lib/2023/1.ex +++ b/2023/elixir/lib/2023/1.ex @@ -2,26 +2,98 @@ import AOC aoc 2023, 1 do def p1(input) do - input - |> String.split("\n") - |> Stream.map(&get_first_and_last_numbers_as_number/1) + lines = String.split(input, "\n") + + firsts = + lines + |> Stream.map(&to_digits/1) + |> Stream.map(&List.first/1) + + lasts = + lines + |> Stream.map(&String.reverse/1) + |> Stream.map(&to_digits/1) + |> Stream.map(&List.first/1) + + Stream.zip_with(firsts, lasts, &digits_to_number/2) |> Enum.sum() end - def p2(_input) do + def p2(input) do + lines = String.split(input, "\n") + + firsts = + lines + |> Stream.map(&to_digits_2/1) + |> Stream.map(&List.first/1) + + lasts = + lines + |> Stream.map(&String.reverse/1) + |> Stream.map(&to_digits_3/1) + |> Stream.map(&List.first/1) + + Stream.zip_with(firsts, lasts, &digits_to_number/2) + |> Enum.sum() end - def get_first_and_last_numbers_as_number(str) do - trimmed = - str - |> String.replace(~r/^[^\d]+/, "") - |> String.reverse() - |> String.replace(~r/^[^\d]+/, "") - |> String.reverse() + def digits_to_number(f, l), do: String.to_integer("#{f}#{l}") - first = String.first(trimmed) - last = String.last(trimmed) + def to_digits(""), do: [] + def to_digits("1" <> rest), do: [1 | to_digits(rest)] + def to_digits("2" <> rest), do: [2 | to_digits(rest)] + def to_digits("3" <> rest), do: [3 | to_digits(rest)] + def to_digits("4" <> rest), do: [4 | to_digits(rest)] + def to_digits("5" <> rest), do: [5 | to_digits(rest)] + def to_digits("6" <> rest), do: [6 | to_digits(rest)] + def to_digits("7" <> rest), do: [7 | to_digits(rest)] + def to_digits("8" <> rest), do: [8 | to_digits(rest)] + def to_digits("9" <> rest), do: [9 | to_digits(rest)] + def to_digits(<<_::binary-size(1), rest::binary>>), do: to_digits(rest) - String.to_integer(first <> last) - end + def to_digits_2(""), do: [] + def to_digits_2("1" <> rest), do: [1 | to_digits_2(rest)] + def to_digits_2("one" <> rest), do: [1 | to_digits_2(rest)] + def to_digits_2("2" <> rest), do: [2 | to_digits_2(rest)] + def to_digits_2("two" <> rest), do: [2 | to_digits_2(rest)] + def to_digits_2("3" <> rest), do: [3 | to_digits_2(rest)] + def to_digits_2("three" <> rest), do: [3 | to_digits_2(rest)] + def to_digits_2("4" <> rest), do: [4 | to_digits_2(rest)] + def to_digits_2("four" <> rest), do: [4 | to_digits_2(rest)] + def to_digits_2("5" <> rest), do: [5 | to_digits_2(rest)] + def to_digits_2("five" <> rest), do: [5 | to_digits_2(rest)] + def to_digits_2("6" <> rest), do: [6 | to_digits_2(rest)] + def to_digits_2("six" <> rest), do: [6 | to_digits_2(rest)] + def to_digits_2("7" <> rest), do: [7 | to_digits_2(rest)] + def to_digits_2("seven" <> rest), do: [7 | to_digits_2(rest)] + def to_digits_2("8" <> rest), do: [8 | to_digits_2(rest)] + def to_digits_2("eight" <> rest), do: [8 | to_digits_2(rest)] + def to_digits_2("9" <> rest), do: [9 | to_digits_2(rest)] + def to_digits_2("nine" <> rest), do: [9 | to_digits_2(rest)] + + def to_digits_2(<<_::binary-size(1), rest::binary>>), + do: to_digits_2(rest) + + def to_digits_3(""), do: [] + def to_digits_3("1" <> rest), do: [1 | to_digits_3(rest)] + def to_digits_3("eno" <> rest), do: [1 | to_digits_3(rest)] + def to_digits_3("2" <> rest), do: [2 | to_digits_3(rest)] + def to_digits_3("owt" <> rest), do: [2 | to_digits_3(rest)] + def to_digits_3("3" <> rest), do: [3 | to_digits_3(rest)] + def to_digits_3("eerht" <> rest), do: [3 | to_digits_3(rest)] + def to_digits_3("4" <> rest), do: [4 | to_digits_3(rest)] + def to_digits_3("ruof" <> rest), do: [4 | to_digits_3(rest)] + def to_digits_3("5" <> rest), do: [5 | to_digits_3(rest)] + def to_digits_3("evif" <> rest), do: [5 | to_digits_3(rest)] + def to_digits_3("6" <> rest), do: [6 | to_digits_3(rest)] + def to_digits_3("xis" <> rest), do: [6 | to_digits_3(rest)] + def to_digits_3("7" <> rest), do: [7 | to_digits_3(rest)] + def to_digits_3("neves" <> rest), do: [7 | to_digits_3(rest)] + def to_digits_3("8" <> rest), do: [8 | to_digits_3(rest)] + def to_digits_3("thgie" <> rest), do: [8 | to_digits_3(rest)] + def to_digits_3("9" <> rest), do: [9 | to_digits_3(rest)] + def to_digits_3("enin" <> rest), do: [9 | to_digits_3(rest)] + + def to_digits_3(<<_::binary-size(1), rest::binary>>), + do: to_digits_3(rest) end diff --git a/2023/elixir/test/2023/1_test.exs b/2023/elixir/test/2023/1_test.exs deleted file mode 100644 index 291e118..0000000 --- a/2023/elixir/test/2023/1_test.exs +++ /dev/null @@ -1,9 +0,0 @@ -import AOC - -aoc_test 2023, 1, async: true do - describe "p1" do - test "works for the example input" do - assert p1(example_string()) == 142 - end - end -end diff --git a/README.md b/README.md index 602d1ec..4a4ab8b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ 1. [2020] **17/50** 🌟 1. [2021] **43/50** 🌟 1. [2022] **14/50** 🌟 -1. [2023] **1/50** 🌟 +1. [2023] **2/50** 🌟 [2015]: ./2015 [2017]: ./2017