From cc5acfdd12c79e78be36fcf1cc5dc316536db484 Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:53 -0400 Subject: [PATCH] wip 2015 day 19 --- 2015/lib/2015/19.ex | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 2015/lib/2015/19.ex diff --git a/2015/lib/2015/19.ex b/2015/lib/2015/19.ex new file mode 100644 index 0000000..4651a4c --- /dev/null +++ b/2015/lib/2015/19.ex @@ -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