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/CodeChronicle.java b/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java new file mode 100644 index 0000000..206f776 --- /dev/null +++ b/src/main/java/com/adventofcode/flashk/day25/CodeChronicle.java @@ -0,0 +1,60 @@ +package com.adventofcode.flashk.day25; + + +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<>(); + + public CodeChronicle(List inputs) { + + 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..1807b49 --- /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 final int[] heights = new int[5]; + + public Key(char[][] keyMap) { + char[][] transposedMap = Array2DUtil.transpose(keyMap); + + 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 new file mode 100644 index 0000000..5a54038 --- /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 final int[] pins = new int[5]; + + public Lock(char[][] lockMap) { + + char[][] transposedMap = Array2DUtil.transpose(lockMap); + + for(int i = 0; i < transposedMap.length; i++) { + String rowValue = new String(transposedMap[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/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) 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); - - } }