some-advent-of-code: 2fc43bcaf561d712d08e2514bcbea34e034878e8

     1: 
     2: import java.io.IOException;
     3: import java.nio.file.Files;
     4: import java.nio.file.InvalidPathException;
     5: import java.nio.file.Path;
     6: import java.nio.file.Paths;
     7: import java.util.HashSet;
     8: import java.util.LinkedList;
     9: import java.util.List;
    10: import java.util.Map;
    11: import java.util.Set;
    12: import java.util.TreeMap;
    13: 
    14: public class Exercise200201
    15: {
    16: 
    17: 	public static void main( String args[] )
    18: 	{
    19: 		final String here = "e20011.m ";
    20: 		if ( args.length < 1 )
    21: 		{
    22: 			throw new RuntimeException( here +"add a filename argument" );
    23: 		}
    24: 		String userSaysFile = args[ 0 ];
    25: 		List<String> fileLines = new LinkedList<>();
    26: 		try
    27: 		{
    28: 			Path where = Paths.get( userSaysFile );
    29: 			fileLines = Files.readAllLines( where );
    30: 		}
    31: 		catch ( IOException | InvalidPathException ie )
    32: 		{
    33: 			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
    34: 			return;
    35: 		}
    36: 		/*
    37: 		- interpretation of spec -
    38: 		for values in file
    39: 			parse line : value range, letter, password
    40: 			if letter has occurances in range in password
    41: 				increment
    42: 		*/
    43: 		int lineNum = 0;
    44: 		int validCount = 0;
    45: 		for ( String line : fileLines )
    46: 		{
    47: 			lineNum += 1;
    48: 			try
    49: 			{
    50: 				Exercise200201 passwordRule = new Exercise200201();
    51: 				// System.out.println( here +"checking "+ line );
    52: 				if ( passwordRule.satisfied( line ) )
    53: 					validCount += 1;
    54: 			}
    55: 			catch ( Exception nfe )
    56: 			{
    57: 				System.err.println( here +"handed a non int "+ line +" on "+ lineNum +" because "+ nfe );
    58: 				return;
    59: 			}
    60: 		}
    61: 		System.out.println( here +"input has "+ validCount +" valid passwords" );
    62: 	}
    63: 
    64: 
    65: 	private int minCount = 0;
    66: 	private int maxCount = 0;
    67: 	private String essentialChar = "";
    68: 
    69: 	/** form of min hyphen max space letter colon space input */
    70: 	boolean satisfied( String entireLine )
    71: 	{
    72: 		final String here = "e20011.s ";
    73: 		// if invalid etc
    74: 		final int rangeInd = 0, interestInd = rangeInd +1, inputInd = interestInd +1;
    75: 		String[] pieces = entireLine.split( " " );
    76: 		String[] rangePieces = pieces[ rangeInd ].split( "-" );
    77: 		String interest = pieces[ interestInd ].substring(
    78: 			0, pieces[ interestInd ].indexOf( ":" ) );
    79: 		final int rangeMinInd = 0, rangeMaxInd = rangeMinInd +1;
    80: 		minCount = Integer.parseInt( rangePieces[ rangeMinInd ] );
    81: 		maxCount = Integer.parseInt( rangePieces[ rangeMaxInd ] );
    82: 		int timesUsed = 0;
    83: 		int ind = pieces[ inputInd ].indexOf( interest, 0 );
    84: 		while ( ind < pieces[ inputInd ].length() && ind >= 0 )
    85: 		{
    86: 			ind += 1;
    87: 			timesUsed += 1;
    88: 			if ( timesUsed > maxCount )
    89: 				return false;
    90: 			// System.out.println( "\t"+ here +"another "+ ind +" currcount "+ timesUsed );
    91: 			ind = pieces[ inputInd ].indexOf( interest, ind );
    92: 		}
    93: 
    94: 		return timesUsed >= minCount;
    95: 	}
    96: 
    97: }
    98: 
    99: 
   100: 
   101: 
   102: 
   103: 
   104: 
   105: 
   106: 
   107: 
   108: 
   109: 
   110: 
   111: 
   112: 
   113: 
   114: 
   115: 
   116: 
   117: 
   118: 
   119: 
   120: 
   121: 
   122: 
   123: 
   124: 

Generated by git2html.