diff --git a/AdventOfCodeData b/AdventOfCodeData index 2f7dc59..37dcdab 160000 --- a/AdventOfCodeData +++ b/AdventOfCodeData @@ -1 +1 @@ -Subproject commit 2f7dc592366c16a118dbe222ba78dac9fb1689c0 +Subproject commit 37dcdab1758496f98967f2a24a9ca519af8bbc2a diff --git a/src/main/java/com/example/adventofcode/year2025/day01/SecretEntrance.java b/src/main/java/com/example/adventofcode/year2025/day01/SecretEntrance.java new file mode 100644 index 0000000..430b108 --- /dev/null +++ b/src/main/java/com/example/adventofcode/year2025/day01/SecretEntrance.java @@ -0,0 +1,123 @@ +package com.example.adventofcode.year2025.day01; + +import java.io.IOException; +import java.util.List; + +import static com.example.adventofcode.utils.FileUtils.readLines; + +public class SecretEntrance { + private static final String FILENAME = "AdventOfCodeData/2025/day01/input"; + private static final String EXAMPLE_FILENAME = "AdventOfCodeData/2025/day01/example_input"; + + + /** + * 0x434C49434B hex to text is "click" + */ + public static void main(String[] args) throws IOException { + System.out.println(restorePasswordSimulation(EXAMPLE_FILENAME)); + System.out.println(restorePasswordSimulation(FILENAME)); + System.out.println(restorePasswordImproved(EXAMPLE_FILENAME)); + System.out.println(restorePasswordImproved(FILENAME)); + System.out.println(restorePasswordClickMethod(EXAMPLE_FILENAME)); + System.out.println(restorePasswordClickMethod(FILENAME)); + } + + public static long restorePasswordSimulation(final String filename) throws IOException { + List lines = readLines(filename); + + int position = 50; + int count = 0; + for (String line: lines) { + String side = line.substring(0, 1); + int number = Integer.parseInt(line.substring(1)); + + if (side.equals("L")) { + for (int i = 0; i < number; i++) { + if (position > 0) { + position--; + } else { + position = 99; + } + } + } else { + for (int i = 0; i < number; i++) { + if (position < 99) { + position++; + } else { + position = 0; + } + } + } + if (position == 0) { + count++; + } + } + + return count; + } + + public static long restorePasswordImproved(final String filename) throws IOException { + List lines = readLines(filename); + + int position = 50; + int count = 0; + for (String line: lines) { + String side = line.substring(0, 1); + int number = Integer.parseInt(line.substring(1)); + + if (side.equals("L")) { + position -= number; + while (position < 0) { + position += 100; + } + } else { + position += number; + while (position > 99) { + position -= 100; + } + } + + if (position == 0) { + count++; + } + } + + return count; + } + + public static long restorePasswordClickMethod(final String filename) throws IOException { + List lines = readLines(filename); + + int position = 50; + int count = 0; + for (String line: lines) { + String side = line.substring(0, 1); + int number = Integer.parseInt(line.substring(1)); + + if (side.equals("L")) { + + for (int i = 0; i < number; i++) { + if (position > 0) { + position--; + if (position == 0) { + count++; + } + } else { + position = 99; + } + } + } else { + for (int i = 0; i < number; i++) { + if (position < 99) { + position++; + } else { + position = 0; + count++; + } + } + } + } + + return count; + } +} diff --git a/src/main/java/com/example/adventofcode/year2025/day06/TrashCompactor.java b/src/main/java/com/example/adventofcode/year2025/day06/TrashCompactor.java new file mode 100644 index 0000000..d5bb66b --- /dev/null +++ b/src/main/java/com/example/adventofcode/year2025/day06/TrashCompactor.java @@ -0,0 +1,155 @@ +package com.example.adventofcode.year2025.day06; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static com.example.adventofcode.utils.FileUtils.readLines; + +public class TrashCompactor { + private static final String FILENAME = "AdventOfCodeData/2025/day06/input"; + private static final String EXAMPLE_FILENAME = "AdventOfCodeData/2025/day06/example_input"; + + public static void main(String[] args) throws IOException { + System.out.println(solveMathHomework(EXAMPLE_FILENAME)); + System.out.println(solveMathHomework(FILENAME)); + System.out.println(solveMathHomeworkInCephalopodWay(EXAMPLE_FILENAME)); + System.out.println(solveMathHomeworkInCephalopodWay(FILENAME)); + } + + public static long solveMathHomework(final String filename) throws IOException { + List lines = readLines(filename); + List operations = parseOperations(lines); + List> numbers = parseNumbers(lines); + + long result = 0; + for (int i = 0; i < operations.size(); i++) { + int operatorNumber = i; + List nb = numbers.stream().map(list -> list.get(operatorNumber)).toList(); + + if (operations.get(i) == '*') { + result += multiplyNumbers(nb); + } else { + result += sumNumbers(nb); + } + + } + + + return result; + } + + public static long solveMathHomeworkInCephalopodWay(final String filename) throws IOException { + List lines = readLines(filename); + List operations = parseOperations(lines); + List columnLengths = calculateColumnLengths(lines); + + List> numbers = buildNumberRows(lines, columnLengths); + List> numbersInColumns = mapNumbersInCephalopodWay(columnLengths, numbers); + + long result = 0; + for (int i = 0; i < operations.size(); i++) { + if (operations.get(i) == '*') { + result += multiplyNumbers(numbersInColumns.get(i)); + } else { + result += sumNumbers(numbersInColumns.get(i)); + } + } + + return result; + } + + private static List parseOperations(List lines) { + String operationsLine = lines.getLast(); + String cleanedLine = operationsLine.trim().replaceAll(" +", " "); + return new ArrayList<>(Arrays.stream(cleanedLine.split(" ")) + .map(s -> s.charAt(0)) + .toList()); + } + + private static List> parseNumbers(List lines) { + List> numbers = new ArrayList<>(); + for (String line : lines) { + String cleanedLine = line.trim().replaceAll(" +", " "); + if (!cleanedLine.contains("+") && !cleanedLine.contains("*")) { + List numbersRow = new ArrayList<>(); + for (String n : cleanedLine.split(" ")) { + numbersRow.add(Long.parseLong(n)); + } + numbers.add(numbersRow); + } + } + return numbers; + } + + private static List calculateColumnLengths(List lines) { + List columnLengths = new ArrayList<>(); + String operationsLine = lines.getLast(); + int currentLength = 0; + for (int i = 0; i < operationsLine.length(); i++) { + if (operationsLine.charAt(i) == ' ') { + currentLength++; + } else { + if (currentLength > 0) { + columnLengths.add(currentLength); + } + currentLength = 0; + } + } + columnLengths.add(currentLength + 1); + return columnLengths; + } + + private static List> buildNumberRows(List lines, List columnLengths) { + List> numbers = new ArrayList<>(); + for (String line : lines) { + if (!line.contains("+") && !line.contains("*")) { + List numbersRow = new ArrayList<>(); + int prev = 0; + for (int length : columnLengths) { + numbersRow.add(line.substring(prev, prev + length)); + prev += length + 1; + } + numbers.add(numbersRow); + } + } + return numbers; + } + + private static List> mapNumbersInCephalopodWay(List columnLengths, List> numbers) { + List> numbersInColumns = new ArrayList<>(); + for (int i = 0; i < columnLengths.size(); i++) { + List numbersInColumn = new ArrayList<>(); + int columnLength = columnLengths.get(i); + for (int j = 0; j < columnLength; j++) { + String tempNumber = ""; + for (List number : numbers) { + char numberFromRow = number.get(i).charAt(j); + if (numberFromRow != ' ') { + tempNumber += numberFromRow; + } + } + numbersInColumn.add(Long.parseLong(tempNumber)); + } + numbersInColumns.add(numbersInColumn); + } + return numbersInColumns; + } + + private static long multiplyNumbers(List numbersInColumns) { + long product = 1; + for (Long numbersInColumn : numbersInColumns) { + product *= numbersInColumn; + } + return product; + } + + private static long sumNumbers(List numbersInColumns) { + int sum = 0; + for (Long numbersInColumn : numbersInColumns) { + sum += numbersInColumn; + } + return sum; + } +} diff --git a/src/test/java/com/example/adventofcode/year2025/day01/SecretEntranceTest.java b/src/test/java/com/example/adventofcode/year2025/day01/SecretEntranceTest.java new file mode 100644 index 0000000..aee5a63 --- /dev/null +++ b/src/test/java/com/example/adventofcode/year2025/day01/SecretEntranceTest.java @@ -0,0 +1,48 @@ +package com.example.adventofcode.year2025.day01; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class SecretEntranceTest { + + private static Stream filepathsAndExpectedPassword() { + return Stream.of( + Arguments.of("AdventOfCodeData/2025/day01/example_input", 3), + Arguments.of("AdventOfCodeData/2025/day01/input", 1086) + ); + } + + @ParameterizedTest + @MethodSource("filepathsAndExpectedPassword") + void restorePasswordSimulation(final String filename, + final int expectedPassword) throws IOException { + assertEquals(expectedPassword, SecretEntrance.restorePasswordSimulation(filename)); + } + + @ParameterizedTest + @MethodSource("filepathsAndExpectedPassword") + void restorePasswordImproved(final String filename, + final int expectedPassword) throws IOException { + assertEquals(expectedPassword, SecretEntrance.restorePasswordImproved(filename)); + } + + private static Stream filepathsAndExpectedPasswordByClickMethod() { + return Stream.of( + Arguments.of("AdventOfCodeData/2025/day01/example_input", 6), + Arguments.of("AdventOfCodeData/2025/day01/input", 6268) + ); + } + + @ParameterizedTest + @MethodSource("filepathsAndExpectedPasswordByClickMethod") + void restorePasswordClickMethod(final String filename, + final int expectedPassword) throws IOException { + assertEquals(expectedPassword, SecretEntrance.restorePasswordClickMethod(filename)); + } +} \ No newline at end of file diff --git a/src/test/java/com/example/adventofcode/year2025/day06/TrashCompactorTest.java b/src/test/java/com/example/adventofcode/year2025/day06/TrashCompactorTest.java new file mode 100644 index 0000000..ebeb64c --- /dev/null +++ b/src/test/java/com/example/adventofcode/year2025/day06/TrashCompactorTest.java @@ -0,0 +1,41 @@ +package com.example.adventofcode.year2025.day06; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class TrashCompactorTest { + + private static Stream filepathsAndExpectedMathHomeworkResult() { + return Stream.of( + Arguments.of("AdventOfCodeData/2025/day06/example_input", 4277556), + Arguments.of("AdventOfCodeData/2025/day06/input", 6503327062445L) + ); + } + + @ParameterizedTest + @MethodSource("filepathsAndExpectedMathHomeworkResult") + void solveMathHomework(final String filename, + final long expectedMathHomeworkResult) throws IOException { + assertEquals(expectedMathHomeworkResult, TrashCompactor.solveMathHomework(filename)); + } + + private static Stream filepathsAndExpectedMathHomeworkResultInCephalopodWay() { + return Stream.of( + Arguments.of("AdventOfCodeData/2025/day06/example_input", 3263827), + Arguments.of("AdventOfCodeData/2025/day06/input", 9640641878593L) + ); + } + + @ParameterizedTest + @MethodSource("filepathsAndExpectedMathHomeworkResultInCephalopodWay") + void solveMathHomeworkInCephalopodWay(final String filename, + final long expectedMathHomeworkResult) throws IOException { + assertEquals(expectedMathHomeworkResult, TrashCompactor.solveMathHomeworkInCephalopodWay(filename)); + } +} \ No newline at end of file