generated from Flashky/advent-of-code-yyyy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/com/adventofcode/flashk/day19/LinenLayout.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/main/java/com/adventofcode/flashk/day19/PatternComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule inputs
updated
from 656e23 to 2c1a75