Last active
December 17, 2017 12:08
-
-
Save sasa1977/97e97e69eefb1f98bc6f405eccf88a4d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Day17 do | |
def part1(num_steps), do: | |
insert(2017, num_steps, [], &List.insert_at/3) | |
|> Stream.chunk_every(2, 1) | |
|> Enum.find(&match?([2017, _], &1)) | |
|> Enum.at(1) | |
def part2(num_steps), do: | |
insert(50_000_000, num_steps, nil, | |
fn | |
(_current_after_zero, 1, new_after_zero) -> new_after_zero | |
(current_after_zero, _pos, _value) -> current_after_zero | |
end | |
) | |
defp insert(count, num_steps, acc, reducer), do: | |
insert(count + 1, num_steps, acc, reducer, 0, 0) | |
defp insert(0, _num_steps, acc, _reducer, _value, _pos), do: acc | |
defp insert(count, num_steps, acc, reducer, value, pos) do | |
acc = reducer.(acc, pos, value) | |
next_value = value + 1 | |
next_pos = rem(pos + num_steps, next_value) + 1 | |
insert(count - 1, num_steps, acc, reducer, next_value, next_pos) | |
end | |
end | |
System.argv() |> hd() |> String.to_integer() |> Day17.part1() |> IO.inspect | |
System.argv() |> hd() |> String.to_integer() |> Day17.part2() |> IO.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment