Skip to content

Commit

Permalink
add filter on remaining position iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
samabcde committed Jul 9, 2024
1 parent eb32f47 commit bc308d5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private boolean isCurrentBoardSolvable(Block block) {
private boolean isRemainingBlockPositionsSolvable(BoardFillState cloneBoardFillState,
List<PossiblePositions> remainingBlocksPossiblePositions) {

long weight1BlockCount = getRemainingBlocks().stream().filter(b -> b.getWeight() == 1).count();
boolean hasChange;
do {
hasChange = false;
Expand All @@ -153,6 +154,9 @@ private boolean isRemainingBlockPositionsSolvable(BoardFillState cloneBoardFillS
if (cloneBoardFillState.existCannotFillPoint()) {
return false;
}
if (cloneBoardFillState.countPointCanOnlyFillByWeight1Block() > weight1BlockCount) {
return false;
}
}
}

Expand All @@ -177,6 +181,9 @@ private boolean isRemainingBlockPositionsSolvable(BoardFillState cloneBoardFillS
if (cloneBoardFillState.existCannotFillPoint()) {
return false;
}
if (cloneBoardFillState.countPointCanOnlyFillByWeight1Block() > weight1BlockCount) {
return false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,8 @@ public int getPossiblePositionCount(Block block) {
return possiblePositionCountOfBlocks[block.id];
}

private int[] getIntersectionCountOfBlockPositions() {
return intersectionCountOfBlockPositions;
}

int getIntersectionCount(BlockPosition blockPosition) {
return getIntersectionCountOfBlockPositions()[blockPosition.id];
return intersectionCountOfBlockPositions[blockPosition.id];
}

private boolean isPossible(BlockPosition blockPosition) {
Expand Down Expand Up @@ -126,7 +122,7 @@ public Stream<BlockPosition> placeBlockPosition(BlockPosition blockPosition) {
boolean wasPossible = isPossible(intersectBlockPosition);
this.incrementIntersectionCount(intersectBlockPosition);
// from possible to not possible
if (wasPossible && !isPlaced(intersectBlockPosition.getBlock())) {
if (wasPossible && isNotPlaced(intersectBlockPosition.getBlock())) {
builder.add(intersectBlockPosition);
}
}
Expand All @@ -141,15 +137,15 @@ public Stream<BlockPosition> takeBlockPosition(BlockPosition blockPosition) {
BlockPosition intersectBlockPosition = blockPuzzle.getBlockPositionById(intersectPositionId);
this.decrementIntersectionCount(intersectBlockPosition);
boolean nowPossible = isPossible(intersectBlockPosition);
if (nowPossible && !isPlaced(intersectBlockPosition.getBlock())) {
if (nowPossible && isNotPlaced(intersectBlockPosition.getBlock())) {
builder.add(intersectBlockPosition);
}
}
return Stream.concat(builder.build(), blockPosition.getBlock().getBlockPositions().stream().filter(this::isPossible));
}

private boolean isPlaced(Block block) {
return this.placedBlockIds.contains(block.id);
private boolean isNotPlaced(Block block) {
return !this.placedBlockIds.contains(block.id);
}

// check which position is added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public boolean existCannotFillPoint() {
return emptyPoints.stream().anyMatch(emptyPoint -> !emptyPoint.canFill());
}

// TODO cache this instead of loop through
public long countPointCanOnlyFillByWeight1Block() {
return getEmptyPoints().stream().filter(PointFillState::canOnlyFillByWeight1Block).count();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void testSolve4A() {
"11,11,01", "01,011,111,111"

});
assertPuzzleSolvable(blockPuzzle, 2367);
assertPuzzleSolvable(blockPuzzle, 2325);
}

@Test
Expand Down Expand Up @@ -118,7 +118,7 @@ public void testSolve10a() {
"111,001", "1,111", "01,111", "001,111", "11,011", "011,11", "1,11,1", "01,11,01", "01,11,1",
"1,11,01", "11111", "1,1,1,1,1", "1111,1", "1111,01", "1111,001", "111,101"
});
assertPuzzleSolvable(blockPuzzle, 1875);
assertPuzzleSolvable(blockPuzzle, 1843);
// 2023-05-21 17:28:43 INFO BlockPuzzleSolver - Solved
// 2023-05-21 17:28:43 INFO BlockPuzzleSolver - iterate Count: 1875
// 2023-05-21 17:28:43 INFO BlockPuzzleSolver - Solution:
Expand Down Expand Up @@ -217,7 +217,7 @@ public void testSolve12b() {
"01,111,01,01,111,001,001,111,1"

});
assertPuzzleSolvable(blockPuzzle, 622474);
assertPuzzleSolvable(blockPuzzle, 619230);
}

@Test
Expand Down Expand Up @@ -262,7 +262,7 @@ public void testSolve12d() {
"111,111,111,111,111,0111", "011,111,1,11111,111,111", "111,001,001,111", "01,1111,111111",
"01,111,01,01,111,001,001,111,1"
});
assertPuzzleSolvable(blockPuzzle, 6875);
assertPuzzleSolvable(blockPuzzle, 6873);
}


Expand Down Expand Up @@ -316,10 +316,10 @@ public void testSolve14() {
"111,111,111,111,111,0111", "011,111,1,11111,111,111", "111,001,001,111", "01,1111,111111",
"1111111", "1111111", "1,1,1,1,1,1,1", "1,1,1,1,1,1", "01,111,01,01,111,001,001,111,1"
});
assertPuzzleSolvable(blockPuzzle, 34701447);
// 2023-05-21 21:39:46 INFO BlockPuzzleSolver - Solved
// 2023-05-21 21:39:46 INFO BlockPuzzleSolver - iterate Count: 34701447
// 2023-05-21 21:39:46 INFO BlockPuzzleSolver - Solution:
assertPuzzleSolvable(blockPuzzle, 34649267);
// 2024-07-09 22:38:20 INFO BlockPuzzleSolver - Solved
// 2024-07-09 22:38:20 INFO BlockPuzzleSolver - iterate Count: 34649267
// 2024-07-09 22:38:20 INFO BlockPuzzleSolver - Solution:
// cccccccddddddd
// b222333333jjje
// b22234444oajee
Expand All @@ -334,9 +334,9 @@ public void testSolve14() {
// f111115hhh7770
// f111m555577000
// f111m5555550nn
// 2023-05-21 21:39:46 INFO PerformanceRecorder - Step Complete initialize block puzzle time: 170ms
// 2023-05-21 21:39:46 INFO PerformanceRecorder - Step Solve complete time: 1893456ms
// 2023-05-21 21:39:46 INFO PerformanceRecorder - Step Print solution time: 1ms
// 2024-07-09 22:38:20 INFO PerformanceRecorder - Step Complete initialize block puzzle time: 94ms
// 2024-07-09 22:38:20 INFO PerformanceRecorder - Step Solve complete time: 2154276ms
// 2024-07-09 22:38:20 INFO PerformanceRecorder - Step Print solution time: 4ms
}

@Test
Expand Down

0 comments on commit bc308d5

Please sign in to comment.