2021 day 16 part 2
This commit is contained in:
parent
84d40cce99
commit
13e154a0c2
1 changed files with 28 additions and 11 deletions
|
@ -4,21 +4,17 @@ defmodule Day16 do
|
||||||
version_sum
|
version_sum
|
||||||
end
|
end
|
||||||
|
|
||||||
def part2(_input) do
|
|
||||||
:ok
|
|
||||||
end
|
|
||||||
|
|
||||||
@literal_type 4
|
@literal_type 4
|
||||||
@literal_part 1
|
@literal_part 1
|
||||||
@literal_end 0
|
@literal_end 0
|
||||||
@total_length_type 0
|
@total_length_type 0
|
||||||
@sub_packets_length_type 1
|
@sub_packets_length_type 1
|
||||||
|
|
||||||
def decode_packet(<<version::3, @literal_type::3, rest::bits>>, count, version_sum) do
|
defp decode_packet(<<version::3, @literal_type::3, rest::bits>>, count, version_sum) do
|
||||||
decode_literal(rest, <<>>, count + 6, version_sum + version)
|
decode_literal(rest, <<>>, count + 6, version_sum + version)
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_packet(<<version::3, operator_type::3, rest::bits>>, count, version_sum) do
|
defp decode_packet(<<version::3, operator_type::3, rest::bits>>, count, version_sum) do
|
||||||
{sub_packets, rest, count, version_sum} =
|
{sub_packets, rest, count, version_sum} =
|
||||||
case rest do
|
case rest do
|
||||||
<<@total_length_type::1, length::15, rest::bits>> ->
|
<<@total_length_type::1, length::15, rest::bits>> ->
|
||||||
|
@ -45,21 +41,21 @@ defmodule Day16 do
|
||||||
{{:operator, operator_type, sub_packets}, rest, count, version_sum}
|
{{:operator, operator_type, sub_packets}, rest, count, version_sum}
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_literal(<<@literal_part::1, group::bits-4, rest::bits>>, acc, count, version_sum) do
|
defp decode_literal(<<@literal_part::1, group::bits-4, rest::bits>>, acc, count, version_sum) do
|
||||||
decode_literal(rest, <<acc::bits, group::bits>>, count + 5, version_sum)
|
decode_literal(rest, <<acc::bits, group::bits>>, count + 5, version_sum)
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_literal(<<@literal_end::1, group::bits-4, rest::bits>>, acc, count, version_sum) do
|
defp decode_literal(<<@literal_end::1, group::bits-4, rest::bits>>, acc, count, version_sum) do
|
||||||
literal_binary = <<acc::bits, group::bits>>
|
literal_binary = <<acc::bits, group::bits>>
|
||||||
<<literal::size(bit_size(literal_binary))>> = literal_binary
|
<<literal::size(bit_size(literal_binary))>> = literal_binary
|
||||||
{{:literal, literal}, rest, count + 5, version_sum}
|
{{:literal, literal}, rest, count + 5, version_sum}
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_sub_packets_by(_method, 0, decoded_packets, bits, count, version_sum) do
|
defp decode_sub_packets_by(_method, 0, decoded_packets, bits, count, version_sum) do
|
||||||
{Enum.reverse(decoded_packets), bits, count, version_sum}
|
{Enum.reverse(decoded_packets), bits, count, version_sum}
|
||||||
end
|
end
|
||||||
|
|
||||||
def decode_sub_packets_by(method, remaining, decoded_packets, bits, count, version_sum) do
|
defp decode_sub_packets_by(method, remaining, decoded_packets, bits, count, version_sum) do
|
||||||
{decoded_packet, rest, packet_size, version_sum} = decode_packet(bits, 0, version_sum)
|
{decoded_packet, rest, packet_size, version_sum} = decode_packet(bits, 0, version_sum)
|
||||||
|
|
||||||
remaining =
|
remaining =
|
||||||
|
@ -78,6 +74,27 @@ defmodule Day16 do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def part2(input) do
|
||||||
|
{packet, _rest, _count, _version_sum} = decode_packet(input, 0, 0)
|
||||||
|
evaluate(packet)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp evaluate({:literal, literal}), do: literal
|
||||||
|
|
||||||
|
defp evaluate({:operator, op, args}) do
|
||||||
|
args
|
||||||
|
|> Enum.map(&evaluate/1)
|
||||||
|
|> calculate(op)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp calculate(args, 0), do: Enum.sum(args)
|
||||||
|
defp calculate(args, 1), do: Enum.reduce(args, &Kernel.*/2)
|
||||||
|
defp calculate(args, 2), do: Enum.min(args)
|
||||||
|
defp calculate(args, 3), do: Enum.max(args)
|
||||||
|
defp calculate([a, b], 5), do: if(a > b, do: 1, else: 0)
|
||||||
|
defp calculate([a, b], 6), do: if(a < b, do: 1, else: 0)
|
||||||
|
defp calculate([a, b], 7), do: if(a == b, do: 1, else: 0)
|
||||||
|
|
||||||
def input do
|
def input do
|
||||||
with [input_filename] <- System.argv(),
|
with [input_filename] <- System.argv(),
|
||||||
{:ok, input} <- File.read(input_filename) do
|
{:ok, input} <- File.read(input_filename) do
|
||||||
|
@ -120,4 +137,4 @@ defmodule Day16 do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Day16.run()
|
Day16.run()
|
||||||
|
|
Loading…
Reference in a new issue