From 407453790f86c7bc227e8b243ab273fd48b85e2f Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Mon, 11 Dec 2023 13:19:14 -0500 Subject: [PATCH] solve 2015 19.1 --- 2015/.envrc | 2 +- 2015/.tool-versions | 4 ++-- 2015/README.md | 3 ++- 2015/lib/2015/19.ex | 34 ++++++++++++++++++---------------- README.md | 2 +- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/2015/.envrc b/2015/.envrc index e24737b..24bb1b6 100644 --- a/2015/.envrc +++ b/2015/.envrc @@ -1 +1 @@ -export ADVENT_OF_CODE_SESSION=53616c7465645f5f92dfbc249005a7e94ec3c6e62cd4e5e9ddf8e368a48c0c80440b6c5e37d836cc0b294e19a9e8a7f1 +export ADVENT_OF_CODE_SESSION=53616c7465645f5f18112545c7b5290961cf3f2c965e90c1b68415326ef1f99790e4dde4ef0cebc7abd120ea1b537aada76420516a0ceb883a722a84b81e7a02 diff --git a/2015/.tool-versions b/2015/.tool-versions index eda65d9..ff52406 100644 --- a/2015/.tool-versions +++ b/2015/.tool-versions @@ -1,2 +1,2 @@ -erlang 24.1.4 -elixir 1.12.3-otp-24 +erlang 26.1.2 +elixir 1.15.7-otp-26 diff --git a/2015/README.md b/2015/README.md index d8143d1..6574c8c 100644 --- a/2015/README.md +++ b/2015/README.md @@ -16,7 +16,7 @@ | :--: | :--: | :-: | :-: | :-: | :-: | :-: | | | | [1] | [2] | [3] | [4] | [5] | | [6] | [7] | [8] | [9] | [10] | [11] | [12] | -| [13] | [14] | [15] | [16]| [17] | [18] | 19 | +| [13] | [14] | [15] | [16]| [17] | [18] | [19] | | 20 | 21 | 22 | 23 | 24 | 25 | | [1]: ./lib/2015/1.ex @@ -37,3 +37,4 @@ [16]: ./lib/2015/16.ex [17]: ./lib/2015/17.ex [18]: ./lib/2015/18.ex +[19]: ./lib/2015/19.ex diff --git a/2015/lib/2015/19.ex b/2015/lib/2015/19.ex index 4651a4c..bb53976 100644 --- a/2015/lib/2015/19.ex +++ b/2015/lib/2015/19.ex @@ -18,25 +18,27 @@ aoc 2015, 19 do {replacement_mappings, starting_molecule} end - def splits(xs) do - for i <- 1..(length(xs) - 1) do - Enum.split(xs, i) - end + def possible_substitutions("", _, _), do: [] + + def possible_substitutions(str, source, replacement) do + <> = str + + this = + if String.starts_with?(str, source), + do: [String.replace(str, source, replacement, global: false)], + else: [] + + this ++ for r <- possible_substitutions(next, source, replacement), do: curr <> r end - def possible_replacements(mappings, str) do - for {source, replacements} <- mappings, - replacement <- replacements, - {l, r} <- splits(String.split(str, source)), - uniq: true do - Enum.join( - [ - Enum.join(l, source), - Enum.join(r, source) - ], - replacement - ) + def possible_replacements(mappings, molecule) do + for {source, replacements} <- mappings, replacement <- replacements do + {source, replacement} end + |> Enum.flat_map(fn {source, replacement} -> + possible_substitutions(molecule, source, replacement) + end) + |> Enum.uniq() end def p1 do diff --git a/README.md b/README.md index 307b285..bbf2052 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ## Years -1. [2015] **36/50** 🌟 +1. [2015] **37/50** 🌟 1. 2016 1. [2017] **18/50** 🌟 1. 2018