2021 day 14
This commit is contained in:
parent
a1ad866646
commit
b82ec31fe3
2 changed files with 11 additions and 27 deletions
|
@ -16,7 +16,7 @@
|
||||||
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
|
||||||
| | | | [1] | [2] | [3] | [4] |
|
| | | | [1] | [2] | [3] | [4] |
|
||||||
| [5] | [6] | [7] | [8] | [9] | [10] | [11]|
|
| [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 |
|
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
|
||||||
|
|
||||||
<!-- links -->
|
<!-- links -->
|
||||||
|
@ -34,3 +34,4 @@
|
||||||
[11]: ./lib/2021/11.ex
|
[11]: ./lib/2021/11.ex
|
||||||
[12]: ./lib/2021/12.ex
|
[12]: ./lib/2021/12.ex
|
||||||
[13]: ./lib/2021/13.ex
|
[13]: ./lib/2021/13.ex
|
||||||
|
[14]: ./lib/2021/14.ex
|
||||||
|
|
|
@ -20,15 +20,6 @@ aoc 2021, 14 do
|
||||||
{sequence, insertion_mappings}
|
{sequence, insertion_mappings}
|
||||||
end
|
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),
|
def perform_insertions_lossy(sequence, mappings) when is_binary(sequence),
|
||||||
do:
|
do:
|
||||||
sequence
|
sequence
|
||||||
|
@ -37,10 +28,10 @@ aoc 2021, 14 do
|
||||||
|
|
||||||
def perform_insertions_lossy({letter_freqs, pair_freqs}, mappings) do
|
def perform_insertions_lossy({letter_freqs, pair_freqs}, mappings) do
|
||||||
[pair_ops, letter_ops] =
|
[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)
|
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}],
|
{[{from, -existing_pairs}, {front <> to, existing_pairs}, {to <> back, existing_pairs}],
|
||||||
[{to, existing_pairs}]}
|
[{to, existing_pairs}]}
|
||||||
|
@ -73,27 +64,19 @@ aoc 2021, 14 do
|
||||||
{letter_frequencies, pair_frequencies}
|
{letter_frequencies, pair_frequencies}
|
||||||
end
|
end
|
||||||
|
|
||||||
def calculate_score_of_iteration(input \\ input(), n) do
|
def calculate_score_of_iteration({starting_seq, mappings} \\ input(), n) do
|
||||||
{final_sequence, _} =
|
{final_letter_freqs, _} =
|
||||||
iterate(n, input, fn {sequence, mappings} ->
|
iterate(n, starting_seq, fn sequence ->
|
||||||
next = perform_insertions(sequence, mappings)
|
perform_insertions_lossy(sequence, mappings)
|
||||||
{next, mappings}
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
{{_, min}, {_, max}} =
|
{{_, min}, {_, max}} =
|
||||||
final_sequence
|
final_letter_freqs
|
||||||
|> String.split("", trim: true)
|
|
||||||
|> Enum.frequencies()
|
|
||||||
|> Enum.min_max_by(&elem(&1, 1))
|
|> Enum.min_max_by(&elem(&1, 1))
|
||||||
|
|
||||||
max - min
|
max - min
|
||||||
end
|
end
|
||||||
|
|
||||||
def p1, do: calculate_score_of_iteration(10)
|
def p1, do: calculate_score_of_iteration(10)
|
||||||
|
def p2, do: calculate_score_of_iteration(40)
|
||||||
def p2 do
|
|
||||||
{sequence, _mappings} = input()
|
|
||||||
|
|
||||||
to_frequencies(sequence)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue