import std.stdio : writeln;
import std.algorithm.comparison: min;
import std.algorithm.iteration: sum;
import core.time: MonoTime, Duration;
auto sum_subranges(T)(T input, uint range)
{
import std.range : chunks, ElementType, array;
import std.algorithm : map;
if (range == 0)
{
return ElementType!(T)[].init;
}
return input.chunks(range).map!(sum).array;
}
unittest
{
assert(sum_subranges([1,1,1], 2) == [2, 1]);
assert(sum_subranges([1,1,1,2,3,3], 2) == [2, 3, 6]);
assert(sum_subranges([], 2) == []);
assert(sum_subranges([1], 2) == [1]);
assert(sum_subranges([1], 0) == []);
}
void main()
{
import std.range : iota, array;
auto v = iota(0,1000000).array;
int sum;
MonoTime beg = MonoTime.currTime;
for (int i=0; i < 100; i++)
sum += cast(int)sum_subranges(v,2).length;
MonoTime end = MonoTime.currTime;
writeln(end-beg);
writeln(sum);
}
aW1wb3J0IHN0ZC5zdGRpbyA6IHdyaXRlbG47CmltcG9ydCBzdGQuYWxnb3JpdGhtLmNvbXBhcmlzb246IG1pbjsKaW1wb3J0IHN0ZC5hbGdvcml0aG0uaXRlcmF0aW9uOiBzdW07CmltcG9ydCBjb3JlLnRpbWU6IE1vbm9UaW1lLCBEdXJhdGlvbjsKCgphdXRvIHN1bV9zdWJyYW5nZXMoVCkoVCBpbnB1dCwgdWludCByYW5nZSkKewogICAgaW1wb3J0IHN0ZC5yYW5nZSA6IGNodW5rcywgRWxlbWVudFR5cGUsIGFycmF5OwogICAgaW1wb3J0IHN0ZC5hbGdvcml0aG0gOiBtYXA7CgogICAgaWYgKHJhbmdlID09IDApCiAgICB7CiAgICAgICAgcmV0dXJuIEVsZW1lbnRUeXBlIShUKVtdLmluaXQ7CiAgICB9CiAgICByZXR1cm4gaW5wdXQuY2h1bmtzKHJhbmdlKS5tYXAhKHN1bSkuYXJyYXk7Cn0KCnVuaXR0ZXN0CnsKICAgIGFzc2VydChzdW1fc3VicmFuZ2VzKFsxLDEsMV0sIDIpID09IFsyLCAxXSk7CiAgICBhc3NlcnQoc3VtX3N1YnJhbmdlcy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