From 99eb052fcbc2b6fadc7449845cf4059a8a79f13e Mon Sep 17 00:00:00 2001 From: Maciej Uniejewski Date: Wed, 3 Dec 2025 07:06:04 +0100 Subject: [PATCH] [year2025day03] Add solution --- AdventOfCodeData | 2 +- .../adventofcode/year2025/day03/Lobby.java | 97 +++++++++++++++++++ .../year2025/day03/LobbyTest.java | 45 +++++++++ 3 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/adventofcode/year2025/day03/Lobby.java create mode 100644 src/test/java/com/example/adventofcode/year2025/day03/LobbyTest.java diff --git a/AdventOfCodeData b/AdventOfCodeData index 62fb2d3..c0f5905 160000 --- a/AdventOfCodeData +++ b/AdventOfCodeData @@ -1 +1 @@ -Subproject commit 62fb2d35f737793254912489151b43d46026f505 +Subproject commit c0f5905d5eec95206736665189eb564c570f9aa9 diff --git a/src/main/java/com/example/adventofcode/year2025/day03/Lobby.java b/src/main/java/com/example/adventofcode/year2025/day03/Lobby.java new file mode 100644 index 0000000..1535c5e --- /dev/null +++ b/src/main/java/com/example/adventofcode/year2025/day03/Lobby.java @@ -0,0 +1,97 @@ +package com.example.adventofcode.year2025.day03; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static com.example.adventofcode.utils.FileUtils.readLines; + +public class Lobby { + private static final String FILENAME = "AdventOfCodeData/2025/day03/input"; + private static final String EXAMPLE_FILENAME = "AdventOfCodeData/2025/day03/example_input"; + + public static void main(String[] args) throws IOException { + System.out.println(calculateTotalOutputJoltage(EXAMPLE_FILENAME)); + System.out.println(calculateTotalOutputJoltage(FILENAME)); + System.out.println(calculateTotalOutputJoltage(EXAMPLE_FILENAME, 2)); + System.out.println(calculateTotalOutputJoltage(FILENAME, 2)); + System.out.println(calculateTotalOutputJoltage(EXAMPLE_FILENAME, 12)); + System.out.println(calculateTotalOutputJoltage(FILENAME, 12)); + } + + public static long calculateTotalOutputJoltage(final String filename) throws IOException { + List lines = readLines(filename); + int sum = 0; + for (String line: lines) { + String[] elements = line.split(""); + + int max = 0; + int maxPosition = 0; + for (int i = 0; i < elements.length-1; i++) { + if (Integer.parseInt(elements[i]) > max) { + max = Integer.parseInt(elements[i]); + maxPosition = i; + } + } + + int secondMax = 0; + for (int i = maxPosition+1; i < elements.length; i++) { + if (Integer.parseInt(elements[i]) > secondMax) { + secondMax = Integer.parseInt(elements[i]); + } + } + + sum += max*10+secondMax; + } + + return sum; + } + + public static long calculateTotalOutputJoltage(final String filename, final int length) throws IOException { + List lines = readLines(filename); + long sum = 0; + + for (String line: lines) { + String[] elements = line.split(""); + + List maxValues = prepareArray(length, 0); + List maxPositions = prepareArray(length + 1, -1); + + for (int i = 0; i < length; i++) { + for (int j = maxPositions.get(i)+1; j < elements.length - length + i + 1; j++) { + if (Integer.parseInt(elements[j]) > maxValues.get(i)) { + maxValues.set(i, Integer.parseInt(elements[j])); + maxPositions.set(i + 1, j); + } + } + } + + sum += parseMaxJoltageNumber(length, maxValues); + } + + return sum; + } + + private static List prepareArray(final int length, final int elements) { + List max = new ArrayList<>(); + for (int i = 0; i < length; i++) { + max.add(elements); + } + return max; + } + + private static long parseMaxJoltageNumber(final int length, final List maxValues) { + long sum = 0; + for (int i = 0; i < length; i++) { + sum += (long) maxValues.get(i) * pow(10, length - i - 1L); + } + return sum; + } + + private static long pow(final long a, final long b) { + if (b == 0) return 1; + if (b == 1) return a; + if (((b & 1) == 0)) return pow(a * a, b / 2); + else return a * pow(a * a, b / 2); + } +} diff --git a/src/test/java/com/example/adventofcode/year2025/day03/LobbyTest.java b/src/test/java/com/example/adventofcode/year2025/day03/LobbyTest.java new file mode 100644 index 0000000..4d07693 --- /dev/null +++ b/src/test/java/com/example/adventofcode/year2025/day03/LobbyTest.java @@ -0,0 +1,45 @@ +package com.example.adventofcode.year2025.day03; + +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 LobbyTest { + + private static Stream filepathsAndExpectedTotalOutputJoltage() { + return Stream.of( + Arguments.of("AdventOfCodeData/2025/day03/example_input", 357), + Arguments.of("AdventOfCodeData/2025/day03/input", 17359) + ); + } + + @ParameterizedTest + @MethodSource("filepathsAndExpectedTotalOutputJoltage") + void testCalculateTotalOutputJoltage(final String filename, + final long expectedTotalOutputJoltage) throws IOException { + assertEquals(expectedTotalOutputJoltage, Lobby.calculateTotalOutputJoltage(filename)); + } + + private static Stream filepathsAndJoltageLengthAndExpectedTotalOutputJoltage() { + return Stream.of( + Arguments.of("AdventOfCodeData/2025/day03/example_input", 2, 357), + Arguments.of("AdventOfCodeData/2025/day03/input", 2, 17359), + Arguments.of("AdventOfCodeData/2025/day03/example_input",12, 3121910778619L), + Arguments.of("AdventOfCodeData/2025/day03/input", 12, 172787336861064L) + ); + } + + @ParameterizedTest + @MethodSource("filepathsAndJoltageLengthAndExpectedTotalOutputJoltage") + void testCalculateTotalOutputJoltageExtended(final String filename, + final int joltageLength, + final long expectedTotalOutputJoltage) throws IOException { + assertEquals(expectedTotalOutputJoltage, Lobby.calculateTotalOutputJoltage(filename, joltageLength)); + + } +} \ No newline at end of file