From e867cab8928aa9326cdbb098a2ca2dff4211aa01 Mon Sep 17 00:00:00 2001 From: Maciej Uniejewski Date: Fri, 5 Dec 2025 20:01:04 +0100 Subject: [PATCH] [year2025day01] Solution --- .../year2025/day01/SecretEntrance.java | 123 ++++++++++++++++++ .../year2025/day01/SecretEntranceTest.java | 48 +++++++ 2 files changed, 171 insertions(+) create mode 100644 src/main/java/com/example/adventofcode/year2025/day01/SecretEntrance.java create mode 100644 src/test/java/com/example/adventofcode/year2025/day01/SecretEntranceTest.java 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/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