1
0
Fork 0

2021 day 14

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent a1ad866646
commit b82ec31fe3
2 changed files with 11 additions and 27 deletions

View file

@ -16,7 +16,7 @@
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | | [1] | [2] | [3] | [4] |
| [5] | [6] | [7] | [8] | [9] | [10] | [11]|
| [12]| [13]| 14 | 15 | 16 | 17 | 18 |
| [12]| [13]| [14]| 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
<!-- links -->
@ -34,3 +34,4 @@
[11]: ./lib/2021/11.ex
[12]: ./lib/2021/12.ex
[13]: ./lib/2021/13.ex
[14]: ./lib/2021/14.ex

View file

@ -20,15 +20,6 @@ aoc 2021, 14 do
{sequence, insertion_mappings}
end
def perform_insertions(<<a::binary-size(1), b::binary-size(1), rest::binary>>, mappings) do
case Map.fetch(mappings, a <> b) do
{:ok, c} -> a <> c <> perform_insertions(b <> rest, mappings)
_ -> a <> perform_insertions(b <> rest, mappings)
end
end
def perform_insertions(base, _), do: base
def perform_insertions_lossy(sequence, mappings) when is_binary(sequence),
do:
sequence
@ -37,10 +28,10 @@ aoc 2021, 14 do
def perform_insertions_lossy({letter_freqs, pair_freqs}, mappings) do
[pair_ops, letter_ops] =
for {from, to} <- mappings, Map.has_key?(pair_freqs, from) do
for {from, to} <- mappings,
<<front::binary-size(1), back::binary-size(1)>> = from,
Map.has_key?(pair_freqs, from) do
existing_pairs = Map.get(pair_freqs, from, 0)
# Additional letters
<<front::binary-size(1), back::binary-size(1)>> = from
{[{from, -existing_pairs}, {front <> to, existing_pairs}, {to <> back, existing_pairs}],
[{to, existing_pairs}]}
@ -73,27 +64,19 @@ aoc 2021, 14 do
{letter_frequencies, pair_frequencies}
end
def calculate_score_of_iteration(input \\ input(), n) do
{final_sequence, _} =
iterate(n, input, fn {sequence, mappings} ->
next = perform_insertions(sequence, mappings)
{next, mappings}
def calculate_score_of_iteration({starting_seq, mappings} \\ input(), n) do
{final_letter_freqs, _} =
iterate(n, starting_seq, fn sequence ->
perform_insertions_lossy(sequence, mappings)
end)
{{_, min}, {_, max}} =
final_sequence
|> String.split("", trim: true)
|> Enum.frequencies()
final_letter_freqs
|> Enum.min_max_by(&elem(&1, 1))
max - min
end
def p1, do: calculate_score_of_iteration(10)
def p2 do
{sequence, _mappings} = input()
to_frequencies(sequence)
end
def p2, do: calculate_score_of_iteration(40)
end