Skip to content

Instantly share code, notes, and snippets.

@kwando

kwando/day17.exs Secret

Created December 17, 2017 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwando/cc3515a45070d3ced68f4a46d2044c27 to your computer and use it in GitHub Desktop.
Save kwando/cc3515a45070d3ced68f4a46d2044c27 to your computer and use it in GitHub Desktop.
Day 17 advent of code
defmodule AoC.Day17 do
def part1(n) do
part1([0], 0, 0, n)
|> Enum.drop_while(&( &1 != 2017 ))
|> Enum.at(1)
end
defp part1(buffer, _, 2017, _), do: buffer
defp part1(buffer, current_index, iteration, n) do
current_index = rem(current_index + n, iteration + 1) + 1
buffer = Enum.take(buffer, current_index) ++ [iteration + 1] ++ Enum.drop(buffer, current_index)
part1(
buffer,
current_index,
iteration + 1,
n
)
end
def part2(n) do
part2(-1, 0, 0, n)
end
defp part2(value, current_index, 50_000_000, n), do: value
defp part2(value, current_index, iteration, n) do
current_index = rem(current_index + n, iteration + 1) + 1
value = if current_index == 1, do: iteration + 1, else: value
part2(value, current_index, iteration + 1, n)
end
end
AoC.Day17.part1(371)
|> IO.inspect(label: "part 1")
AoC.Day17.part2(371)
|> IO.inspect(label: "part 2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment