Merge branch 'main' of https://git.micosita.es/micosil/adventOfCode_2023
commit
6935399150
|
|
@ -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.
|
||||||
|
|
@ -2,12 +2,25 @@ package day1;
|
||||||
|
|
||||||
import util.FileReader;
|
import util.FileReader;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
// Part 1 one of the problem has this flag set to false, while part 2 has it set to true
|
// 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 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) {
|
public static void main(String[] args) {
|
||||||
String inputFilePath = System.getProperty("user.dir") + "\\java\\src\\main\\java\\day1\\input.txt";
|
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) {
|
private static String replaceSpelledDigits(String string) {
|
||||||
return string
|
StringBuilder newStringBuilder = new StringBuilder(string);
|
||||||
.replaceAll("oneight", "18")
|
SPELLED_NUMBERS.keySet().stream()
|
||||||
.replaceAll("threeight", "38")
|
.filter(string::contains)
|
||||||
.replaceAll("fiveight", "58")
|
.forEach(number -> getIndexesOfAllSubstringOccurrences(string, number)
|
||||||
.replaceAll("nineight", "98")
|
.forEach(index -> newStringBuilder.setCharAt(index, SPELLED_NUMBERS.get(number)))
|
||||||
.replaceAll("twone", "21")
|
);
|
||||||
.replaceAll("eightwo", "82")
|
|
||||||
.replaceAll("eighthree", "83")
|
return newStringBuilder.toString();
|
||||||
.replaceAll("one", "1")
|
}
|
||||||
.replaceAll("two", "2")
|
|
||||||
.replaceAll("three", "3")
|
private static List<Integer> getIndexesOfAllSubstringOccurrences(String string, String substring) {
|
||||||
.replaceAll("four", "4")
|
List<Integer> indexes = new ArrayList<>();
|
||||||
.replaceAll("five", "5")
|
int index = string.indexOf(substring);
|
||||||
.replaceAll("six", "6")
|
while (index >= 0) {
|
||||||
.replaceAll("seven", "7")
|
indexes.add(index);
|
||||||
.replaceAll("eight", "8")
|
index = string.indexOf(substring, index + 1);
|
||||||
.replaceAll("nine", "9");
|
}
|
||||||
|
return indexes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -21,7 +21,7 @@ public class FileReader {
|
||||||
scanner.useDelimiter("\n");
|
scanner.useDelimiter("\n");
|
||||||
List<String> data = new ArrayList<>();
|
List<String> data = new ArrayList<>();
|
||||||
while (scanner.hasNext()) {
|
while (scanner.hasNext()) {
|
||||||
data.add(scanner.next());
|
data.add(scanner.next().replace("\r", ""));
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue