Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AdventOfCodeData
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> lines = readLines(filename);
List<Character> operations = parseOperations(lines);
List<List<Long>> numbers = parseNumbers(lines);

long result = 0;
for (int i = 0; i < operations.size(); i++) {
int operatorNumber = i;
List<Long> 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<String> lines = readLines(filename);
List<Character> operations = parseOperations(lines);
List<Integer> columnLengths = calculateColumnLengths(lines);

List<List<String>> numbers = buildNumberRows(lines, columnLengths);
List<List<Long>> 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<Character> parseOperations(List<String> 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<List<Long>> parseNumbers(List<String> lines) {
List<List<Long>> numbers = new ArrayList<>();
for (String line : lines) {
String cleanedLine = line.trim().replaceAll(" +", " ");
if (!cleanedLine.contains("+") && !cleanedLine.contains("*")) {
List<Long> numbersRow = new ArrayList<>();
for (String n : cleanedLine.split(" ")) {
numbersRow.add(Long.parseLong(n));
}
numbers.add(numbersRow);
}
}
return numbers;
}

private static List<Integer> calculateColumnLengths(List<String> lines) {
List<Integer> 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<List<String>> buildNumberRows(List<String> lines, List<Integer> columnLengths) {
List<List<String>> numbers = new ArrayList<>();
for (String line : lines) {
if (!line.contains("+") && !line.contains("*")) {
List<String> 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<List<Long>> mapNumbersInCephalopodWay(List<Integer> columnLengths, List<List<String>> numbers) {
List<List<Long>> numbersInColumns = new ArrayList<>();
for (int i = 0; i < columnLengths.size(); i++) {
List<Long> numbersInColumn = new ArrayList<>();
int columnLength = columnLengths.get(i);
for (int j = 0; j < columnLength; j++) {
String tempNumber = "";
for (List<String> 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<Long> numbersInColumns) {
long product = 1;
for (Long numbersInColumn : numbersInColumns) {
product *= numbersInColumn;
}
return product;
}

private static long sumNumbers(List<Long> numbersInColumns) {
int sum = 0;
for (Long numbersInColumn : numbersInColumns) {
sum += numbersInColumn;
}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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<Arguments> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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<Arguments> 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<Arguments> 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));
}
}
Loading