some-advent-of-code: a980df4ad47e2612c9a200666b220899ccb823ae

     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 Exercise200602
    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: 		*/
    39: 		// gather
    40: 		Map<Character, Integer> groupAnswers = new TreeMap<>();
    41: 		int target = 0, lineNum = 0;
    42: 		for ( String line : fileLines )
    43: 		{
    44: 			if ( line.isEmpty() )
    45: 			{
    46: 				for ( Character answer : groupAnswers.keySet() )
    47: 					if ( groupAnswers.get( answer ) == lineNum )
    48: 						target += 1;
    49: 				groupAnswers.clear();
    50: 				lineNum = 0;
    51: 			}
    52: 			else
    53: 			{
    54: 				lineNum += 1;
    55: 				for ( int ind = 0; ind < line.length(); ind++ )
    56: 				{
    57: 					char answer = line.charAt( ind );
    58: 					if ( ! groupAnswers.containsKey( answer ) )
    59: 						groupAnswers.put( answer, 0 );
    60: 					groupAnswers.put( answer, groupAnswers.get( answer ) +1 );
    61: 				}
    62: 			}
    63: 		}
    64: 		if ( ! groupAnswers.isEmpty() )
    65: 		{
    66: 			for ( Character answer : groupAnswers.keySet() )
    67: 				if ( groupAnswers.get( answer ) == lineNum )
    68: 					target += 1;
    69: 		}
    70: 		System.out.println( here +"input had no pair that summed to "+ target );
    71: 	}
    72: }
    73: 
    74: 
    75: 
    76: 
    77: 
    78: 
    79: 
    80: 
    81: 
    82: 
    83: 
    84: 
    85: 
    86: 
    87: 
    88: 
    89: 
    90: 
    91: 
    92: 
    93: 
    94: 
    95: 
    96: 
    97: 
    98: 
    99: 

Generated by git2html.