From 04e6728ee881a9f84a1720e632710dc5ef67a8f8 Mon Sep 17 00:00:00 2001 From: Flashk Date: Thu, 19 Dec 2024 08:02:56 +0100 Subject: [PATCH] feat: solve day 19 WIP --- .../flashk/day19/LinenLayout.java | 60 ++++++++++++++++++- .../adventofcode/flashk/day19/Day19Test.java | 7 +-- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/adventofcode/flashk/day19/LinenLayout.java b/src/main/java/com/adventofcode/flashk/day19/LinenLayout.java index 3b05f63..d6059b3 100644 --- a/src/main/java/com/adventofcode/flashk/day19/LinenLayout.java +++ b/src/main/java/com/adventofcode/flashk/day19/LinenLayout.java @@ -13,11 +13,12 @@ public class LinenLayout { private List designs; private Map memo = new HashMap<>(); + private Map memo2 = new HashMap<>(); public LinenLayout(List inputs) { patterns = Arrays.stream(inputs.getFirst().replace(StringUtils.SPACE, StringUtils.EMPTY).split(",")) .sorted(new PatternComparator()) - .toList(); + .toList().reversed(); inputs.removeFirst(); inputs.removeFirst(); designs = inputs; @@ -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)) { @@ -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; + } + } diff --git a/src/test/java/com/adventofcode/flashk/day19/Day19Test.java b/src/test/java/com/adventofcode/flashk/day19/Day19Test.java index 37cfec2..5d09b5b 100644 --- a/src/test/java/com/adventofcode/flashk/day19/Day19Test.java +++ b/src/test/java/com/adventofcode/flashk/day19/Day19Test.java @@ -52,8 +52,7 @@ public void testSolvePart1Input() { List 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()); } @@ -66,8 +65,8 @@ public void testSolvePart2Sample() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); - - assertEquals(0L,0L); + LinenLayout linenLayout = new LinenLayout(inputs); + assertEquals(16L,linenLayout.solveB()); } @Test