There is no Spoon - Episode 1 puzzle discussion

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));

    }
}

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

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

1 Like

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

I passed all the testcases but submit screen (‘loading… computing score’) takes forever for some reason

we had an issue with our “jobs”. It should be ok now

Hello,
There are two weird things when i launch this code : (i don’t know how to put it more nice sorry)

import sys
import math

def vDroite(x,y):.........   
def vBas(x,y):...............
width = int(input())  
height = int(input()) 
tab=[]
for i in range(height):
->->line = input()
->->tab.append(list(line))

for i in width:
->->for j in height:
->->->->if tab[j][i]=='0':
->->->->->->print(str(i)+" "+str(j)+" "+vDroite(i,j)+" "+vBas(i,j)

Sortie d’erreur :

SyntaxError: unexpected EOF while parsing

at Answer.py. not in a function on line 38

Informations :

Initializing APU weaponry…

Informations :

Timeout: the program did not provide 1 input lines in due time…

I don’t understand the last message : why doesn’t the input come ???
And as for the problem on line 38…it doesn’t exist ! My last line is 36, so how can there be a problem ?
Thanks for your help !

This is because you create the list, in a way that you must not.
By typing: array = [[0]*width]*height, you make a list with five references to the same list (in our case, array.
You should write this instead: array= [[0]*width for _ in range(height)

Check these 2:

  1. https://www.geeksforgeeks.org/python-using-2d-arrays-lists-the-right-way/
  2. https://stackoverflow.com/questions/2739552/2d-list-has-weird-behavor-when-trying-to-modify-a-single-value
1 Like

Hello, im trying to solve there is no spoon, but i get issues . Here is my code :

import sys
import math

# Don't let the machines win. You are humanity's last hope...

width = int(input())  # the number of cells on the X axis
height = int(input())  # the number of cells on the Y axis
M = [['0' for i in range(width)] for k in range(height)]
for i in range(height): # i take input and put in inside my matrix
    line = input()  
    for j in range(len(line)) :
        M[i][j] = line[j]

for i in range(height) : # this double loop allows me to actually print the answer
    for j in range(width) :
        if M[i][j] == "0": #are we on a node ?
            res = str(j)+" "+str(i) +' '
            if j != width-1 : #are we on the right edge ?
                if M[i][j+1] == '0': #if not on right edge, does the node have a right neighboor ?
                    res += str(j+1) +' '+str(i)+" "
                    if i != height -1 : #are we on the bottom ?
                        if M[i+1][j] == '0': res += str(j) +' '+str(i+1) # if not, is bottom neighboor a node ?
                        else : res+= '-1 -1'#if no neighbour send -1 -1
                    else : res+= '-1 -1'#if we are bottom, send -1 -1 (no neighbour possible)
                else : #no right neighbour
                    res+= '-1 -1 '
                    if i != height -1 : #check if we at bottom
                        if M[i+1][j] == '0': res += str(j) +' '+str(i+1) # check bottom neighbour
                        else : res+= '-1 -1' #no bottom neighbour send -1 -1
                    else : res+= '-1 -1' #bottom
            else : #we are right edge 
                res+= '-1 -1 '
                if i != height -1 : #bottom ? 
                    if M[i+1][j] == '0': res += str(j) +' '+str(i+1) #bottom neighbour ?
                    else : res+= '-1 -1' #no bottom neighbour
                else : res+= '-1 -1' #edge and bottom == south right corner
            print(res) #display the concatenated solution

since we need all the input to get all the neighbour, i first took input and put it inside a matrix. then i search trough the matrix and put conditions to get the result. but i dont know if i can do it this way. i have 50 complete. Thanks for your feed back

admin edit: code formatting using the </> button

oh shite the editor fricked up my code its unreadable

Copy paste your script, select it and click on </> icon.

Your code is hard to read right now but a general advice:
instead of printing a str, use variables (e.g a, b, c, d) and once you determined their value, print(j, i, a, b, c, d).
It will be much easier to read that way.

Hello there,
I think there is an instability in your tests, at least for clojure runs.
For proofs i have submitted 2 times the same code and failed at different tests.

It happens sometimes in the IDE tests to sometimes, and the Error says that the input was not read, but what’s happening (i bet) is that the process failed to start.

Cheers

Hi. Can you affirm that your code is not the cause of the problem ?

I do not us random numbers if that was your question :smiley:
i did experience it with other projects with Clojure.

I’m not thinking to random numbers, but to timeouts. If your code is too slow, it can barely pass sometime and not at all other times.

I’ll try creating inputs and test-it locally.
Since i have picked up this language 3 days ago it might be it.

However if the program did timed out, the debug (stderr) output should still be printed at least, right ?
I did put an init message as the first thing to be executed, nothing appeared, but still crashed.

I made some puzzles with clojure, and never encountered any problem. It is possible that there’s a problem on the site side, but it’s necessary to be sure that’s not from your code before making a bug report. :wink:

I understand, I’ll come back when i have some numbers on my side.

If you have solved this problem, or any other, using clojure
could you try running the IDE tests ?

If you have failures on already passed tests then you have reproduced what i experienced.
If not, then it’s might be my fault then …

I have a clojure solution on this one, and I get 100% to both tests and validators. ^^

Alright it must be my fault.