some-advent-of-code: 9d5afa0c7045e2d9faec180fe3a59e74480b509a
1:
2: import java.io.IOException;
3: import java.nio.file.*;
4: import java.util.*;
5:
6: public class Exercise210602
7: {
8:
9: public static void main(
10: String args[]
11: ) {
12: final String here = "e210602.m ";
13: if ( args.length < 1 )
14: {
15: throw new RuntimeException( here +"add a filename argument" );
16: }
17: String userSaysFile = args[ 0 ];
18: List<String> fileLines = new LinkedList<>();
19: try
20: {
21: Path where = Paths.get( userSaysFile );
22: fileLines = Files.readAllLines( where );
23: }
24: catch ( IOException | InvalidPathException ie )
25: {
26: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
27: return;
28: }
29: /*
30: - interpretation of spec -
31: */
32: Exercise210602.simulateFish( fileLines );
33: }
34:
35:
36: private static void simulateFish(
37: List<String> fileLines
38: ) {
39: boolean testing = true;
40: final int bornGenInd = 8, postBirthGenInd = 6;
41: long[] generations = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
42: for ( String line : fileLines )
43: {
44: if ( line.isEmpty() )
45: continue;
46: String[] initialPopulation = line.split( "," );
47: for ( int ind = 0; ind < initialPopulation.length; ind++ )
48: {
49: int oneFishAge = Integer.parseInt( initialPopulation[ ind ] );
50: generations[ oneFishAge ] += 1L;
51: }
52: }
53:
54: for ( int days = 256; days > 0; days-- )
55: {
56: long temp, tempForGenZero;
57: temp = -1;
58: tempForGenZero = generations[ 0 ];
59: for ( int ind = 1; ind < generations.length; ind++ )
60: {
61: generations[ ind -1 ] = temp;
62: temp = generations[ ind ];
63: generations[ ind -1 ] = generations[ ind ];
64: }
65: generations[ bornGenInd ] = tempForGenZero;
66: generations[ postBirthGenInd ] += tempForGenZero;
67: }
68:
69: if ( testing )
70: for ( int ind = 0; ind < generations.length; ind++ )
71: System.out.println( "pop "+ ind +" is "+ generations[ ind ] );
72:
73: long finalPopulation = 0;
74: for ( int ind = 0; ind < generations.length; ind++ )
75: finalPopulation += generations[ ind ];
76: System.out.println( "\tpopulation "+ finalPopulation );
77: }
78:
79:
80: }
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
Generated by git2html.