From d8d19d3ca190c73aaf406442e5f55ec58f61c2e9 Mon Sep 17 00:00:00 2001 From: Flashk Date: Wed, 25 Dec 2024 07:23:15 +0100 Subject: [PATCH 1/5] feat: solve day 25 puzzle --- .../flashk/day25/CodeChronicle.java | 64 +++++++++++++++++++ .../com/adventofcode/flashk/day25/Key.java | 22 +++++++ .../com/adventofcode/flashk/day25/Lock.java | 36 +++++++++++ .../adventofcode/flashk/day25/Day25Test.java | 42 +++--------- src/test/resources/inputs | 2 +- 5 files changed, 131 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java create mode 100644 src/main/java/com/adventofcode/flashk/day25/Key.java create mode 100644 src/main/java/com/adventofcode/flashk/day25/Lock.java diff --git a/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java b/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java new file mode 100644 index 0000000..875cd24 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java @@ -0,0 +1,64 @@ +package com.adventofcode.flashk.day25; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class CodeChronicle { + + private Set locks = new HashSet<>(); + private Set keys = new HashSet<>(); + //private List locks = new ArrayList<>(); + //private List keys = new ArrayList<>(); + + public CodeChronicle(List inputs) { + // Recorre la lista cogiendo elementos de 7 en 7, los convierte a char[][] y crea un lock o una llave + + int i = 0; + char[][] map = new char[7][5]; + boolean isLock = false; + + + for(String row : inputs) { + + if(i == 0) { + isLock = "#####".equals(row); + } + + if(i == 7) { + + if(isLock) { + locks.add(new Lock(map)); + } else { + keys.add(new Key(map)); + } + + map = new char[7][5]; + i = 0; + + } else { + map[i++] = row.toCharArray(); + } + + } + + if(isLock) { + locks.add(new Lock(map)); + } else { + keys.add(new Key(map)); + } + } + + public long solveA() { + long count = 0; + for(Lock lock : locks) { + for(Key key :keys) { + if(lock.canFit(key)) { + count++; + } + } + } + return count; + } +} diff --git a/src/main/java/com/adventofcode/flashk/day25/Key.java b/src/main/java/com/adventofcode/flashk/day25/Key.java new file mode 100644 index 0000000..bd7c705 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day25/Key.java @@ -0,0 +1,22 @@ +package com.adventofcode.flashk.day25; + +import com.adventofcode.flashk.common.Array2DUtil; +import lombok.Getter; +import org.apache.commons.lang3.StringUtils; + +public class Key { + + @Getter + private int[] heights = new int[5]; + + public Key(char[][] keyMap) { + char[][] trasposedMap = Array2DUtil.transpose(keyMap); + + for(int i = 0; i < trasposedMap.length; i++) { + String rowValue = new String(trasposedMap[i]); + heights[i] = StringUtils.countMatches(rowValue, "#")-1; + } + } + + +} diff --git a/src/main/java/com/adventofcode/flashk/day25/Lock.java b/src/main/java/com/adventofcode/flashk/day25/Lock.java new file mode 100644 index 0000000..73e1d6f --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day25/Lock.java @@ -0,0 +1,36 @@ +package com.adventofcode.flashk.day25; + +import com.adventofcode.flashk.common.Array2DUtil; +import org.apache.commons.lang3.StringUtils; + +public class Lock { + + private int[] pins = new int[5]; + + public Lock(char[][] lockMap) { + + char[][] trasposedMap = Array2DUtil.transpose(lockMap); + + for(int i = 0; i < trasposedMap.length; i++) { + String rowValue = new String(trasposedMap[i]); + pins[i] = StringUtils.countMatches(rowValue, "#")-1; + } + } + + public boolean canFit(Key key) { + + boolean fit = true; + int[] heights = key.getHeights(); + int i = 0; + + while(fit && i < 5) { + int spaceLeft = 5 - pins[i]; + if(spaceLeft < heights[i]){ + fit = false; + } + i++; + } + + return fit; + } +} diff --git a/src/test/java/com/adventofcode/flashk/day25/Day25Test.java b/src/test/java/com/adventofcode/flashk/day25/Day25Test.java index cb09660..87a693b 100644 --- a/src/test/java/com/adventofcode/flashk/day25/Day25Test.java +++ b/src/test/java/com/adventofcode/flashk/day25/Day25Test.java @@ -14,15 +14,13 @@ import com.adventofcode.flashk.common.test.constants.TestFilename; import com.adventofcode.flashk.common.test.constants.TestFolder; import com.adventofcode.flashk.common.test.constants.TestTag; -import com.adventofcode.flashk.common.test.utils.PuzzleTest; import com.adventofcode.flashk.common.test.utils.Input; import static org.junit.jupiter.api.Assertions.assertEquals; @DisplayName(TestDisplayName.DAY_25) @TestMethodOrder(OrderAnnotation.class) -@Disabled // TODO Remove comment when implemented -public class Day25Test extends PuzzleTest { +class Day25Test { private static final String INPUT_FOLDER = TestFolder.DAY_25; @@ -31,12 +29,14 @@ public class Day25Test extends PuzzleTest { @Tag(TestTag.PART_1) @Tag(TestTag.SAMPLE) @DisplayName(TestDisplayName.PART_1_SAMPLE) - public void testSolvePart1Sample() { + void part1SampleTest() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); - assertEquals(0L,0L); + CodeChronicle codeChronicle = new CodeChronicle(inputs); + + assertEquals(3L, codeChronicle.solveA()); } @Test @@ -44,42 +44,16 @@ public void testSolvePart1Sample() { @Tag(TestTag.PART_1) @Tag(TestTag.INPUT) @DisplayName(TestDisplayName.PART_1_INPUT) - public void testSolvePart1Input() { + void part1InputTest() { // Read input file List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - System.out.println("Solution: "); - assertEquals(0L,0L); - - } - - @Test - @Order(3) - @Tag(TestTag.PART_2) - @Tag(TestTag.SAMPLE) - @DisplayName(TestDisplayName.PART_2_SAMPLE) - public void testSolvePart2Sample() { + CodeChronicle codeChronicle = new CodeChronicle(inputs); - // Read input file - List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE); + assertEquals(3320L,codeChronicle.solveA()); - assertEquals(0L,0L); } - @Test - @Order(4) - @Tag(TestTag.PART_2) - @Tag(TestTag.INPUT) - @DisplayName(TestDisplayName.PART_2_INPUT) - public void testSolvePart2Input() { - - // Read input file - List inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE); - - System.out.println("Solution: "); - assertEquals(0L,0L); - - } } diff --git a/src/test/resources/inputs b/src/test/resources/inputs index 9242638..7c55ae9 160000 --- a/src/test/resources/inputs +++ b/src/test/resources/inputs @@ -1 +1 @@ -Subproject commit 9242638852acb165f6f55fc53533e2cb02232ac0 +Subproject commit 7c55ae945c48adc028e95455cea57536d1eb08c2 From ec7f22f36cae0779f46bf4fae99dd4c0832758fc Mon Sep 17 00:00:00 2001 From: Flashk Date: Wed, 25 Dec 2024 07:25:13 +0100 Subject: [PATCH 2/5] refactor: clean day 25 code --- .../java/com/adventofcode/flashk/day25/CodeChronicle.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java b/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java index 875cd24..206f776 100644 --- a/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java +++ b/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java @@ -1,6 +1,6 @@ package com.adventofcode.flashk.day25; -import java.util.ArrayList; + import java.util.HashSet; import java.util.List; import java.util.Set; @@ -9,17 +9,13 @@ public class CodeChronicle { private Set locks = new HashSet<>(); private Set keys = new HashSet<>(); - //private List locks = new ArrayList<>(); - //private List keys = new ArrayList<>(); public CodeChronicle(List inputs) { - // Recorre la lista cogiendo elementos de 7 en 7, los convierte a char[][] y crea un lock o una llave int i = 0; char[][] map = new char[7][5]; boolean isLock = false; - for(String row : inputs) { if(i == 0) { From 899da331b44701dea1b1bf8152dfe8454cc752fe Mon Sep 17 00:00:00 2001 From: Flashk Date: Wed, 25 Dec 2024 07:26:24 +0100 Subject: [PATCH 3/5] docs: update READMEs --- README.md | 2 +- src/main/java/com/adventofcode/flashk/day25/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 966f6c1..66800fa 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ - [Day 22 - Monkey Market](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day22) - [Day 23 - LAN Party](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day23) - [Day 24 - Crossed Wires](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day24) -- [Day 25](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day25) +- [Day 25 - Code Chronicle](https://github.com/Flashky/advent-of-code-2024/tree/master/src/main/java/com/adventofcode/flashk/day25) ## Cloning this repository diff --git a/src/main/java/com/adventofcode/flashk/day25/README.md b/src/main/java/com/adventofcode/flashk/day25/README.md index 21ec494..ffa865a 100644 --- a/src/main/java/com/adventofcode/flashk/day25/README.md +++ b/src/main/java/com/adventofcode/flashk/day25/README.md @@ -1,3 +1,3 @@ -# Day 25: +# Day 25: Code Chronicle [https://adventofcode.com/2024/day/25](https://adventofcode.com/2024/day/25) From 3a1dac05b86fd013b2e6ed9d8c4c4d96ebf18d2d Mon Sep 17 00:00:00 2001 From: Flashk Date: Wed, 25 Dec 2024 07:28:12 +0100 Subject: [PATCH 4/5] feat: minor adjustments to day 25 --- src/main/java/com/adventofcode/flashk/day25/Key.java | 8 ++++---- src/main/java/com/adventofcode/flashk/day25/Lock.java | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/adventofcode/flashk/day25/Key.java b/src/main/java/com/adventofcode/flashk/day25/Key.java index bd7c705..1807b49 100644 --- a/src/main/java/com/adventofcode/flashk/day25/Key.java +++ b/src/main/java/com/adventofcode/flashk/day25/Key.java @@ -7,13 +7,13 @@ public class Key { @Getter - private int[] heights = new int[5]; + private final int[] heights = new int[5]; public Key(char[][] keyMap) { - char[][] trasposedMap = Array2DUtil.transpose(keyMap); + char[][] transposedMap = Array2DUtil.transpose(keyMap); - for(int i = 0; i < trasposedMap.length; i++) { - String rowValue = new String(trasposedMap[i]); + for(int i = 0; i < transposedMap.length; i++) { + String rowValue = new String(transposedMap[i]); heights[i] = StringUtils.countMatches(rowValue, "#")-1; } } diff --git a/src/main/java/com/adventofcode/flashk/day25/Lock.java b/src/main/java/com/adventofcode/flashk/day25/Lock.java index 73e1d6f..5a54038 100644 --- a/src/main/java/com/adventofcode/flashk/day25/Lock.java +++ b/src/main/java/com/adventofcode/flashk/day25/Lock.java @@ -5,14 +5,14 @@ public class Lock { - private int[] pins = new int[5]; + private final int[] pins = new int[5]; public Lock(char[][] lockMap) { - char[][] trasposedMap = Array2DUtil.transpose(lockMap); + char[][] transposedMap = Array2DUtil.transpose(lockMap); - for(int i = 0; i < trasposedMap.length; i++) { - String rowValue = new String(trasposedMap[i]); + for(int i = 0; i < transposedMap.length; i++) { + String rowValue = new String(transposedMap[i]); pins[i] = StringUtils.countMatches(rowValue, "#")-1; } } From fa1f9779534e4c2cb906c61237f73e859da94227 Mon Sep 17 00:00:00 2001 From: Flashk Date: Thu, 26 Dec 2024 12:56:52 +0100 Subject: [PATCH 5/5] feat: add day 24 input changes --- src/test/resources/inputs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/resources/inputs b/src/test/resources/inputs index 7c55ae9..d934f3d 160000 --- a/src/test/resources/inputs +++ b/src/test/resources/inputs @@ -1 +1 @@ -Subproject commit 7c55ae945c48adc028e95455cea57536d1eb08c2 +Subproject commit d934f3d88a796442094337e32ae6b9a53b1a30de