-
-
Save kwando/cc3515a45070d3ced68f4a46d2044c27 to your computer and use it in GitHub Desktop.
Day 17 advent of code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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