/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication21;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 *
 * @author Ben Wright
 */
public class Main {

    /**
     * And what a hack this is...
     * @param args the command line arguments
     *
     */
    public static void main(String[] args) {
        try {
            connect();

            //process() was what I actually used, I've since made the more elegant
            //connect(), to by-pass any tedious browser rubbish.
            //process();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void connect() throws Exception {
        URL url = new URL("http://faux.uwcs.co.uk/progcomp20110127/");
        Object obj = url.getContent();
        BufferedReader is = new BufferedReader(new InputStreamReader((InputStream) obj));
        StringBuilder sb = new StringBuilder();
        while (true) {
            String str = is.readLine();
            if (str == null) {
                break;
            }
            sb.append(str);
            sb.append('\n');
        }
        String thing = sb.toString().split("<pre>\n")[1].split("EOF")[0];
        String map = sb.toString().split("Problem: </label><input type=\"text\" name=\"p\" id=\"p\" value=\"")[1];
        map = map.split("\"/></p>")[0];
        System.out.println(thing);

        String solution = solve(thing);

        URL surl = new URL("http://faux.uwcs.co.uk/progcomp20110127/?a=" + solution + "&n=Queex&p=" + map);
        obj = surl.getContent();
        is = new BufferedReader(new InputStreamReader((InputStream) obj));
        sb = new StringBuilder();
        while (true) {
            String str = is.readLine();
            if (str == null) {
                break;
            }
            sb.append(str);
            sb.append('\n');
        }
        System.out.println(sb.toString());
    }

    private static void process() throws Exception {
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
        String clip = (String) clipboard.getContents(null).getTransferData(DataFlavor.stringFlavor);

        clip = clip.substring(0, clip.length() - 4);

        System.out.println(clip);

        String solution = solve(clip);

        clipboard.setContents(new StringSelection(solution), null);

    }

    private static String solve(String maze) {
        //isn't flawless, but doesn't need to be because you can always try again.
        int[] pos = new int[6];
        String[] line = maze.split("\n");
        int width = line[0].length();
        pos[0] = width / 2;
        for (int ii = 1; ii < 6; ii++) {
            String str = line[ii * 10];
            System.out.println(str);
            int jj = 1;
            while (str.charAt(jj) == '#') {
                jj++;
            }
            //a bit of padding to avoid catching the tip due to wobble
            int min = jj + 3;
            jj = width - 1;
            while (str.charAt(jj) == '#') {
                jj--;
            }
            int max = jj - 3;
            if (Math.abs(max - pos[ii - 1]) < Math.abs(min - pos[ii - 1])) {
                pos[ii] = max;
            } else {
                pos[ii] = min;
            }
        }
        for (int ii = 0; ii < 6; ii++) {
            System.out.println(pos[ii]);
        }

        String solution = "";
        for (int ii = 1; ii < 6; ii++) {
            solution += move(pos[ii - 1], pos[ii]);
        }
        solution += end(line.length - 50);
        System.out.println(solution);
        return solution;

    }

    private static String move(int start, int end) {
        int cur = start;
        String out = "";
        for (int ii = 0; ii < 10; ii++) {
            if (cur > end) {
                out += "l";
                cur--;
            } else {
                out += "r";
                cur++;
            }
        }
        return out;
    }

    private static String end(int num) {
        return "lrlrlrlrlr".substring(0, num - 1);
    }
}

