Compare commits

..

No commits in common. "13e154a0c294b309fd57ac9b512c61f72522e5a2" and "53fe13031ed0eec142140a7971cc4c3c8086f098" have entirely different histories.

2 changed files with 0 additions and 246 deletions

View file

@ -1,106 +0,0 @@
defmodule Day13 do
defmodule Paper do
defstruct dots: MapSet.new(), instructions: []
end
def part1(paper) do
folded = fold(paper)
MapSet.size(folded.dots)
end
defp fold(%Paper{dots: dots, instructions: [{axis, pos} | rest]}) do
dots =
MapSet.new(dots, fn {x, y} ->
cond do
axis == "x" and x > pos -> {pos - (x - pos), y}
axis == "y" and y > pos -> {x, pos - (y - pos)}
true -> {x, y}
end
end)
%Paper{dots: dots, instructions: rest}
end
def part2(paper) do
paper
|> fold_all
|> draw
end
defp fold_all(%Paper{instructions: []} = paper), do: paper
defp fold_all(%Paper{} = paper), do: paper |> fold |> fold_all
defp draw(paper) do
{max_x, max_y} =
for {x, y} <- paper.dots, reduce: {0, 0} do
{max_x, max_y} -> {max(x, max_x), max(y, max_y)}
end
for y <- 0..max_y do
row =
for x <- 0..max_x do
if MapSet.member?(paper.dots, {x, y}), do: ?#, else: ?\s
end
# could Enum.join("\n") here, but I want to play with iolists
[row | "\n"]
end
end
def input do
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
[dots, instructions] = String.split(input, "\n\n")
dots =
dots
|> String.split([",", "\n"])
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(2)
|> MapSet.new(fn [x, y] -> {x, y} end)
instructions =
instructions
|> String.split("\n", trim: true)
|> Enum.map(fn <<"fold along ", axis::binary-1, "=", pos::binary>> ->
{axis, String.to_integer(pos)}
end)
%Paper{dots: dots, instructions: instructions}
else
_ -> :error
end
end
#######################
# HERE BE BOILERPLATE #
#######################
def run do
case input() do
:error -> print_usage()
input -> run_parts_with_timer(input)
end
end
defp run_parts_with_timer(input) do
run_with_timer(1, fn -> part1(input) end)
run_with_timer(2, fn -> part2(input) end)
end
defp run_with_timer(part, fun) do
{time, result} = :timer.tc(fun)
IO.puts("Part #{part} (completed in #{format_time(time)}):\n")
IO.puts("#{result}\n")
end
defp format_time(μsec) when μsec < 1_000, do: "#{μsec}μs"
defp format_time(μsec) when μsec < 1_000_000, do: "#{μsec / 1000}ms"
defp format_time(μsec), do: "#{μsec / 1_000_000}s"
defp print_usage do
IO.puts("Usage: elixir day13.exs input_filename")
end
end
Day13.run()

View file

@ -1,140 +0,0 @@
defmodule Day16 do
def part1(input) do
{_type, _rest, _count, version_sum} = decode_packet(input, 0, 0)
version_sum
end
@literal_type 4
@literal_part 1
@literal_end 0
@total_length_type 0
@sub_packets_length_type 1
defp decode_packet(<<version::3, @literal_type::3, rest::bits>>, count, version_sum) do
decode_literal(rest, <<>>, count + 6, version_sum + version)
end
defp decode_packet(<<version::3, operator_type::3, rest::bits>>, count, version_sum) do
{sub_packets, rest, count, version_sum} =
case rest do
<<@total_length_type::1, length::15, rest::bits>> ->
decode_sub_packets_by(
:length,
length,
[],
rest,
count + 3 + 3 + 1 + 15,
version_sum + version
)
<<@sub_packets_length_type::1, num_sub_packets::11, rest::bits>> ->
decode_sub_packets_by(
:quantity,
num_sub_packets,
[],
rest,
count + 3 + 3 + 1 + 11,
version_sum + version
)
end
{{:operator, operator_type, sub_packets}, rest, count, version_sum}
end
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)
end
defp decode_literal(<<@literal_end::1, group::bits-4, rest::bits>>, acc, count, version_sum) do
literal_binary = <<acc::bits, group::bits>>
<<literal::size(bit_size(literal_binary))>> = literal_binary
{{:literal, literal}, rest, count + 5, version_sum}
end
defp decode_sub_packets_by(_method, 0, decoded_packets, bits, count, version_sum) do
{Enum.reverse(decoded_packets), bits, count, version_sum}
end
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)
remaining =
case method do
:length -> remaining - packet_size
:quantity -> remaining - 1
end
decode_sub_packets_by(
method,
remaining,
[decoded_packet | decoded_packets],
rest,
count + packet_size,
version_sum
)
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
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
input
|> String.trim()
|> Base.decode16!()
else
_ -> :error
end
end
#######################
# HERE BE BOILERPLATE #
#######################
def(run) do
case input() do
:error -> print_usage()
input -> run_parts_with_timer(input)
end
end
defp run_parts_with_timer(input) do
run_with_timer(1, fn -> part1(input) end)
run_with_timer(2, fn -> part2(input) end)
end
defp run_with_timer(part, fun) do
{time, result} = :timer.tc(fun)
IO.puts("Part #{part} (completed in #{format_time(time)}):\n")
IO.puts("#{result}\n")
end
defp format_time(μsec) when μsec < 1_000, do: "#{μsec}μs"
defp format_time(μsec) when μsec < 1_000_000, do: "#{μsec / 1000}ms"
defp format_time(μsec), do: "#{μsec / 1_000_000}s"
defp print_usage do
IO.puts("Usage: elixir day16.exs input_filename")
end
end
Day16.run()