-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC022023.java
73 lines (59 loc) · 2.17 KB
/
AoC022023.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.adventofcode.aoc2023;
import static com.adventofcode.utils.Utils.atoi;
import static com.adventofcode.utils.Utils.itoa;
import static java.lang.Math.max;
import com.adventofcode.Solution;
import com.google.common.collect.Streams;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
class AoC022023 implements Solution {
private static final Pattern RED_REGEX = Pattern.compile( "(\\d+) red" );
private static final Pattern GREEN_REGEX = Pattern.compile( "(\\d+) green" );
private static final Pattern BLUE_REGEX = Pattern.compile( "(\\d+) blue" );
@Override
public String solveFirstPart(final Stream<String> input) {
return solve( input, (str, index) -> isPossible( str ) ? index + 1 : 0 );
}
@Override
public String solveSecondPart(final Stream<String> input) {
return solve( input, (str, index) -> getPower( str ) );
}
private String solve(final Stream<String> input,
final BiFunction<String, Long, Long> gameToNumber) {
return itoa(
Streams.mapWithIndex( input, gameToNumber::apply ).mapToLong( Long::longValue ).sum() );
}
private static boolean isPossible(final String str) {
for ( String reveal : str.split( ";" ) ) {
final RGB rgb = getRGB( reveal );
if ( !(rgb.red <= 12 && rgb.green <= 13 && rgb.blue <= 14) ) {
return false;
}
}
return true;
}
private static long getPower(final String str) {
long red = 0;
long green = 0;
long blue = 0;
for ( String reveal : str.split( ";" ) ) {
final RGB rgb = getRGB( reveal );
red = max( red, rgb.red );
green = max( green, rgb.green );
blue = max( blue, rgb.blue );
}
return red * green * blue;
}
private static RGB getRGB(final String str) {
return new RGB( getColor( RED_REGEX, str ), getColor( GREEN_REGEX, str ),
getColor( BLUE_REGEX, str ) );
}
private static long getColor(final Pattern colorRegex, final String str) {
final Matcher matcher = colorRegex.matcher( str );
return matcher.find() ? atoi( matcher.group( 1 ) ) : 0;
}
private record RGB(long red, long green, long blue) {
}
}