1
0
Fork 0

solve 2021 day 8

This commit is contained in:
Sloane Perrault 2022-09-21 09:19:53 -04:00
parent 441bb93569
commit 6abb36d4bb
2 changed files with 28 additions and 115 deletions

View file

@ -15,7 +15,7 @@
| S | M | T | W | T | F | S | | S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | | [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 |
@ -28,3 +28,4 @@
[5]: ./lib/2021/5.ex [5]: ./lib/2021/5.ex
[6]: ./lib/2021/6.ex [6]: ./lib/2021/6.ex
[7]: ./lib/2021/7.ex [7]: ./lib/2021/7.ex
[8]: ./lib/2021/8.ex

View file

@ -21,127 +21,39 @@ aoc 2021, 8 do
def decode_digit_count([_, _, _, _, _, _, _]), do: 1 def decode_digit_count([_, _, _, _, _, _, _]), do: 1
def decode_digit_count(_word), do: 0 def decode_digit_count(_word), do: 0
def decode_digit('abcefg'), do: 0
def decode_digit('cf'), do: 1
def decode_digit('acdeg'), do: 2
def decode_digit('acdfg'), do: 3
def decode_digit('bcdf'), do: 4
def decode_digit('abdfg'), do: 5
def decode_digit('abdefg'), do: 6
def decode_digit('acf'), do: 7
def decode_digit('abcdefg'), do: 8
def decode_digit('abcdfg'), do: 9
def add_mappings(chars, mappings \\ %{})
def add_mappings(_, mappings) when map_size(mappings) == 7, do: mappings
def add_mappings([c, f], mappings),
do:
mappings
|> Map.put_new(c, ?c)
|> Map.put_new(f, ?f)
def add_mappings([a, c, f], mappings),
do:
mappings
|> Map.put_new(a, ?a)
|> Map.put_new(c, ?c)
|> Map.put_new(f, ?f)
def add_mappings([b, c, d, f], mappings),
do:
mappings
|> Map.put_new(b, ?b)
|> Map.put_new(c, ?c)
|> Map.put_new(d, ?d)
|> Map.put_new(f, ?f)
def add_mappings([a, b, c, d, e, f, g], mappings),
do:
mappings
|> Map.put_new(a, ?a)
|> Map.put_new(b, ?b)
|> Map.put_new(c, ?c)
|> Map.put_new(d, ?d)
|> Map.put_new(e, ?e)
|> Map.put_new(f, ?f)
|> Map.put_new(g, ?g)
def add_mappings(_, mappings), do: mappings
def infer_mappings(words) do def infer_mappings(words) do
one = Enum.find(words, &(length(&1) == 2)) |> MapSet.new() %{
seven = Enum.find(words, &(length(&1) == 3)) |> MapSet.new() 2 => [one],
eight = Enum.find(words, &(length(&1) == 7)) |> MapSet.new() 3 => [seven],
four = Enum.find(words, &(length(&1) == 4)) |> MapSet.new() 4 => [four],
5 => two_three_five,
6 => zero_six_nine,
7 => [eight]
} = Enum.group_by(words, &length/1)
bd = MapSet.difference(four, one) {[three], two_five} = two_three_five |> Enum.split_with(&Enum.all?(one, fn x -> x in &1 end))
[six | two_five] = [b] = four -- three
Enum.reject(words, fn word ->
MapSet.subset?(one, MapSet.new(word))
end)
|> Enum.sort_by(&length/1, :desc)
|> Enum.map(&MapSet.new/1)
[c] = MapSet.difference(eight, six) |> MapSet.to_list() {[five], [two]} = two_five |> Enum.split_with(&(b in &1))
[f] = MapSet.delete(one, c) |> MapSet.to_list()
nine = {[nine], six_zero} = zero_six_nine |> Enum.split_with(&Enum.all?(four, fn x -> x in &1 end))
Enum.find(words, fn word -> {[zero], [six]} = six_zero |> Enum.split_with(&Enum.all?(one, fn x -> x in &1 end))
length(word) == 6 and not MapSet.equal?(MapSet.new(word), six)
end)
|> MapSet.new()
[e] = MapSet.difference(eight, nine) |> MapSet.to_list()
five = MapSet.delete(six, e)
two = Enum.find(two_five, &(not MapSet.equal?(&1, five)))
three = two |> MapSet.delete(e) |> MapSet.put(f)
[b] = MapSet.difference(nine, three) |> MapSet.to_list()
[d] = MapSet.delete(bd, b) |> MapSet.to_list()
zero = MapSet.delete(eight, d)
%{ %{
MapSet.to_list(zero) => 0, Enum.sort(zero) => 0,
MapSet.to_list(one) => 1, Enum.sort(one) => 1,
MapSet.to_list(two) => 2, Enum.sort(two) => 2,
MapSet.to_list(three) => 3, Enum.sort(three) => 3,
MapSet.to_list(four) => 4, Enum.sort(four) => 4,
MapSet.to_list(five) => 5, Enum.sort(five) => 5,
MapSet.to_list(six) => 6, Enum.sort(six) => 6,
MapSet.to_list(seven) => 7, Enum.sort(seven) => 7,
MapSet.to_list(eight) => 8, Enum.sort(eight) => 8,
MapSet.to_list(nine) => 9 Enum.sort(nine) => 9
} }
end end
def rewire(mappings, word), do: Enum.map(word, &Map.get(mappings, &1, ?x)) |> Enum.sort()
def render_mappings(mappings) do
s = 32
IO.inspect(mappings)
[a, b, c, d, e, f, g] = ?a..?g |> Enum.map(&Map.get(mappings, &1, ?x))
Enum.map(
[
[s, a, a, a, a, s],
[b, s, s, s, s, c],
[b, s, s, s, s, c],
[s, d, d, d, d, s],
[e, s, s, s, s, f],
[e, s, s, s, s, f],
[s, g, g, g, g, s]
],
&IO.chardata_to_string/1
)
|> Enum.join("\n")
|> IO.puts()
end
def p1 do def p1 do
parse_input() parse_input()
|> Enum.map(fn {_, output} -> |> Enum.map(fn {_, output} ->
@ -157,10 +69,10 @@ aoc 2021, 8 do
|> Enum.map(fn {signals, output} -> |> Enum.map(fn {signals, output} ->
mappings = infer_mappings(signals) mappings = infer_mappings(signals)
output for word <- output do
|> Enum.map(&Map.get(mappings, &1)) Map.get(mappings, Enum.sort(word))
end
|> Integer.undigits() |> Integer.undigits()
|> IO.inspect()
end) end)
|> Enum.sum() |> Enum.sum()
end end