1
0
Fork 0

solve 2015 19.1

This commit is contained in:
sloane 2023-12-11 13:19:14 -05:00
parent e73bc3ecb8
commit 407453790f
No known key found for this signature in database
5 changed files with 24 additions and 21 deletions

View file

@ -1 +1 @@
export ADVENT_OF_CODE_SESSION=53616c7465645f5f92dfbc249005a7e94ec3c6e62cd4e5e9ddf8e368a48c0c80440b6c5e37d836cc0b294e19a9e8a7f1 export ADVENT_OF_CODE_SESSION=53616c7465645f5f18112545c7b5290961cf3f2c965e90c1b68415326ef1f99790e4dde4ef0cebc7abd120ea1b537aada76420516a0ceb883a722a84b81e7a02

View file

@ -1,2 +1,2 @@
erlang 24.1.4 erlang 26.1.2
elixir 1.12.3-otp-24 elixir 1.15.7-otp-26

View file

@ -16,7 +16,7 @@
| :--: | :--: | :-: | :-: | :-: | :-: | :-: | | :--: | :--: | :-: | :-: | :-: | :-: | :-: |
| | | [1] | [2] | [3] | [4] | [5] | | | | [1] | [2] | [3] | [4] | [5] |
| [6] | [7] | [8] | [9] | [10] | [11] | [12] | | [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 | | | 20 | 21 | 22 | 23 | 24 | 25 | |
[1]: ./lib/2015/1.ex [1]: ./lib/2015/1.ex
@ -37,3 +37,4 @@
[16]: ./lib/2015/16.ex [16]: ./lib/2015/16.ex
[17]: ./lib/2015/17.ex [17]: ./lib/2015/17.ex
[18]: ./lib/2015/18.ex [18]: ./lib/2015/18.ex
[19]: ./lib/2015/19.ex

View file

@ -18,25 +18,27 @@ aoc 2015, 19 do
{replacement_mappings, starting_molecule} {replacement_mappings, starting_molecule}
end end
def splits(xs) do def possible_substitutions("", _, _), do: []
for i <- 1..(length(xs) - 1) do
Enum.split(xs, i) def possible_substitutions(str, source, replacement) do
end <<curr::binary-size(1), next::binary>> = 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 end
def possible_replacements(mappings, str) do def possible_replacements(mappings, molecule) do
for {source, replacements} <- mappings, for {source, replacements} <- mappings, replacement <- replacements do
replacement <- replacements, {source, replacement}
{l, r} <- splits(String.split(str, source)),
uniq: true do
Enum.join(
[
Enum.join(l, source),
Enum.join(r, source)
],
replacement
)
end end
|> Enum.flat_map(fn {source, replacement} ->
possible_substitutions(molecule, source, replacement)
end)
|> Enum.uniq()
end end
def p1 do def p1 do

View file

@ -6,7 +6,7 @@
## Years ## Years
1. [2015] **36/50** 🌟 1. [2015] **37/50** 🌟
1. 2016 1. 2016
1. [2017] **18/50** 🌟 1. [2017] **18/50** 🌟
1. 2018 1. 2018