All pastes #344839 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-02-08 01:30 UTC
rendered paste body
public class run {
	public static void main ( String args[] ) {
		
		Robot robot = new Robot (args.length>0? args[0] : "");
		
		int lcount = 0;
		int lpeek = 0;
		int rcount = robot.nbins()-1;
		int rpeek = rcount;
		
		if (rcount <= 0) {
			robot.report();
			return;
		}
		
		while(robot.peek(lcount) == 'r' && lcount < rcount) lcount++;
		lpeek = lcount;

		while(robot.peek(rcount) == 'b' && rcount > lcount) rcount--;
		rpeek = rcount;
		
		while(true) {
			while ( robot.peek(lpeek) != 'r' && lpeek < rcount) 
				lpeek++;
			if (lpeek <= rcount) {
				robot.swap(lpeek, lcount);
				if (lpeek == rcount)					
					break;
				else
					lcount++;
			}
		}
		
		while(true) {
			while (robot.peek(rpeek) != 'b' && rpeek > lcount) 
				rpeek--;
			if (rpeek >= lcount) {
				if (rpeek == lcount) {
					break;
				} else {
					robot.swap(rpeek, rcount);
					rcount--;
					rpeek--;
				}
			}
		}
			

		robot.report();
	}
}