花札シャッフル

この問題を解くことがあったんだけど,その時はRubyで非常にあれなコードだったので,とりあえずD言語で書き直してみた.やっぱりRubyの方がすっきり書けるというか,std.range周りが色々と融通が効かなくてあばば.Rubyの配列のように扱えるeagerな処理も欲しいなぁ…

// Written in the D programming language.

/**
 * Hanafuda shuffle
 *
 * See_Also:
 *  http://www.ehime-u.ac.jp/ICPC/problems/domestic/d2004/A.jp/A.html
 */

import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
import std.string;
import std.typecons;


alias Tuple!(uint, "first", uint, "second") Pair;


/**
 * Parses formatted input.
 */
Pair parseInput()
{
    auto parsed = map!"to!uint(a)"(readln().split());

    Pair result;

    result.first  = parsed.front; parsed.popFront();
    result.second = parsed.front;

    return result;
}


/**
 * Shuffles hanafuda set.
 */
void shuffle(ref uint[] set)
{
    auto input = parseInput();
    auto point = set.length - input.first + 1;
    auto lower = point - input.second;
    auto pSet  = set[point..$].dup;
    auto cSet  = set[lower..point].dup;

    set[lower..$ - input.second] = pSet;
    set[$ - input.second..$]     = cSet;
}


void main()
{
    while (true) {
        auto input = parseInput();

        if (input.first == 0 && input.second == 0)
            break;

        auto set = array(iota(1, input.first + 1));

        foreach (Unused; 0..input.second)
            shuffle(set);

        writeln(set.back);
    }
}