-
Notifications
You must be signed in to change notification settings - Fork 7
/
7.jl
37 lines (35 loc) · 895 Bytes
/
7.jl
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
function solve(nums, use_growing=false)
counter = zeros(Int, maximum(nums) + 1)
grow = copy(counter)
for num in nums
counter[num + 1] += 1
end
s = 0
while length(counter) > 1
front, back = counter[1] + grow[1], counter[end] + grow[end]
if front < back
counter[2] += counter[1]
popfirst!(counter)
if use_growing
popfirst!(grow)
grow[1] = front
end
s += front
else
counter[end-1] += counter[end]
pop!(counter)
if use_growing
pop!(grow)
grow[end] = back
end
s += back
end
end
return s
end
function main()
nums = (x -> parse(Int, x)).(split(readlines()[1], ','))
println(solve(nums, false))
println(solve(nums, true))
end
main()