-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC062021.java
38 lines (28 loc) · 916 Bytes
/
AoC062021.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
package com.adventofcode.aoc2021;
import static com.adventofcode.utils.Utils.itoa;
import static com.adventofcode.utils.Utils.toLongStream;
import java.util.Arrays;
import java.util.stream.Stream;
import com.adventofcode.Solution;
class AoC062021 implements Solution {
@Override
public String solveFirstPart( final Stream<String> input ) {
return solve( input, 80 );
}
@Override
public String solveSecondPart( final Stream<String> input ) {
return solve( input, 256 );
}
private String solve( final Stream<String> input, final int days ) {
final long[] ages = new long[9];
toLongStream( input.findFirst().orElseThrow() ).forEach( age -> ages[age.intValue()]++ );
for ( int i = 0; i < days; i++ ) {
final long spawn = ages[0];
// age decreases
System.arraycopy( ages, 1, ages, 0, 8 );
ages[6] += spawn;
ages[8] = spawn;
}
return itoa( Arrays.stream( ages ).sum() );
}
}