some-advent-of-code: b71140734f112ed7855b6ed56d0695d961a0c1b6

     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.LinkedList;
     8: import java.util.List;
     9: import java.util.Map;
    10: import java.util.TreeMap;
    11: 
    12: public class Exercise18021
    13: {
    14: 
    15: 	public static void main( String args[] )
    16: 	{
    17: 		final String here = "e18021.m ";
    18: 		if ( args.length < 1 )
    19: 		{
    20: 			throw new RuntimeException( here +"add a filename argument" );
    21: 		}
    22: 		String userSaysFile = args[ 0 ];
    23: 		List<String> fileLines = new LinkedList<>();
    24: 		try
    25: 		{
    26: 			Path where = Paths.get( userSaysFile );
    27: 			fileLines = Files.readAllLines( where );
    28: 		}
    29: 		catch ( IOException | InvalidPathException ie )
    30: 		{
    31: 			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
    32: 			return;
    33: 		}
    34: 		/*
    35: 		- interpretation of spec -
    36: 		make a map
    37: 		make a trackingVal for pairs and triple chars
    38: 		iterate over the list of words
    39: 		  clear the map
    40: 		  iterate over the characters of the current word
    41: 			at each character, add 1 to map at that position
    42: 		  iterate over the map characters
    43: 		  |= two bools: exactly 2 and exactly 3 occurances
    44: 		  if bool of occurance is true, add 1 to that trackingVal
    45: 		multiply the values of the pairs against the triples, that's the answer
    46: 		*/
    47: 		Map<Character, Integer> charDistribution = new TreeMap<>();
    48: 		int pairCount = 0, tripleCount = 0;
    49: 		for ( String word : fileLines )
    50: 		{
    51: 			if ( word.isEmpty() )
    52: 			{
    53: 				continue;
    54: 			}
    55: 			charDistribution.clear();
    56: 			for ( int ind = word.length() -1; ind >= 0; ind-- )
    57: 			{
    58: 				Character nibble = word.charAt( ind );
    59: 				Integer nibbleSeen = charDistribution.get( nibble );
    60: 				if ( nibbleSeen == null )
    61: 				{
    62: 					nibbleSeen = Integer.valueOf( 0 );
    63: 					charDistribution.put( nibble, nibbleSeen );
    64: 				}
    65: 				nibbleSeen = Integer.valueOf( nibbleSeen.intValue() +1 );
    66: 				charDistribution.put( nibble, nibbleSeen );
    67: 			}
    68: 			boolean foundPair = false, foundTriple = false;
    69: 			for ( Character nibble : charDistribution.keySet() )
    70: 			{
    71: 				Integer nibbleTimes = charDistribution.get( nibble );
    72: 				if ( nibbleTimes == 2 )
    73: 				{
    74: 					foundPair |= true;
    75: 				}
    76: 				if ( nibbleTimes == 3 )
    77: 				{
    78: 					foundTriple |= true;
    79: 				}
    80: 				if ( foundPair && foundTriple )
    81: 				{
    82: 					break;
    83: 					// NOTE going through all of them is fine if we didn't find either or only one
    84: 				}
    85: 			}
    86: 			if ( foundPair )
    87: 			{
    88: 				pairCount++;
    89: 			}
    90: 			if ( foundTriple )
    91: 			{
    92: 				tripleCount++;
    93: 			}
    94: 		}
    95: 		System.out.println( here +" pairs "+ pairCount +" triples "+ tripleCount +" result "+ ( pairCount * tripleCount ) );
    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.