some-advent-of-code: 44af86e7dd20bab300a8dcdce0a85cdd90e96dc2

     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 Exercise200202
    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 : position pair, letter, password
    40: 			if letter is in only one of the positions (one indexed)
    41: 				increment
    42: 		*/
    43: 		int lineNum = 0;
    44: 		int validCount = 0;
    45: 		for ( String line : fileLines )
    46: 		{
    47: 			lineNum += 1;
    48: 			try
    49: 			{
    50: 				Exercise200202 passwordRule = new Exercise200202();
    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 positionInd = 0, interestInd = positionInd +1, inputInd = interestInd +1;
    75: 		String[] pieces = entireLine.split( " " );
    76: 		String[] positionPieces = pieces[ positionInd ].split( "-" );
    77: 		String interest = pieces[ interestInd ].substring(
    78: 			0, pieces[ interestInd ].indexOf( ":" ) );
    79: 		char desired = interest.charAt( 0 );
    80: 		final int positionMinInd = 0, positionMaxInd = positionMinInd +1;
    81: 		int firstInd = Integer.parseInt( positionPieces[ positionMinInd ] ) -1;
    82: 		int secondInd = Integer.parseInt( positionPieces[ positionMaxInd ] ) -1;
    83: 		int timesUsed = 0;
    84: 		if ( firstInd < pieces[ inputInd ].length()
    85: 				&& pieces[ inputInd ].charAt( firstInd ) == desired )
    86: 		{
    87: 			timesUsed += 1;
    88: 		}
    89: 		if ( secondInd < pieces[ inputInd ].length()
    90: 				&& pieces[ inputInd ].charAt( secondInd ) == desired )
    91: 		{
    92: 			timesUsed += 1;
    93: 		}
    94: 		return timesUsed == 1;
    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.