Jaime9070 2023-12-03 17:22:02 +01:00
commit 6935399150
6 changed files with 156 additions and 18 deletions

7
java/README.md Normal file
View File

@ -0,0 +1,7 @@
### How to run?
You need to have your JDK installed. I'll be using JDK-23.
Run from IDE directly, as JAR files have not yet been created.
Different problems are in different folders. Generally a variable contains the value that controlls whether the first sub-problem is executed or the second.

View File

@ -2,12 +2,25 @@ package day1;
import util.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
// Part 1 one of the problem has this flag set to false, while part 2 has it set to true
private static final boolean IS_NUMBER_SPELLING_ENABLED = true;
private static final HashMap<String, Character> SPELLED_NUMBERS = new HashMap<String, Character>() {{
put("one", '1');
put("two", '2');
put("three", '3');
put("four", '4');
put("five", '5');
put("six", '6');
put("seven", '7');
put("eight", '8');
put("nine", '9');
}};
public static void main(String[] args) {
String inputFilePath = System.getProperty("user.dir") + "\\java\\src\\main\\java\\day1\\input.txt";
@ -36,23 +49,24 @@ public class Main {
}
private static String replaceSpelledDigits(String string) {
return string
.replaceAll("oneight", "18")
.replaceAll("threeight", "38")
.replaceAll("fiveight", "58")
.replaceAll("nineight", "98")
.replaceAll("twone", "21")
.replaceAll("eightwo", "82")
.replaceAll("eighthree", "83")
.replaceAll("one", "1")
.replaceAll("two", "2")
.replaceAll("three", "3")
.replaceAll("four", "4")
.replaceAll("five", "5")
.replaceAll("six", "6")
.replaceAll("seven", "7")
.replaceAll("eight", "8")
.replaceAll("nine", "9");
StringBuilder newStringBuilder = new StringBuilder(string);
SPELLED_NUMBERS.keySet().stream()
.filter(string::contains)
.forEach(number -> getIndexesOfAllSubstringOccurrences(string, number)
.forEach(index -> newStringBuilder.setCharAt(index, SPELLED_NUMBERS.get(number)))
);
return newStringBuilder.toString();
}
private static List<Integer> getIndexesOfAllSubstringOccurrences(String string, String substring) {
List<Integer> indexes = new ArrayList<>();
int index = string.indexOf(substring);
while (index >= 0) {
indexes.add(index);
index = string.indexOf(substring, index + 1);
}
return indexes;
}
}

View File

@ -0,0 +1,29 @@
package day2;
import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Getter
public class Game {
private static final String GAME_ID_PREFIX = "Game ";
private static final String GAME_ID_SEPARATOR = ":";
private static final String ROUNDS_SEPARATOR = ";";
private final Integer id;
private final List<Round> rounds;
public Game(String gameRepresentation) {
String[] splitGameRepresentation = gameRepresentation.split(GAME_ID_SEPARATOR, 2);
this.id = Integer.parseInt(splitGameRepresentation[0].replace(GAME_ID_PREFIX, ""));
this.rounds = Arrays.stream(splitGameRepresentation[1].split(ROUNDS_SEPARATOR))
.map(Round::new)
.collect(Collectors.toList());
}
}

View File

@ -0,0 +1,43 @@
package day2;
import util.FileReader;
import java.util.List;
public class Main {
private static final Round ROUND_MAXIMUM_THRESHOLD = new Round(12, 14, 13);
public static void main(String[] args) {
String inputFilePath = System.getProperty("user.dir") + "\\java\\src\\main\\java\\day2\\input.txt";
FileReader reader = new FileReader(inputFilePath);
List<String> inputData = reader.readContents();
Integer validGamesCount = inputData.stream()
.map(Game::new)
.filter(game -> {
Round round = game.getRounds().stream()
.reduce(Round::mergePrevailMaximum)
.orElseThrow(RuntimeException::new);
return round.getRedCount() <= ROUND_MAXIMUM_THRESHOLD.getRedCount()
&& round.getBlueCount() <= ROUND_MAXIMUM_THRESHOLD.getBlueCount()
&& round.getGreenCount() <= ROUND_MAXIMUM_THRESHOLD.getGreenCount();
}
).map(Game::getId)
.reduce(Integer::sum)
.orElse(0);
System.out.println(validGamesCount);
Integer cubesPowerCount = inputData.stream()
.map(Game::new)
.map(Game::getRounds)
.map(rounds -> rounds.stream()
.reduce(Round::mergePrevailMaximum)
.orElseThrow(RuntimeException::new))
.map(round -> round.getGreenCount() * round.getRedCount() * round.getBlueCount())
.reduce(Integer::sum)
.orElse(0);
System.out.println(cubesPowerCount);
}
}

View File

@ -0,0 +1,45 @@
package day2;
import lombok.Getter;
import java.util.Arrays;
@Getter
public class Round {
private static final String ROUND_COLORS_SEPARATOR = ",";
private static final String RED_SUFFIX = "red";
private static final String BLUE_SUFFIX = "blue";
private static final String GREED_SUFFIX = "green";
private Integer redCount = 0;
private Integer blueCount = 0;
private Integer greenCount = 0;
public Round(Integer redCount, Integer blueCount, Integer greenCount) {
this.redCount = redCount;
this.blueCount = blueCount;
this.greenCount = greenCount;
}
public Round(String roundRepresentation) {
Arrays.stream(roundRepresentation.split(ROUND_COLORS_SEPARATOR, 3))
.map(colorRepresentation -> colorRepresentation.replaceAll(" ", ""))
.forEach(colorRepresentation -> {
if (colorRepresentation.contains(RED_SUFFIX)) {
this.redCount = Integer.parseInt(colorRepresentation.replace(RED_SUFFIX, ""));
} else if (colorRepresentation.contains(BLUE_SUFFIX)) {
this.blueCount = Integer.parseInt(colorRepresentation.replace(BLUE_SUFFIX, ""));
} else if (colorRepresentation.contains(GREED_SUFFIX)) {
this.greenCount = Integer.parseInt(colorRepresentation.replace(GREED_SUFFIX, ""));
}
});
}
public Round mergePrevailMaximum(Round otherRound) {
return new Round(
Math.max(this.redCount, otherRound.redCount),
Math.max(this.blueCount, otherRound.blueCount),
Math.max(this.greenCount, otherRound.greenCount)
);
}
}

View File

@ -21,7 +21,7 @@ public class FileReader {
scanner.useDelimiter("\n");
List<String> data = new ArrayList<>();
while (scanner.hasNext()) {
data.add(scanner.next());
data.add(scanner.next().replace("\r", ""));
}
return data;
} catch (Exception e) {