Compare commits

..

No commits in common. "550b652b45bdef5c88053d435fe8bf3b6542201a" and "522bbf2deee01a02846c7a1f3dee02be99c428e3" have entirely different histories.

4 changed files with 0 additions and 235 deletions

View file

@ -1,52 +0,0 @@
defmodule Day1 do
def part1(<<>>), do: 0
def part1(<<?(, rest::binary>>), do: 1 + part1(rest)
def part1(<<?), rest::binary>>), do: -1 + part1(rest)
def part2(input), do: find_basement(input, _floor = 0, _pos = 1)
def find_basement(<<?), _::binary>>, 0, pos), do: pos
def find_basement(<<?), rest::binary>>, floor, pos), do: find_basement(rest, floor - 1, pos + 1)
def find_basement(<<?(, rest::binary>>, floor, pos), do: find_basement(rest, floor + 1, pos + 1)
def input do
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
input
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 day1.exs input_filename")
end
end
Day1.run()

View file

@ -1,66 +0,0 @@
defmodule Day2 do
def part1(input) do
input
|> Enum.map(fn [x, y, z] ->
a = x * y
b = x * z
c = y * z
min = Enum.min([a, b, c])
2 * (a + b + c) + min
end)
|> Enum.sum()
end
def part2(input) do
input
|> Enum.map(fn box ->
[x, y, z] = Enum.sort(box)
2 * (x + y) + x * y * z
end)
|> Enum.sum()
end
def input do
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
input
|> String.split(["\n", "x"], trim: true)
|> Enum.map(&String.to_integer/1)
|> Enum.chunk_every(3)
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 day2.exs input_filename")
end
end
Day2.run()

View file

@ -1,67 +0,0 @@
defmodule Day3 do
def part1(input), do: move(input, MapSet.new([{0, 0}]), {0, 0})
def move(<<?<, rest::binary>>, houses, {x, y}), do: deliver(rest, houses, {x - 1, y})
def move(<<?>, rest::binary>>, houses, {x, y}), do: deliver(rest, houses, {x + 1, y})
def move(<<?^, rest::binary>>, houses, {x, y}), do: deliver(rest, houses, {x, y - 1})
def move(<<?v, rest::binary>>, houses, {x, y}), do: deliver(rest, houses, {x, y + 1})
def move(<<>>, houses, _addr), do: MapSet.size(houses)
def deliver(moves, houses, addr), do: move(moves, MapSet.put(houses, addr), addr)
def part2(input), do: move2(input, MapSet.new([{0, 0}]), {0, 0}, {0, 0})
def move2(<<ms, mb, rest::binary>>, houses, santa, bot) do
santa = next(ms, santa)
bot = next(mb, bot)
move2(rest, MapSet.union(houses, MapSet.new([santa, bot])), santa, bot)
end
def move2(<<>>, houses, _santa, _bot), do: MapSet.size(houses)
def next(?<, {x, y}), do: {x - 1, y}
def next(?>, {x, y}), do: {x + 1, y}
def next(?^, {x, y}), do: {x, y - 1}
def next(?v, {x, y}), do: {x, y + 1}
def input do
with [input_filename] <- System.argv(),
{:ok, input} <- File.read(input_filename) do
input
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 day3.exs input_filename")
end
end
Day3.run()

View file

@ -1,50 +0,0 @@
defmodule Day4 do
def part1(input), do: mine(input, 1, "00000")
def mine(input, nonce, prefix) do
hash = :crypto.hash(:md5, input <> Integer.to_string(nonce)) |> Base.encode16()
if String.starts_with?(hash, prefix), do: nonce, else: mine(input, nonce + 1, prefix)
end
def part2(input), do: mine(input, 1, "000000")
def input do
with [input] <- System.argv() do
input
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 day4.exs input")
end
end
Day4.run()