Skip to content

Commit

Permalink
feat: solve day 19 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Flashky committed Dec 19, 2024
1 parent 8b10134 commit 6007e26
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
64 changes: 64 additions & 0 deletions src/main/java/com/adventofcode/flashk/day19/LinenLayout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.adventofcode.flashk.day19;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LinenLayout {

private List<String> patterns;
private List<String> designs;

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

public LinenLayout(List<String> inputs) {
patterns = Arrays.stream(inputs.getFirst().replace(StringUtils.SPACE, StringUtils.EMPTY).split(","))
.sorted(new PatternComparator())
.toList();
inputs.removeFirst();
inputs.removeFirst();
designs = inputs;
}

public int solveA() {
int result = 0;
for(String design : designs) {
if(isPossible(design)) {
result++;
}
}
return result;
}

private boolean isPossible(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, isPossible(leftSide));
memo.put(rightSide, isPossible(rightSide));

if(memo.get(leftSide) && memo.get(rightSide)) {
return true;
}
}
}

return false;
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/adventofcode/flashk/day19/PatternComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.adventofcode.flashk.day19;

import java.util.Comparator;

public class PatternComparator implements Comparator<String> {

@Override
public int compare(String o1, String o2) {
if(o1.length() < o2.length()) {
return 1;
}
if(o1.length() > o2.length()) {
return -1;
}
return 0;
}
}
7 changes: 5 additions & 2 deletions src/test/java/com/adventofcode/flashk/day19/Day19Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public void testSolvePart1Sample() {
// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);

assertEquals(0L,0L);
LinenLayout linenLayout = new LinenLayout(inputs);

assertEquals(6, linenLayout.solveA());
}

@Test
Expand All @@ -48,8 +50,9 @@ public void testSolvePart1Input() {

// Read input file
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
LinenLayout linenLayout = new LinenLayout(inputs);

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

}
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/inputs

0 comments on commit 6007e26

Please sign in to comment.