There is no Spoon - Episode 1 puzzle discussion


#198

I can’t find why it does not work online : Timeout: the program did not provide 1 input lines in due time…
It’s okay on my IDE and returns within less than 50ms

import java.util.Scanner;

class Player {
    
    public static String findDown(final boolean[][] cells, final int i, final int j, final int h) {
        for (int y = j + 1; y < h; y++) {
            if (cells[i][y]) {
                return i + " " + y;
            }
        }
        return -1 + " " + -1;
    }

    public static String findRight(final boolean[][] cells, final int i, final int j, final int w) {
        for (int x = i + 1; x < w; x++) {
            if (cells[x][j]) {
                return x + " " + j;
            }
        }
        return -1 + " " + -1;
    }
    
    public static void main(final String args[]) {

        final long time = System.currentTimeMillis();

        System.err.println("starting at " + time);

        final Scanner in = new Scanner(System.in);
        final int width = in.nextInt(); // the number of cells on the X axis
        final int height = in.nextInt(); // the number of cells on the Y axis
        if (in.hasNextLine()) {
            in.nextLine();
        }
        final boolean[][] cells = new boolean[width][height];
        int lineNumber = 0;
        while (in.hasNextLine()) {
            final String line = in.nextLine(); // width characters, each either 0 or .
            for (int c = 0; c < line.length(); c++) {
                cells[c][lineNumber] = line.charAt(c) == '0';
            }
            lineNumber++;
        }

        System.err.println("cells is built in " + (System.currentTimeMillis() - time));
        
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                if (cells[i][j]) {
                    final String cell = i + " " + j + " "
                            + Player.findRight(cells, i, j, width) + " "
                            + Player.findDown(cells, i, j, height);
                    System.out.println(cell);
                }
            }

        }

        System.err.println("finished in " + (System.currentTimeMillis() - time));

    }
}

#199

you can’t use
while (in.hasNextLine()) {
after reading all the inputs, the scanner blocks waiting for another input, hence the timeout. A simple loop for on height will solve your day


#200

Thanks, and it worked in my IDE because I was passing the input through “java Test < file” !


#201

The problem is “Timeout: the program did not provide 1 input lines in due time…”