Author: Nicholas Prado <nmprado@nzen.ws>
Date: Tue Dec 14 06:52:52 UTC 2021
Parent: 4394c5ea8b80a4a315c1f737c06c33ec3d7000b4
Log message:
feat 21 08.2 solve constraints
1: diff --git a/src/java/y2021/Exercise210802.java b/src/java/y2021/Exercise210802.java 2: new file mode 100644 3: index 0000000..6b1037d 4: --- /dev/null 5: +++ b/src/java/y2021/Exercise210802.java 6: @@ -0,0 +1,355 @@ 7: + 8: +import java.io.IOException; 9: +import java.nio.file.*; 10: +import java.util.*; 11: + 12: +public class Exercise210802 13: +{ 14: + 15: + public static void main( 16: + String args[] 17: + ) { 18: + final String here = "e210802.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 -52 37: + */ 38: + new Exercise210802().countSegments( fileLines ); 39: + } 40: + 41: + 42: + private void countSegments( 43: + List<String> fileLines 44: + ) { 45: + int outputs = 0; 46: + for ( String line : fileLines ) 47: + { 48: + if ( line.isEmpty() ) 49: + continue; 50: + SegmentSolver expertSystem = new SegmentSolver( line ); 51: + outputs += expertSystem.shownValue(); 52: + } 53: + System.out.println( "\tanswer is "+ outputs ); 54: + } 55: + 56: + 57: + class SegmentSolver 58: + { 59: + int value = -1; 60: + 61: + String notSet = "-"; 62: + String centerTop = notSet; 63: + String centerCenter = notSet; 64: + String centerBottom = notSet; 65: + String leftTop = notSet; 66: + String rightTop = notSet; 67: + String leftBottom = notSet; 68: + String rightBottom = notSet; 69: + 70: + String segment0 = ""; 71: + String segment1 = ""; 72: + String segment2 = ""; 73: + String segment3 = ""; 74: + String segment4 = ""; 75: + String segment5 = ""; 76: + String segment6 = ""; 77: + String segment7 = ""; 78: + String segment8 = ""; 79: + String segment9 = ""; 80: + 81: + String[] letters = { "a", "b", "c", "d", "e", "f", "g" }; 82: + 83: + Map<String, List<String>> char_inputs = new HashMap<>(); 84: + Map<String, List<String>> missingChar_inputs = new HashMap<>(); 85: + Map<Integer, List<String>> length_inputs = new HashMap<>(); 86: + 87: + List<String> inputsToResolve = new ArrayList<>( 10 ); 88: + List<String> termsToResolve = new ArrayList<>( 4 ); 89: + 90: + 91: + SegmentSolver( 92: + String line 93: + ) { 94: + String[] input_terms = line.split( " \\| " ); 95: + int inputInd = 0, termInd = inputInd +1; 96: + String[] inputsSplit = input_terms[ inputInd ].split( " " ); 97: + String[] termsSplit = input_terms[ termInd ].split( " " ); 98: + for ( String input : inputsSplit ) 99: + { 100: + char[] charsOfInput = input.toCharArray(); 101: + Arrays.sort( charsOfInput ); 102: + inputsToResolve.add( new String( charsOfInput ) ); 103: + } 104: + for ( String term : termsSplit ) 105: + { 106: + char[] charsOfTerm = term.toCharArray(); 107: + Arrays.sort( charsOfTerm ); 108: + termsToResolve.add( new String( charsOfTerm ) ); 109: + } 110: + } 111: + 112: + 113: + int shownValue( 114: + ) { 115: + characterizeByContent(); 116: + characterizeByLength(); 117: + deriveTopCenter(); 118: + deriveTwoAndRightBottom(); 119: + deriveRightTop(); 120: + deriveSixAndFive(); 121: + deriveLeftBottom(); 122: + deriveZeroAndNine(); 123: + deriveThree(); 124: + translateTerms(); 125: + 126: + return value; 127: + } 128: + 129: + 130: + private void characterizeByContent( 131: + ) { 132: + for ( String letter : letters ) 133: + { 134: + List<String> inputsWithLetter = new LinkedList<>(); 135: + List<String> inputsWithoutLetter = new LinkedList<>(); 136: + for ( String input : inputsToResolve ) 137: + { 138: + if ( input.contains( letter ) ) 139: + inputsWithLetter.add( input ); 140: + else 141: + inputsWithoutLetter.add( input ); 142: + } 143: + char_inputs.put( letter, inputsWithLetter ); 144: + missingChar_inputs.put( letter, inputsWithoutLetter ); 145: + } 146: + } 147: + 148: + 149: + private void characterizeByLength( 150: + ) { 151: + for ( String input : inputsToResolve ) 152: + { 153: + int len = input.length(); 154: + if ( len == 2 ) 155: + segment1 = input; 156: + else if ( len == 4 ) 157: + segment4 = input; 158: + else if ( len == 3 ) 159: + segment7 = input; 160: + else if ( len == 7 ) 161: + segment8 = input; 162: + else 163: + { 164: + if ( ! length_inputs.containsKey( len ) ) 165: + length_inputs.put( len, new LinkedList<>() ); 166: + List<String> inputsOfLen = length_inputs.get( len ); 167: + inputsOfLen.add( input ); 168: + } 169: + } 170: + } 171: + 172: + 173: + private void deriveTopCenter( 174: + ) { 175: + String inSevenNotOne = null; 176: + for ( int ind = 0; ind < segment7.length(); ind++ ) 177: + { 178: + String there = segment7.substring( ind, ind +1 ); 179: + if ( segment1.contains( there ) ) 180: + continue; 181: + else 182: + inSevenNotOne = there; 183: + } 184: + if ( inSevenNotOne != null ) 185: + centerTop = inSevenNotOne; 186: + else 187: + throw new RuntimeException( "could not derive tcenter" ); 188: + } 189: + 190: + 191: + private void deriveTwoAndRightBottom( 192: + ) { 193: + String bottomRight = null, two = null; 194: + for ( String candidate : missingChar_inputs.keySet() ) 195: + { 196: + List<String> inputsThatLack = missingChar_inputs.get( candidate ); 197: + if ( inputsThatLack.size() == 1 ) 198: + { 199: + bottomRight = candidate; 200: + two = inputsThatLack.get( 0 ); 201: + break; 202: + } 203: + } 204: + if ( bottomRight == null ) 205: + throw new RuntimeException( "could not derive 2" ); 206: + { 207: + segment2 = two; 208: + rightBottom = bottomRight; 209: + } 210: + } 211: + 212: + 213: + private void deriveRightTop( 214: + ) { 215: + String firstChar = segment1.substring( 0, 1 ); 216: + rightTop = ! rightBottom.equals( firstChar ) 217: + ? firstChar : segment1.substring( 1, 2 ); 218: + } 219: + 220: + 221: + private void deriveSixAndFive( 222: + ) { 223: + String six = null; 224: + List<String> inputsThatLackRightTop = missingChar_inputs.get( rightTop ); 225: + if ( inputsThatLackRightTop.get( 0 ).length() == 5 ) 226: + { 227: + segment6 = inputsThatLackRightTop.get( 1 ); 228: + segment5 = inputsThatLackRightTop.get( 0 ); 229: + } 230: + else 231: + { 232: + segment6 = inputsThatLackRightTop.get( 0 ); 233: + segment5 = inputsThatLackRightTop.get( 1 ); 234: + } 235: + } 236: + 237: + 238: + private void deriveLeftBottom( 239: + ) { 240: + String bottomLeft = null; 241: + for ( int ind = 0; ind < segment6.length(); ind++ ) 242: + { 243: + if ( ! segment5.contains( segment6.substring( ind, ind +1 ) ) ) 244: + { 245: + bottomLeft = segment6.substring( ind, ind +1 ); 246: + break; 247: + } 248: + } 249: + if ( bottomLeft == null ) 250: + throw new RuntimeException( "could not derive lbottom" ); 251: + else 252: + leftBottom = bottomLeft; 253: + } 254: + 255: + 256: + private void deriveZeroAndNine( 257: + ) { 258: + List<String> inputsLen6 = length_inputs.get( 6 ); 259: + for ( String input : inputsLen6 ) 260: + { 261: + if ( input.equals( segment6 ) ) 262: + continue; 263: + else if ( ! input.contains( leftBottom ) ) 264: + segment9 = input; 265: + else 266: + segment0 = input; 267: + } 268: + } 269: + 270: + 271: + private void deriveThree( 272: + ) { 273: + List<String> inputsLen5 = length_inputs.get( 5 ); 274: + for ( String input : inputsLen5 ) 275: + { 276: + if ( input.equals( segment2 ) || input.equals( segment5 ) ) 277: + continue; 278: + else 279: + { 280: + segment3 = input; 281: + break; 282: + } 283: + } 284: + } 285: + 286: + 287: + private void translateTerms( 288: + ) { 289: + int[] digits = { -1, -1, -1, -1 }; 290: + for ( int ind = 0; ind < termsToResolve.size(); ind++ ) 291: + { 292: + String term = termsToResolve.get( ind ); 293: + if ( term.equals( segment0 ) ) 294: + digits[ ind ] = 0; 295: + else if ( term.equals( segment1 ) ) 296: + digits[ ind ] = 1; 297: + else if ( term.equals( segment2 ) ) 298: + digits[ ind ] = 2; 299: + else if ( term.equals( segment3 ) ) 300: + digits[ ind ] = 3; 301: + else if ( term.equals( segment4 ) ) 302: + digits[ ind ] = 4; 303: + else if ( term.equals( segment5 ) ) 304: + digits[ ind ] = 5; 305: + else if ( term.equals( segment6 ) ) 306: + digits[ ind ] = 6; 307: + else if ( term.equals( segment7 ) ) 308: + digits[ ind ] = 7; 309: + else if ( term.equals( segment8 ) ) 310: + digits[ ind ] = 8; 311: + else if ( term.equals( segment9 ) ) 312: + digits[ ind ] = 9; 313: + } 314: + value = 0; 315: + value += digits[ 0 ] * 1_000; 316: + value += digits[ 1 ] * 100; 317: + value += digits[ 2 ] * 10; 318: + value += digits[ 3 ]; 319: + } 320: + 321: + 322: + /* 323: + 324: + 325: + private void ( 326: + ) { 327: + } 328: + */ 329: + 330: + 331: + } 332: + 333: + 334: +} 335: + 336: + 337: + 338: + 339: + 340: + 341: + 342: + 343: + 344: + 345: + 346: + 347: + 348: + 349: + 350: + 351: + 352: + 353: + 354: + 355: + 356: + 357: + 358: + 359: + 360: + 361: + 362: diff --git a/src/res/y2021/21_08_solutions.txt b/src/res/y2021/21_08_solutions.txt 363: index 699a77e..38a385d 100644 364: --- a/src/res/y2021/21_08_solutions.txt 365: +++ b/src/res/y2021/21_08_solutions.txt 366: @@ -3,4 +3,4 @@ example 1 : 26 367: puzzle 1 : 369 368: 369: example 2 : 61229 370: -puzzle 2 : 371: +puzzle 2 : 1031553