-
Notifications
You must be signed in to change notification settings - Fork 0
/
jumping_bunnies.ex
49 lines (42 loc) · 916 Bytes
/
jumping_bunnies.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
defmodule GCD do
@doc """
Jumping Bunnies
https://www.hackerrank.com/challenges/jumping-bunnies
"""
def solve(a, b) do
cond do
a == b -> a
a > b ->
remain = rem(a, b)
if remain == 0 do
b
else
solve(remain, b)
end
a < b ->
remain = rem(b, a)
if remain == 0 do
a
else
solve(a, remain)
end
end
end
end
defmodule JumpingBunnies do
def solve(distances) do
[acc | distances] = [hd(distances) | tl(distances)]
Enum.reduce(distances, acc,
fn (d, acc) -> div(d, GCD.solve(d, acc)) * acc end)
end
def main() do
_ = elem(Integer.parse(IO.gets("")), 0)
distances = IO.gets("")
|> String.trim()
|> String.split(" ")
|> Enum.map(&(elem(Integer.parse(&1), 0)))
|> Enum.sort()
IO.puts(solve(distances))
end
end
JumpingBunnies.main()