To help LDC/GDC
bearophile
bearophileHUGS at lycos.com
Thu May 2 02:40:31 PDT 2013
To: NG D
Subject: Re: To help LDC/GDC
This D code comes from a Haskell solution by tanakh of the
Lawnmower problem of the Google Code Jam 2013 Qualification Round:
import std.stdio, std.string, std.conv, std.range, std.algorithm;
void main() {
immutable n = stdin.readln.strip.to!int;
foreach (immutable cn; 1 .. n + 1) {
const hw = stdin.readln.split.to!(int[]);
const bd = hw[0].iota.map!(i =>
stdin.readln.split.to!(int[])).array;
const tbd = hw[1].iota.map!(i =>
bd.transversal(i).array).array;
bool checkH(in int ch) {
const okX = tbd.map!(xl => xl.all!(x => x <= ch)).array;
foreach (oky, r; bd.map!(yl => yl.all!(y => y <=
ch)).zip(bd))
//foreach (okx, c; tbd.map!(xl => xl.all!(x => x <=
ch)).zip(r))//Slow.
foreach (okx, c; okX.zip(r))
if (c <= ch && !(okx || oky))
return false;
return true;
}
writefln("Case #%d: %s", cn, iota(1, 101).all!checkH ? "YES"
: "NO");
}
}
In the inner foreach I have had to compute okX before the loops
otherwise the code gets several times slower. Is such
optimization possible from a D compiler? Both "map" and "all"
should be pure. The Haskell compiler uses library-defined
"rewrite rules" to help in such cases.
Bye,
bearophile
More information about the Digitalmars-d
mailing list