Skip to content

Commit

Permalink
feat: solve day 19 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 19, 2024
1 parent 63a1576 commit cccb264
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/main/java/com/adventofcode/flashk/day19/LinenLayout2.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LinenLayout2 {
private List<String> patterns;
private List<String> designs;

private Map<String,Boolean> memo = new HashMap<>();
private Map<MemoState,Long> memo = new HashMap<>();

public LinenLayout2(List<String> inputs) {
patterns = Arrays.stream(inputs.getFirst().replace(StringUtils.SPACE, StringUtils.EMPTY).split(","))
Expand All @@ -23,29 +23,41 @@ public LinenLayout2(List<String> inputs) {
designs = inputs;
}

public int solveB() {
int result = 0;
public long solveB() {
long result = 0;
for(String design : designs) {
result += count(design, StringUtils.EMPTY);
memo.clear();
}
return result;
}

private int count(String design, String pattern){
private long count(String design, String pattern){

if(design.equals(pattern)) {
return 1;
return 1L;
}

if(!design.startsWith(pattern)) {
return 0;
}

int count = 0;
long count = 0;
String rightSide = design.replaceFirst(pattern, StringUtils.EMPTY);

for(String newPattern : patterns) {
count += count(rightSide, newPattern);

MemoState newState = new MemoState(rightSide, newPattern);

long partialCount;
if(memo.containsKey(newState)) {
partialCount = memo.get(newState);
} else {
partialCount = count(rightSide, newPattern);
memo.put(new MemoState(rightSide, newPattern), partialCount);
}

count += partialCount;
}

return count;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/adventofcode/flashk/day19/MemoState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.adventofcode.flashk.day19;

public record MemoState(String design, String pattern) {
}
3 changes: 1 addition & 2 deletions src/test/java/com/adventofcode/flashk/day19/Day19Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public void testSolvePart2Input() {
// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
LinenLayout2 linenLayout = new LinenLayout2(inputs);
System.out.println("Solution: "+linenLayout.solveB());
assertEquals(0L,0L);
assertEquals(619970556776002L,linenLayout.solveB());

}

Expand Down

0 comments on commit cccb264

Please sign in to comment.