I declare CurrentTX and CurrentTY int variables before the while statement. I update those variables within each if statement. I have 8 if statements, one for each cardinal direction. I lose on the third challenge when Thor drops off the bottom of the map trying to go SW even though my code for going SW isn’t met because thor’s position is (18,17) and the Light’s position is (0,17) and thus CurrentTY - lightY IS NOT < 0.
It would be a lot less frustrating if you put debug statements in your program to understand what happens exactly. The stderr is made for this and to know the value of your variables and in which “if” statements the code flows would help you a lot.
Note: {∅, N, S} x {∅, E, W} = 9 possibilities. If you have 8 “if”, it won’t be enough in general.
Aries, yes I ended up solving the issue with debug statements. It ended up being an inversion of the updater code for the y position. For a puzzle that was supposed to teach conditionals, they really throw you for a loop with their decision to position 0,0 at the top left instead of bottom left.
8 Conditionals was absolutely enough though. One for each possible direction. I guess theoretically you could have one for “don’t move” but its not really necessary since none of the 8 conditionals will be met in that situation and so none of their code will fire. I mean, the provided solution in the puzzle only used 4 conditionals.
if initial_tx >= 1 :
if(initial_ty <= 1):
if (light_x < initial_tx and light_y < initial_ty):
print(“NW”)
if (initial_tx <=39):
if (light_x > initial_tx and light_y < initial_ty):
print(“NE”)
if (initial_ty >= 17):
if (light_x < initial_tx and light_y > initial_ty):
print(“SW”)
if (light_x < initial_tx):
print(“W”)
if (initial_tx <= 39):
if (initial_ty >= 17):
if (light_x > initial_tx and light_y > initial_ty):
print(“SE”)
if (light_x > initial_tx):
print(“E”)
if(initial_ty >= 1):
if (light_y < initial_ty):
print(“N”)
if (initial_ty <= 17):
if(light_y > initial_ty):
print(“S”)
this doesn’t work in the browser but works in the official IDE ?!?!?
If you mean visible testcases and submit with “browser” and “official IDE”: testcases differ to prevent hardcoding (which you’ve done).
A solution shall pass every possible input, not just the ones you see.
Hello, i’m stuck on the angle problem. My thor keeps going SW despite a condition making it impossible when thor reaches a coordinate close to the light in the vertical direction:
the conditions are ignored even with the overkill 3rd condition. Thor reaches 18 while still going SW (same statement executed instead of falling back on another one)
Just to be extra safe i made sure the expected fall back was the next command:
Yes. The coordinates are updated as follow:
$thorY = $thorY + 1;
$thorX = $thorX - 1;
so in theory, if thorY < 17, ThorY cease updating beacause the following case is
elseif (($thorX > $lightX) && ($thorY >= $lightY)) {
echo (“W\n”);
$thorX = $thorX - 1;
So whevener Thor reaches the edge of the map hes supposed to quit going in the SW direction.
However on execution Thor only goes SW 18 times and dies.
The loop works fine for single direction cases.
The thor variables are duplicates of the initial variables, updated on each loop iteration.
If that part of the code is correct then the problem must be somewhere else… sounds obvious but it bears mentioning. Updating variables, nesting braces, etc are common spots to check. Try inserting some debug comments to see what your variables are doing and what parts of the code are being used.
When I try to submit, I’m stuck on the “Loading…” screen while it computes my score, ant it never submits…any idea why this is happening? My code passes all the tests.
Help please, I am stuck here. I tried the 8-if-conditions solution, but for some reason, the code below only works for cases 1 & 2 but not 3 & 4. I don’t know what went wrong!
char direction;
if (thorX < lightX && thorY < lightY) {
direction = 'SE';
thorX++; thorY++;
}
if (thorX < lightX && thorY > lightY) {
direction = 'NE';
thorX++; thorY--;
}
if (thorX > lightX && thorY < lightY) {
direction = 'SW';
thorX--; thorY++;
}
if (thorX > lightX && thorY > lightY) {
direction = 'NW';
thorX--; thorY--;
}
if (thorX == lightX && thorY < lightY) {
direction = 'S';
thorY++;
}
if (thorX == lightX && thorY > lightY) {
direction = 'N';
thorY--;
}
if (thorX < lightX && thorY == lightY) {
direction = 'E';
thorX++;
}
if (thorX > lightX && thorY == lightY) {
direction = 'W';
thorX--;
}
printf("%c\n", direction);