Compare commits
4 commits
522bbf2dee
...
550b652b45
Author | SHA1 | Date | |
---|---|---|---|
550b652b45 | |||
f1762d886a | |||
6eee6cace0 | |||
b788fa1096 |
4 changed files with 235 additions and 0 deletions
52
2015/day1.exs
Normal file
52
2015/day1.exs
Normal file
|
@ -0,0 +1,52 @@
|
|||
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()
|
66
2015/day2.exs
Normal file
66
2015/day2.exs
Normal file
|
@ -0,0 +1,66 @@
|
|||
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()
|
67
2015/day3.exs
Normal file
67
2015/day3.exs
Normal file
|
@ -0,0 +1,67 @@
|
|||
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()
|
50
2015/day4.exs
Normal file
50
2015/day4.exs
Normal file
|
@ -0,0 +1,50 @@
|
|||
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()
|
Loading…
Reference in a new issue