From 9e6a872461e7a6ecc06374a802a5258dfa42a6db Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Mon, 5 Dec 2022 23:40:41 +0900 Subject: [PATCH] 2022 Day 5 --- 2022/README.md | 2 +- 2022/day5.exs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 2022/day5.exs diff --git a/2022/README.md b/2022/README.md index df95749..e258ae8 100644 --- a/2022/README.md +++ b/2022/README.md @@ -2,7 +2,7 @@ My (attempted) solutions to [Advent of Code 2022](https://adventofcode.com/2022) in Elixir. -image +image ## Running: diff --git a/2022/day5.exs b/2022/day5.exs new file mode 100644 index 0000000..1a999ee --- /dev/null +++ b/2022/day5.exs @@ -0,0 +1,90 @@ +defmodule Day5 do + def part1(input), do: input |> move_with(&move_9000/3) |> top_crates() + def part2(input), do: input |> move_with(&move_9001/3) |> top_crates() + + defp move_with({stacks, procedure}, move) do + for [number, from_index, to_index] <- procedure, reduce: stacks do + stacks -> + {moved_from, moved_to} = move.(number, stacks[from_index], stacks[to_index]) + %{stacks | from_index => moved_from, to_index => moved_to} + end + end + + defp move_9000(0, from, to), do: {from, to} + defp move_9000(number, [crate | from], to), do: move_9000(number - 1, from, [crate | to]) + + defp move_9001(number, from, to) do + {moving, moved_from} = Enum.split(from, number) + {moved_from, moving ++ to} + end + + defp top_crates(stacks) do + Enum.reduce(stacks, "", fn {_label, [top | _stack]}, acc -> acc <> top end) + end + + def input do + with [input_filename] <- System.argv(), + {:ok, input} <- File.read(input_filename) do + [raw_stacks, raw_procedure] = String.split(input, "\n\n") + + stacks = + raw_stacks + |> String.split("\n") + |> parse_stacks() + |> Enum.zip() + |> Enum.map(fn stack -> stack |> Tuple.to_list() |> Enum.drop_while(&is_nil/1) end) + |> Enum.with_index() + |> Map.new(fn {stack, index} -> {index + 1, stack} end) + + procedure = + raw_procedure + |> String.split(["\n", " ", "move", "from", "to"], trim: true) + |> Enum.map(&String.to_integer/1) + |> Enum.chunk_every(3) + + {stacks, procedure} + else + _ -> :error + end + end + + defp parse_stacks([_labels_]), do: [] + defp parse_stacks([line | rest]), do: [parse_line(line) | parse_stacks(rest)] + + defp parse_line(<<"">>), do: [] + defp parse_line(<<" ", rest::binary>>), do: [nil | parse_line(rest)] + defp parse_line(<<"[", crate::binary-1, "]", rest::binary>>), do: [crate | parse_line(rest)] + defp parse_line(<<" ", rest::binary>>), do: parse_line(rest) + + ####################### + # 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 day5.exs input_filename") + end +end + +Day5.run()