Ad
  • Custom User Avatar

    Yep. https://www.bell-labs.com/usr/dmr/www/cman.pdf

    7.11 expression && expression
    The && operator returns 1 if both its operands are non-zero, 0 otherwise. Unlike &, && guarantees left-to-right
    evaluation; moreover the second operand is not evaluated if the first operand is 0.
    The operands need not have the same type, but each must have one of the fundamental types or be a pointer.

    7.12 expression || expression
    The || operator returns 1 if either of its operands is non-zero, and 0 otherwise. Unlike | , || guarantees left-to-right
    evaluation; moreover, the second operand is not evaluated if the value of the first operand is non-zero.
    The operands need not have the same type, but each must have one of the fundamental types or be a pointer.

  • Custom User Avatar

    If p is NULL then it is falsy; and the && operator uses lazy evaluation, if its left operand is false, the expression evaluates to false without actually running the evaluation of the right operand. The same thing is true for the || operator as well, if the left operand is true, it returns true without trying to evaluate the right operand.

  • Custom User Avatar

    Okay thank you for clearing that up. That is super helpful. It looks like I didn't copy-paste from the test script. It's funny how you see things one way until someone says something.

  • Custom User Avatar

    Any live cell with two or three live neighbours lives on to the next generation.
    Any dead cell with exactly three live neighbours becomes a live cell.

    there needs to be 3 live cells for a dead cell to become alive. 2 is not enough

  • Custom User Avatar

    Why does the top right cell not get upgraded to 1? It is has 2 "living" neighbors? I copy-pasted this expected 3x3 matrix directly from the test code. If the actual expected output for this test is as you say than the test code is wrong. Also - I don't update the next generation sequentially, but all in parallel in a new area called local_next, where the values are all determined by the current generation's cell data. I am still unclear why this 3x3 data doesn't grow to 4x4. The problem states that the space is infinite and that the areas outside the given grid are assumed to be 0. Are we not supposed to spawn new live cells in this area? If we are, than why should the area not grow? Specifically the bottom row has 2 1's. This should provide 2 locations - in the 4th row - where the assumed 0s become 1s. Is this not how the game works?

  • Custom User Avatar

    The expected output you've listed is wrong as well. The actual expected output for that test is:

    0 1 0
    0 0 1
    1 1 1
    

    I could be wrong, but it looks like you are progressively changing the input, and that is causing errors as your process each cell. Everything in the universe happens all at once for each generation, what happens in one cell for the next generation shouldn't effect what happens in the next cell for that current generation.

    This gen    Next Gen
    1 0 0       0 1 0   //The cell in [0,0] dies because there is only one living neighbor at [1,1], the cell at [0,1] becomes alive because there are exactly 3 neighbors at [0,0], [1,1], [1,2]. The cell at [0,2]
    0 1 1  -->  0 0 1   //only has two neighbors at [1,1] and [1,2]. notice how the neighbor at [0,0] still counts for [0,1], even though [0,0] will die for the next generation. And the cell at [0,1] will be alive
    1 1 0       1 1 1   //next generation, but it doesn't count as a neighbor for [0,2] this generation. And so on for the rest of the cells in the universe.
    
    
  • Custom User Avatar

    I don't think I am getting ho wthis is supposed to work. My code is taking this input

    1 0 0.

    0 1 1.

    1 1 0.

    And after 1 generation I have a 4x4 output (as some of the surrounding vlaues are brought to life).
    Here is my output after 1 generation

    0 0 1 1.

    1 0 0 1.

    0 1 1 1.

    0 1 1 0.

    However, the test is looking for a 3x3 output here.

    0 1 1.

    0 0 1.

    1 1 1.

    Which is not the complete next generation.
    Am I missing something?

  • Custom User Avatar

    You return an array - a pointer to an array. When it is empty you can return NULL.

  • Custom User Avatar

    I am having trouble with the c version. I pass the 3 tests, and when I "attempt" it either fails the first test or passes 6 tests and then fails with a message that it crashed. I think I may not be returning the right thing for the empty [[]] matrix. I have tried returning 0, NULL, and int *out where out is set to 0. What are you returning for this case?

    EDIT: I found the bug. Returning NULL for this case is fine. My bug was when I was allocating memory for the output vector I was using (cols)x(row) rather than (cols)x(rows), and row was a variable I had set to 0. It was working some of the time, probably overwriting unused memory or something, but then occasionally crashing it.

  • Custom User Avatar

    Wow. I can't even follow this.

  • Custom User Avatar

    This is a good example of the tradeoff between speed/memory with this solution using very little memory. Well done.

  • Custom User Avatar

    After looking at some of the other solutions I see how helpful the queue actually is. I think in my solution I could have skipped the queue and put it strait in the int array. Also I had tried putting the & infront of the node->value, but forgot to derefrerence on the way out so that is where I was really stuck. Thanks for the help!

  • Custom User Avatar

    the Queue elements are pointers (void*) but you are storing integers instead. You need to take the address with & when you enqueue, and dereference with * when you dequeue. see my fork of your solution

  • Custom User Avatar

    c:

    I have submitted my solution, which passes all tests, but I am getting the following warnings.
    I am not sure how to use these external functions without generating these conversion warnings. Any advice?

    solution.c:45:20: warning: incompatible pointer to integer conversion assigning to 'int' from 'void *' [-Wint-conversion]
    sorted_tree[i] = queue_dequeue(queue);
    ^ ~~~~~~~~~~~~~~~~~~~~
    solution.c:76:34: warning: incompatible integer to pointer conversion passing 'const int' to parameter of type 'const void *' [-Wint-conversion]
    queue = queue_enqueue(queue, node->value);
    ^~~~~~~~~~~
    solution.c:16:56: note: passing argument to parameter 'data' here
    extern Queue *queue_enqueue (Queue *queue, const void *data);
    ^
    2 warnings generated.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution