Created
December 15, 2017 09:06
-
-
Save sasa1977/2e9f0d59741db9eae26a4f11b2c7773d to your computer and use it in GitHub Desktop.
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 Day15 do | |
require Bitwise | |
def part1(a, b), do: | |
judge(40_000_000, a, b, &rem(&1 * 16807, 2147483647), &rem(&1 * 48271, 2147483647)) | |
def part2(a, b), do: | |
judge(5_000_000, a, b, &find_next_factor(&1, 16807, 4), &find_next_factor(&1, 48271, 8)) | |
defp judge(steps, a, b, generator_a, generator_b, count \\ 0) | |
defp judge(0, _a, _b, _generator_a, _generator_b, count), do: count | |
defp judge(steps, a, b, generator_a, generator_b, count) do | |
a = generator_a.(a) | |
b = generator_b.(b) | |
count = if Bitwise.band(a, 0xFFFF) == Bitwise.band(b, 0xFFFF), do: count + 1, else: count | |
judge(steps - 1, a, b, generator_a, generator_b, count) | |
end | |
defp find_next_factor(x, multiplier, factor_of) do | |
x = rem(x * multiplier, 2147483647) | |
if rem(x, factor_of) == 0, do: x, else: find_next_factor(x, multiplier, factor_of) | |
end | |
end | |
[a, b] = System.argv() |> Enum.map(&String.to_integer/1) | |
Day15.part1(a, b) |> IO.inspect | |
Day15.part2(a, b) |> IO.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment