Do not test ambiguous train placement, clean up code

This commit is contained in:
Arne Keller 2020-03-04 14:25:31 +01:00
parent 57708e1b4f
commit 1a049e97cf
4 changed files with 20 additions and 42 deletions

View File

@ -0,0 +1,6 @@
add track (0,0) -> (5,0)
add switch (5,0) -> (5,5),(10,0)
set switch 2 position (10,0)
create engine diesel T3 Emma 1 true true
add train 1 T3-Emma
exit

View File

@ -0,0 +1,5 @@
1
2
OK
T3-Emma
diesel engine T3-Emma added to train 1

View File

@ -108,6 +108,11 @@ class MainTest {
cmpInOut("lgt_input.txt", "lgt_output.txt"); cmpInOut("lgt_input.txt", "lgt_output.txt");
} }
@Test
void newest_ilias_shit() throws IOException {
cmpInOut("newest_ilias_shit_input.txt", "newest_ilias_shit_output.txt");
}
private void cmpInOut(String in, String out) throws IOException { private void cmpInOut(String in, String out) throws IOException {
System.setIn(new ByteArrayInputStream(readFile(in))); System.setIn(new ByteArrayInputStream(readFile(in)));
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();

View File

@ -211,26 +211,7 @@ public class RailwayNetwork {
Vector2D nextPossiblePosition = position.add(direction); Vector2D nextPossiblePosition = position.add(direction);
Rail possibleContainingRail = findRailPotentiallyContaining(nextPossiblePosition); Rail possibleContainingRail = findRailPotentiallyContaining(nextPossiblePosition);
if (possibleContainingRail != null && !possibleContainingRail.contains(position.subtract(direction))) { if (possibleContainingRail != null && !possibleContainingRail.contains(position.subtract(direction))) {
Vector2D positionOnThatRail = possibleContainingRail.move(position, direction, 1); return possibleContainingRail.move(position, direction, 1);
/*
if ((positionOnThatRail == null || positionOnThatRail.equals(position)) && touchingRails.length == 2) {
Vector2D onRailOne = touchingRails[0].move(position, new Vector2D(direction), 1);
Vector2D onRailTwo = touchingRails[1].move(position, new Vector2D(direction), 1);
if ((position.equals(onRailOne) || onRailOne == null) && !position.equals(onRailTwo)) {
// we are moving on rail two
Vector2D newDirection = touchingRails[1].getDirectionFrom(position);
direction.copyFrom(newDirection);
return onRailTwo;
} else if ((position.equals(onRailTwo) || onRailTwo == null) && !position.equals(onRailOne)) {
Vector2D newDirection = touchingRails[0].getDirectionFrom(position);
direction.copyFrom(newDirection);
return onRailOne;
} else {
throw new RuntimeException("wtf");
}
}
*/
return positionOnThatRail;
} }
if (touchingRails.length == 0) { if (touchingRails.length == 0) {
@ -250,30 +231,11 @@ public class RailwayNetwork {
direction.copyFrom(newDirection); direction.copyFrom(newDirection);
return onRailOne; return onRailOne;
} else { } else {
throw new RuntimeException("wtf"); // this case: https://ilias.studium.kit.edu/goto.php?client_id=produktiv&target=frm_996924_139074_534267
} // it will not be tested, but simply refusing to place a train
/* // if there is more than one way to place it is fine
Vector2D nextPosition = position.add(direction);
if (touchingRails[0].contains(nextPosition) || touchingRails[0].connectsTo(nextPosition)) {
return nextPosition;
} else if (touchingRails[1].contains(nextPosition)) {
Vector2D nextDirection = touchingRails[1].getDirectionFrom(position);
direction.setX(nextDirection.getX());
direction.setY(nextDirection.getY());
return nextPosition;
}
Vector2D previousPosition = position.add(direction.negated());
Rail nextRail = touchingRails[0].contains(previousPosition)
|| touchingRails[0].canConnectTo(previousPosition) ? touchingRails[1] : touchingRails[0];
Vector2D nextDirection = nextRail.getDirectionFrom(position);
if (nextDirection != null) {
direction.setX(nextDirection.getX());
direction.setY(nextDirection.getY());
return position.add(direction);
} else {
return null; return null;
} }
*/
} }
/** /**