Author: Nicholas Prado <nmprado@nzen.ws>
Date: Wed Dec 8 03:22:54 UTC 2021
Parent: 923a0a4b542fa0b76ddf59960b23a5547fab2d1c
Log message:
feat 21 06 bucket filling
1: diff --git a/src/java/y2021/Exercise210601.java b/src/java/y2021/Exercise210601.java 2: new file mode 100644 3: index 0000000..d65296f 4: --- /dev/null 5: +++ b/src/java/y2021/Exercise210601.java 6: @@ -0,0 +1,107 @@ 7: + 8: +import java.io.IOException; 9: +import java.nio.file.*; 10: +import java.util.*; 11: + 12: +public class Exercise210601 13: +{ 14: + 15: + public static void main( 16: + String args[] 17: + ) { 18: + final String here = "e210601.m "; 19: + if ( args.length < 1 ) 20: + { 21: + throw new RuntimeException( here +"add a filename argument" ); 22: + } 23: + String userSaysFile = args[ 0 ]; 24: + List<String> fileLines = new LinkedList<>(); 25: + try 26: + { 27: + Path where = Paths.get( userSaysFile ); 28: + fileLines = Files.readAllLines( where ); 29: + } 30: + catch ( IOException | InvalidPathException ie ) 31: + { 32: + System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie ); 33: + return; 34: + } 35: + /* 36: + - interpretation of spec - 37: + */ 38: + Exercise210601.simulateFish( fileLines ); 39: + } 40: + 41: + 42: + private static void simulateFish( 43: + List<String> fileLines 44: + ) { 45: + boolean testing = true; 46: + final int bornGenInd = 8, postBirthGenInd = 6; 47: + int[] generations = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 48: + for ( String line : fileLines ) 49: + { 50: + if ( line.isEmpty() ) 51: + continue; 52: + String[] initialPopulation = line.split( "," ); 53: + for ( int ind = 0; ind < initialPopulation.length; ind++ ) 54: + { 55: + int oneFishAge = Integer.parseInt( initialPopulation[ ind ] ); 56: + generations[ oneFishAge ] += 1; 57: + } 58: + } 59: + 60: + for ( int days = 80; days > 0; days-- ) 61: + { 62: + int temp, tempForGenZero; 63: + temp = -1; 64: + tempForGenZero = generations[ 0 ]; 65: + for ( int ind = 1; ind < generations.length; ind++ ) 66: + { 67: + generations[ ind -1 ] = temp; 68: + temp = generations[ ind ]; 69: + generations[ ind -1 ] = generations[ ind ]; 70: + } 71: + generations[ bornGenInd ] = tempForGenZero; 72: + generations[ postBirthGenInd ] += tempForGenZero; 73: + } 74: + 75: + if ( testing ) 76: + for ( int ind = 0; ind < generations.length; ind++ ) 77: + System.out.println( "pop "+ ind +" is "+ generations[ ind ] ); 78: + 79: + int finalPopulation = 0; 80: + for ( int ind = 0; ind < generations.length; ind++ ) 81: + finalPopulation += generations[ ind ]; 82: + System.out.println( "\tpopulation "+ finalPopulation ); 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: + 108: + 109: + 110: + 111: + 112: + 113: + 114: diff --git a/src/java/y2021/Exercise210602.java b/src/java/y2021/Exercise210602.java 115: new file mode 100644 116: index 0000000..9d5afa0 117: --- /dev/null 118: +++ b/src/java/y2021/Exercise210602.java 119: @@ -0,0 +1,107 @@ 120: + 121: +import java.io.IOException; 122: +import java.nio.file.*; 123: +import java.util.*; 124: + 125: +public class Exercise210602 126: +{ 127: + 128: + public static void main( 129: + String args[] 130: + ) { 131: + final String here = "e210602.m "; 132: + if ( args.length < 1 ) 133: + { 134: + throw new RuntimeException( here +"add a filename argument" ); 135: + } 136: + String userSaysFile = args[ 0 ]; 137: + List<String> fileLines = new LinkedList<>(); 138: + try 139: + { 140: + Path where = Paths.get( userSaysFile ); 141: + fileLines = Files.readAllLines( where ); 142: + } 143: + catch ( IOException | InvalidPathException ie ) 144: + { 145: + System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie ); 146: + return; 147: + } 148: + /* 149: + - interpretation of spec - 150: + */ 151: + Exercise210602.simulateFish( fileLines ); 152: + } 153: + 154: + 155: + private static void simulateFish( 156: + List<String> fileLines 157: + ) { 158: + boolean testing = true; 159: + final int bornGenInd = 8, postBirthGenInd = 6; 160: + long[] generations = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 161: + for ( String line : fileLines ) 162: + { 163: + if ( line.isEmpty() ) 164: + continue; 165: + String[] initialPopulation = line.split( "," ); 166: + for ( int ind = 0; ind < initialPopulation.length; ind++ ) 167: + { 168: + int oneFishAge = Integer.parseInt( initialPopulation[ ind ] ); 169: + generations[ oneFishAge ] += 1L; 170: + } 171: + } 172: + 173: + for ( int days = 256; days > 0; days-- ) 174: + { 175: + long temp, tempForGenZero; 176: + temp = -1; 177: + tempForGenZero = generations[ 0 ]; 178: + for ( int ind = 1; ind < generations.length; ind++ ) 179: + { 180: + generations[ ind -1 ] = temp; 181: + temp = generations[ ind ]; 182: + generations[ ind -1 ] = generations[ ind ]; 183: + } 184: + generations[ bornGenInd ] = tempForGenZero; 185: + generations[ postBirthGenInd ] += tempForGenZero; 186: + } 187: + 188: + if ( testing ) 189: + for ( int ind = 0; ind < generations.length; ind++ ) 190: + System.out.println( "pop "+ ind +" is "+ generations[ ind ] ); 191: + 192: + long finalPopulation = 0; 193: + for ( int ind = 0; ind < generations.length; ind++ ) 194: + finalPopulation += generations[ ind ]; 195: + System.out.println( "\tpopulation "+ finalPopulation ); 196: + } 197: + 198: + 199: +} 200: + 201: + 202: + 203: + 204: + 205: + 206: + 207: + 208: + 209: + 210: + 211: + 212: + 213: + 214: + 215: + 216: + 217: + 218: + 219: + 220: + 221: + 222: + 223: + 224: + 225: + 226: + 227: diff --git a/src/res/y2021/21_06_example.txt b/src/res/y2021/21_06_example.txt 228: new file mode 100644 229: index 0000000..a7af2b1 230: --- /dev/null 231: +++ b/src/res/y2021/21_06_example.txt 232: @@ -0,0 +1 @@ 233: +3,4,3,1,2 234: \ No newline at end of file 235: diff --git a/src/res/y2021/21_06_input.txt b/src/res/y2021/21_06_input.txt 236: new file mode 100644 237: index 0000000..90498da 238: --- /dev/null 239: +++ b/src/res/y2021/21_06_input.txt 240: @@ -0,0 +1 @@ 241: +1,2,5,1,1,4,1,5,5,5,3,4,1,2,2,5,3,5,1,3,4,1,5,2,5,1,4,1,2,2,1,5,1,1,1,2,4,3,4,2,2,4,5,4,1,2,3,5,3,4,1,1,2,2,1,3,3,2,3,2,1,2,2,3,1,1,2,5,1,2,1,1,3,1,1,5,5,4,1,1,5,1,4,3,5,1,3,3,1,1,5,2,1,2,4,4,5,5,4,4,5,4,3,5,5,1,3,5,2,4,1,1,2,2,2,4,1,2,1,5,1,3,1,1,1,2,1,2,2,1,3,3,5,3,4,2,1,5,2,1,4,1,1,5,1,1,5,4,4,1,4,2,3,5,2,5,5,2,2,4,4,1,1,1,4,4,1,3,5,4,2,5,5,4,4,2,2,3,2,1,3,4,1,5,1,4,5,2,4,5,1,3,4,1,4,3,3,1,1,3,2,1,5,5,3,1,1,2,4,5,3,1,1,1,2,5,2,4,5,1,3,2,4,5,5,1,2,3,4,4,1,4,1,1,3,3,5,1,2,5,1,2,5,4,1,1,3,2,1,1,1,3,5,1,3,2,4,3,5,4,1,1,5,3,4,2,3,1,1,4,2,1,2,2,1,1,4,3,1,1,3,5,2,1,3,2,1,1,1,2,1,1,5,1,1,2,5,1,1,4 242: diff --git a/src/res/y2021/21_06_solutions.txt b/src/res/y2021/21_06_solutions.txt 243: new file mode 100644 244: index 0000000..5ebdc66 245: --- /dev/null 246: +++ b/src/res/y2021/21_06_solutions.txt 247: @@ -0,0 +1,6 @@ 248: + 249: +example 1 : 5934 250: +puzzle 1 : 356190 251: + 252: +example 2 : 26984457539 253: +puzzle 2 :