Skip to content

Instantly share code, notes, and snippets.

@nickforce
Last active December 2, 2023 21:30
Show Gist options
  • Save nickforce/7ee51833a62377268f7eb14eb1acfbf2 to your computer and use it in GitHub Desktop.
Save nickforce/7ee51833a62377268f7eb14eb1acfbf2 to your computer and use it in GitHub Desktop.
day02-part2
public with sharing class day02 {
public static Integer part2(List<String> puzzleInputLines) {
Integer total = 0;
for(String line : puzzleInputLines) {
Map<String, Integer> maxGameCubesByColor = new Map<String, Integer>();
Integer gameNumber = Integer.valueOf(line.substring(line.indexOf(' ') + 1, line.indexOf(':')));
String gameRounds = line.substring(line.indexOf(':') + 1, line.length());
for(String round : gameRounds.split(';')) {
for(String score : round.split(',')) {
String numberColor = score.trim();
Integer cubeNumber = Integer.valueOf(numberColor.substring(0,numberColor.indexOf(' ')));
String cubeColor = numberColor.substring(numberColor.indexOf(' ') + 1, numberColor.length());
if(maxGameCubesByColor.get(cubeColor) == null) {
maxGameCubesByColor.put(cubeColor, cubeNumber);
}
// if new highest cube number, put in map
if(maxGameCubesByColor.get(cubeColor) < cubeNumber) {
maxGameCubesByColor.put(cubeColor, cubeNumber);
}
}
}
total += powerOfMaxCubes(maxGameCubesByColor);
}
return total;
}
private static Integer powerOfMaxCubes(Map<String, Integer> maxGameCubesByColor) {
Integer powerResult = maxGameCubesByColor.values()[0] * maxGameCubesByColor.values()[1] * maxGameCubesByColor.values()[2];
return powerResult;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment