some-advent-of-code: 55a3e1c2b96235e2fa8bb04c9016fc6a5a6536c8
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.regex.*;
13: import java.util.Set;
14: import java.util.TreeMap;
15:
16: public class Exercise200402
17: {
18:
19: public static void main( String args[] )
20: {
21: final String here = "e20042.m ";
22: if ( args.length < 1 )
23: {
24: throw new RuntimeException( here +"add a filename argument" );
25: }
26: String userSaysFile = args[ 0 ];
27: List<String> fileLines = new LinkedList<>();
28: try
29: {
30: Path where = Paths.get( userSaysFile );
31: fileLines = Files.readAllLines( where );
32: }
33: catch ( IOException | InvalidPathException ie )
34: {
35: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
36: return;
37: }
38: /*
39: - interpretation of spec -
40: */
41: int valid = 0;
42: Collection<String> onePassport = new LinkedList<>();
43: int lineNum = 0;
44: boolean parsing = false;
45: Exercise200402 buh = new Exercise200402();
46: Pattern hexColor = Pattern.compile( "#[abcdef\\d]{6}" );
47: for ( String line : fileLines )
48: {
49: if ( line.isEmpty() )
50: {
51: lineNum += 1;
52: if ( ! onePassport.isEmpty() )
53: {
54: Exercise200402.Passport person = buh.new Passport( onePassport );
55: if ( person.valid( hexColor ) )
56: {
57: valid += 1;
58: System.out.println( "\t"+ person );
59: }
60: }
61: onePassport.clear();
62: continue;
63: }
64: else
65: {
66: onePassport.add( line );
67: }
68: }
69: if ( ! onePassport.isEmpty() )
70: {
71: Exercise200402.Passport person = buh.new Passport( onePassport );
72: if ( person.valid( hexColor ) )
73: {
74: valid += 1;
75: System.out.println( "\t"+ person );
76: }
77: }
78: System.out.println( here +"input summed to "+ valid +" of "+ lineNum +" total" );
79: }
80:
81:
82: private class Passport
83: {
84: String birthYear = "";
85: String issueYear = "";
86: String expirationYear = "";
87: String height = "";
88: String hairColor = "";
89: String eyeColor = "";
90: String passportId = "";
91: String countryId = "";
92: final String byr = "byr",
93: iyr = "iyr",
94: eyr = "eyr",
95: hgt = "hgt",
96: hcl = "hcl",
97: ecl = "ecl",
98: pid = "pid",
99: cid = "cid";
100:
101: public Passport(
102: Collection<String> input
103: ) {
104: parse( input );
105: }
106: private void parse(
107: Collection<String> input
108: ) {
109: final String here = "e20012.pp.p ";
110: for ( String line : input )
111: {
112: String[] lineElements = line.split( " " );
113: for ( String element : lineElements )
114: {
115: int colonInd = element.indexOf( ':' );
116: String aspect = element.substring( 0, colonInd );
117: String content = element.substring( colonInd +1 );
118: switch ( aspect )
119: {
120: case byr :
121: birthYear = content;
122: break;
123: case iyr :
124: issueYear = content;
125: break;
126: case eyr :
127: expirationYear = content;
128: break;
129: case hgt :
130: height = content;
131: break;
132: case hcl :
133: hairColor = content;
134: break;
135: case ecl :
136: eyeColor = content;
137: break;
138: case pid :
139: passportId = content;
140: break;
141: case cid :
142: countryId = content;
143: break;
144: }
145: }
146: }
147: }
148: public boolean valid(
149: Pattern hexColor
150: ) {
151: if ( birthYear.isEmpty()
152: || issueYear.isEmpty()
153: || expirationYear.isEmpty()
154: || height.isEmpty()
155: || hairColor.isEmpty()
156: || eyeColor.isEmpty()
157: || passportId.isEmpty() )
158: // || countryId.isEmpty(); // haha, hacking
159: {
160: // System.out.println( "v not full" );
161: return false;
162: }
163: else if ( ! clamp( birthYear, 1920, 2002 ) )
164: {
165: // System.out.println( "v not birthyear "+ birthYear );
166: return false;
167: }
168: else if ( ! clamp( issueYear, 2010, 2020 ) )
169: {
170: // System.out.println( "v not issueYear "+ issueYear );
171: return false;
172: }
173: else if ( ! clamp( expirationYear, 2020, 2030 ) )
174: {
175: // System.out.println( "v not expirationYear "+ expirationYear );
176: return false;
177: }
178: else if ( ! eye() )
179: {
180: // System.out.println( "v not eye "+ eyeColor );
181: return false;
182: }
183: else if ( ! height() )
184: {
185: // System.out.println( "v not eye "+ eyeColor );
186: return false;
187: }
188: else if ( ! hexColor.matcher( hairColor ).find() )
189: {
190: // System.out.println( "v not hair matcher "+ hairColor );
191: return false;
192: }
193: else if ( passportId.length() != 9 || ! clamp( passportId, 0, 999_999_999 ) )
194: {
195: // System.out.println( "v not passport "+ passportId );
196: return false;
197: }
198: return true;
199: }
200: private boolean clamp(
201: String asChars, int min, int max
202: ) {
203: try {
204: int value = Integer.parseInt( asChars );
205: return value >= min && value <= max;
206: }
207: catch ( NumberFormatException nfe )
208: {
209: return false;
210: }
211: }
212: private boolean eye(
213: ) {
214: return eyeColor.equals( "amb" )
215: || eyeColor.equals( "blu" )
216: || eyeColor.equals( "brn" )
217: || eyeColor.equals( "gry" )
218: || eyeColor.equals( "grn" )
219: || eyeColor.equals( "hzl" )
220: || eyeColor.equals( "oth" );
221: }
222: private boolean height(
223: ) {
224: if ( height.endsWith( "in" ) )
225: return clamp( height.substring( 0, height.length() -2 ), 59, 76 );
226: else if ( height.endsWith( "cm" ) )
227: return clamp( height.substring( 0, height.length() -2 ), 150, 193 );
228: else
229: return false;
230: }
231: public String toString(
232: ) {
233: return ""/*
234: "by"+ birthYear
235: +" iy"+ issueYear
236: +"\t ey"+ expirationYear
237: +"\t ec_"+ eyeColor
238: */
239: +"\t h "+ height.substring( 0, height.length() -2 ) +"_"+ height.substring( height.length() -2 )
240: /*
241: +" pi"+ passportId
242: +" _"+ hairColor
243: */ ;
244: }
245: }
246: }
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
Generated by git2html.