Day 3 implementation in Java

main
MiguelMLorente 2023-12-06 21:24:20 +01:00
parent 32f529dc22
commit d32cbd51ec
2 changed files with 115 additions and 0 deletions

View File

@ -0,0 +1,100 @@
package day3;
import io.vavr.Tuple;
import io.vavr.Tuple2;
import io.vavr.collection.List;
import io.vavr.collection.Set;
import util.FileReader;
public class Main {
public static void main(String[] args) {
String inputFilePath = System.getProperty("user.dir") + "\\java\\src\\main\\java\\day3\\input.txt";
FileReader reader = new FileReader(inputFilePath);
List<String> inputData = List.ofAll(reader.readContents());
Set<Position> symbolsPositions = inputData
.zipWithIndex()
.toMap(Tuple2::swap)
.mapValues(String::toCharArray)
.mapValues(List::ofAll)
.mapValues(charArray -> charArray
.zipWithIndex()
.toMap(Tuple2::swap)
.filterValues(character -> '.' != character)
.filterValues(character -> !Character.isDigit(character))
.keySet().toList()
).map((lineIndex, columnIndexes) -> Tuple.of(lineIndex, columnIndexes
.map(columnIndex -> new Position(lineIndex, columnIndex))
)
).values().toSet()
.flatMap(set -> set);
List<List<Position>> numbersPositions = inputData
.zipWithIndex()
.toMap(Tuple2::swap)
.mapValues(String::toCharArray)
.mapValues(List::ofAll)
.mapValues(charArray -> charArray
.zipWithIndex()
.toMap(Tuple2::swap)
.filterValues(Character::isDigit)
.keySet().toList()
)
.mapValues(Main::groupConsecutiveNumbers)
.map((lineIndex, groupedNumbersColumnIndexes) -> Tuple.of(lineIndex, groupedNumbersColumnIndexes
.map(columnIndexes -> columnIndexes.map(columnIndex -> new Position(lineIndex, columnIndex)))
)
).values().toList().flatMap(list -> list);
Integer sumOfValidParts = numbersPositions.filter(positions ->
!positions.filter(numberPosition ->
!symbolsPositions
.filter(symbolPosition -> symbolPosition.isAdjacent(numberPosition))
.isEmpty()
).isEmpty()
).map(positions -> transformDigitsPositionsToNumber(positions, inputData))
.reduce(Integer::sum);
System.out.println(sumOfValidParts);
Integer gearRatiosSum = symbolsPositions.toList()
.filter(position -> inputData.get(position.line).charAt(position.column) == '*')
.map(symbolPosition -> numbersPositions
.filter(numberPositions -> !numberPositions.filter(symbolPosition::isAdjacent).isEmpty())
).filter(adjacentNumberList -> adjacentNumberList.length() == 2)
.map(adjacentNumberPositions -> adjacentNumberPositions.map(positions -> transformDigitsPositionsToNumber(positions, inputData)))
.map(adjacentNumbers -> adjacentNumbers.get(0) * adjacentNumbers.get(1))
.reduce(Integer::sum);
System.out.println(gearRatiosSum);
}
private static Integer transformDigitsPositionsToNumber(List<Position> digitsPositions, List<String> inputData) {
return digitsPositions
.map(position -> inputData
.get(position.line)
.charAt(position.column)
).map(String::valueOf)
.map(Integer::parseInt)
.reduce((accumulator, value) -> 10 * accumulator + value);
}
private static List<List<Integer>> groupConsecutiveNumbers(List<Integer> numbers) {
List<Integer> sortedNumbers = numbers.sorted();
List<List<Integer>> groupedNumbers = List.empty();
List<Integer> temp = List.empty();
temp = temp.append(sortedNumbers.get(0));
for (int index = 0; index < sortedNumbers.size() - 1; index++) {
if (sortedNumbers.get(index + 1) != sortedNumbers.get(index) + 1) {
groupedNumbers = groupedNumbers.append(temp);
temp = List.empty();
}
temp = temp.append(sortedNumbers.get(index + 1));
}
return groupedNumbers.append(temp);
}
}

View File

@ -0,0 +1,15 @@
package day3;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class Position {
Integer line;
Integer column;
public boolean isAdjacent(Position other) {
int columnsDiff = Math.abs(this.column - other.column);
int linesDiff = Math.abs(this.line - other.line);
return columnsDiff <= 1 && linesDiff <= 1;
}
}