some-advent-of-code: fbcf610291da5e743ec2f593c38a04441d187edc

     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.Collection;
     8: import java.util.HashSet;
     9: import java.util.LinkedList;
    10: import java.util.List;
    11: import java.util.Map;
    12: import java.util.Set;
    13: import java.util.TreeMap;
    14: 
    15: public class Exercise200401
    16: {
    17: 
    18: 	public static void main( String args[] )
    19: 	{
    20: 		final String here = "e20012.m ";
    21: 		if ( args.length < 1 )
    22: 		{
    23: 			throw new RuntimeException( here +"add a filename argument" );
    24: 		}
    25: 		String userSaysFile = args[ 0 ];
    26: 		List<String> fileLines = new LinkedList<>();
    27: 		try
    28: 		{
    29: 			Path where = Paths.get( userSaysFile );
    30: 			fileLines = Files.readAllLines( where );
    31: 		}
    32: 		catch ( IOException | InvalidPathException ie )
    33: 		{
    34: 			System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
    35: 			return;
    36: 		}
    37: 		/*
    38: 		- interpretation of spec -
    39: 		for values in file
    40: 			save
    41: 		for values in less than half
    42: 			for values in more than half
    43: 				if sum is target
    44: 					print less * more
    45: 		*/
    46: 		// gather
    47: 		int valid = 0;
    48: 		Collection<Passport> values = new LinkedList<>();
    49: 		Collection<String> onePassport = new LinkedList<>();
    50: 		int lineNum = 0;
    51: 		boolean parsing = false;
    52: 		Exercise200401 buh = new Exercise200401();
    53: 		for ( String line : fileLines )
    54: 		{
    55: 			lineNum += 1;
    56: 			if ( line.isEmpty() )
    57: 			{
    58: 				if ( ! onePassport.isEmpty() )
    59: 				{
    60: 					Exercise200401.Passport person = buh.new Passport( onePassport );
    61: 					values.add( person );
    62: 					if ( person.valid() )
    63: 						valid += 1;
    64: 				}
    65: 				onePassport.clear();
    66: 				continue;
    67: 			}
    68: 			else
    69: 			{
    70: 				onePassport.add( line );
    71: 			}
    72: 		}
    73: 		System.out.println( here +"input summed to "+ valid );
    74: 	}
    75: 
    76: 
    77: 	private class Passport
    78: 	{
    79: 		String birthYear = "";
    80: 		String issueYear = "";
    81: 		String expirationYear = "";
    82: 		String height = "";
    83: 		String hairColor = "";
    84: 		String eyeColor = "";
    85: 		String passportId = "";
    86: 		String countryId = "";
    87: 		final String byr = "byr",
    88: 			iyr = "iyr",
    89: 			eyr = "eyr",
    90: 			hgt = "hgt",
    91: 			hcl = "hcl",
    92: 			ecl = "ecl",
    93: 			pid = "pid",
    94: 			cid = "cid";
    95: 
    96: 		public Passport(
    97: 				Collection<String> input
    98: 		) {
    99: 			parse( input );
   100: 		}
   101: 		private void parse(
   102: 				Collection<String> input
   103: 		) {
   104: 			final String here = "e20012.pp.p ";
   105: 			for ( String line : input )
   106: 			{
   107: 				String[] lineElements = line.split( " " );
   108: 				for ( String element : lineElements )
   109: 				{
   110: 					int colonInd = element.indexOf( ':' );
   111: 					String aspect = element.substring( 0, colonInd );
   112: 					String content = element.substring( colonInd +1 );
   113: 					switch ( aspect )
   114: 					{
   115: 						case byr :
   116: 							birthYear = content;
   117: 							break;
   118: 						case iyr :
   119: 							issueYear = content;
   120: 							break;
   121: 						case eyr :
   122: 							expirationYear = content;
   123: 							break;
   124: 						case hgt :
   125: 							height = content;
   126: 							break;
   127: 						case hcl :
   128: 							hairColor = content;
   129: 							break;
   130: 						case ecl :
   131: 							eyeColor = content;
   132: 							break;
   133: 						case pid :
   134: 							passportId = content;
   135: 							break;
   136: 						case cid :
   137: 							countryId = content;
   138: 							break;
   139: 					}
   140: 				}
   141: 			}
   142: 		}
   143: 		public boolean valid(
   144: 		) {
   145: 			return ! birthYear.isEmpty()
   146: 					&& ! issueYear.isEmpty()
   147: 					&& ! expirationYear.isEmpty()
   148: 					&& ! height.isEmpty()
   149: 					&& ! hairColor.isEmpty()
   150: 					&& ! eyeColor.isEmpty()
   151: 					&& ! passportId.isEmpty();
   152: 					// && ! countryId.isEmpty(); // haha, hacking
   153: 		}
   154: 	}
   155: }
   156: 
   157: 
   158: 
   159: 
   160: 
   161: 
   162: 
   163: 
   164: 
   165: 
   166: 
   167: 
   168: 
   169: 
   170: 
   171: 
   172: 
   173: 
   174: 
   175: 
   176: 
   177: 
   178: 
   179: 
   180: 
   181: 
   182: 

Generated by git2html.