1
0
Fork 0

wip 2015 day 19

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent 8d8da8d6a3
commit cc5acfdd12

51
2015/lib/2015/19.ex Normal file
View file

@ -0,0 +1,51 @@
import AOC
aoc 2015, 19 do
def input() do
[replacements_string, starting_molecule] = String.split(input_string(), "\n\n", trim: true)
starting_molecule = String.trim(starting_molecule)
replacement_mappings =
replacements_string
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
[from, to] = String.split(line, " => ", trim: true)
{from, to}
end)
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
{replacement_mappings, starting_molecule}
end
def splits(xs) do
for i <- 1..(length(xs) - 1) do
Enum.split(xs, i)
end
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
)
end
end
def p1 do
{mappings, molecule} = input()
possible_replacements(mappings, molecule)
|> length()
end
def p2 do
end
end