Skip to content

Commit

Permalink
feat: solve day 19 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 19, 2024
1 parent 6007e26 commit 04e6728
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
60 changes: 59 additions & 1 deletion src/main/java/com/adventofcode/flashk/day19/LinenLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ public class LinenLayout {
private List<String> designs;

private Map<String,Boolean> memo = new HashMap<>();
private Map<String,Integer> memo2 = new HashMap<>();

public LinenLayout(List<String> inputs) {
patterns = Arrays.stream(inputs.getFirst().replace(StringUtils.SPACE, StringUtils.EMPTY).split(","))
.sorted(new PatternComparator())
.toList();
.toList().reversed();
inputs.removeFirst();
inputs.removeFirst();
designs = inputs;
Expand All @@ -33,6 +34,34 @@ public int solveA() {
return result;
}

public int solveB() {
int result = 0;
for(String design : designs) {
isPossible2(design, design);
}
return 0;
}

/*
private int countPossible(String design) {
if(memo2.containsKey(design)) {
memo2.put(design, memo2.get(design)+1);
}
if(StringUtils.EMPTY.equals(design)) {
return 1;
}
for(String pattern : patterns) {
int index = design.indexOf(pattern);
String leftSide = design.substring(0, index);
String rightSide = design.substring(index+pattern.length());
memo.put(leftSide, isPossible(leftSide));
memo.put(rightSide, isPossible(rightSide));
}
}*/

private boolean isPossible(String design){

if(memo.containsKey(design)) {
Expand Down Expand Up @@ -61,4 +90,33 @@ private boolean isPossible(String design){
return false;
}

private boolean isPossible2(String targetDesign, String design){

if(memo.containsKey(design)) {
return memo.get(design);
}

if(StringUtils.EMPTY.equals(design)) {
return true;
}

for(String pattern : patterns) {
int index = design.indexOf(pattern);
if(index != - 1) {
String leftSide = design.substring(0, index);
String rightSide = design.substring(index+pattern.length());

memo.put(leftSide, isPossible2(targetDesign, leftSide));
memo.put(rightSide, isPossible2(targetDesign, rightSide));

if(memo.get(leftSide) && memo.get(rightSide)) {
int count = memo2.getOrDefault(targetDesign, 0);
memo2.put(targetDesign, count+1);
}
}
}

return false;
}

}
7 changes: 3 additions & 4 deletions src/test/java/com/adventofcode/flashk/day19/Day19Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public void testSolvePart1Input() {
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
LinenLayout linenLayout = new LinenLayout(inputs);

System.out.println("Solution: "+linenLayout.solveA());
assertEquals(0L,0L);
assertEquals(296, linenLayout.solveA());

}

Expand All @@ -66,8 +65,8 @@ public void testSolvePart2Sample() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

assertEquals(0L,0L);
LinenLayout linenLayout = new LinenLayout(inputs);
assertEquals(16L,linenLayout.solveB());
}

@Test
Expand Down

0 comments on commit 04e6728

Please sign in to comment.