Andrew Wiley: > The main thing that frustrated me was that getting input in D wasn't > anywhere near as straightforward as it is in Java. For the first problem,
I have tried to implement a D solution to the first problem, because its input is a bit more complex. I have used C++ code written the winner as starting point. After several failed D versions (this is BAD for D2/Phobos), I've written a Python prototype and then I have translated it to D2: import std.stdio, std.math, std.conv, std.string, std.array, std.algorithm; auto next(R)(ref R range) { auto result = range.front(); range.popFront(); return result; } void main() { auto fin = File("input.txt"); auto fout = File("output.txt", "w"); foreach (i; 0 .. to!int(fin.readln().strip())) { int[2] lastP = 1; int[2] lastT = 0; int t = 0; auto parts = splitter(fin.readln().strip(), " "); foreach (_; 0 .. to!int(next(parts))) { string s = next(parts); int q = to!int(next(parts)); int id = cast(int)(s == "B"); t = max(t, abs(q - lastP[id]) + lastT[id]) + 1; lastP[id] = q; lastT[id] = t; } fout.writefln("Case #%d: %d", i, t); } } Three problems I've found in translating the prototype: - A next() function/method is missing, but I needed it, so I have had to define it, to keep code from becoming hairy and quite less readable. to!int expects a stripped string. In my code I am never sure to have a stripped string coming from input, so I have to always add a strip(), this is dumb: foreach (i; 0 .. to!int(fin.readln().strip())) { ==> foreach (i; 0 .. to!int(fin.readln())) { std.algorithm.splitter() doesn't default to splitting on whitespace as std.string.split() does. This is bad because in this program I need to add a strip() and in general it's bad because if there are two spaces, or a newline, it causes a mess, so I'd like a new overload of splitter() that acts as split(): auto parts = splitter(fin.readln().strip(), " "); ==> auto parts = splitter(fin.readln()); Bye, bearophile