some-advent-of-code: 6b1037d6894de445951b4e49955063f6116e5ecd
1:
2: import java.io.IOException;
3: import java.nio.file.*;
4: import java.util.*;
5:
6: public class Exercise210802
7: {
8:
9: public static void main(
10: String args[]
11: ) {
12: final String here = "e210802.m ";
13: if ( args.length < 1 )
14: {
15: throw new RuntimeException( here +"add a filename argument" );
16: }
17: String userSaysFile = args[ 0 ];
18: List<String> fileLines = new LinkedList<>();
19: try
20: {
21: Path where = Paths.get( userSaysFile );
22: fileLines = Files.readAllLines( where );
23: }
24: catch ( IOException | InvalidPathException ie )
25: {
26: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
27: return;
28: }
29: /*
30: - interpretation of spec -52
31: */
32: new Exercise210802().countSegments( fileLines );
33: }
34:
35:
36: private void countSegments(
37: List<String> fileLines
38: ) {
39: int outputs = 0;
40: for ( String line : fileLines )
41: {
42: if ( line.isEmpty() )
43: continue;
44: SegmentSolver expertSystem = new SegmentSolver( line );
45: outputs += expertSystem.shownValue();
46: }
47: System.out.println( "\tanswer is "+ outputs );
48: }
49:
50:
51: class SegmentSolver
52: {
53: int value = -1;
54:
55: String notSet = "-";
56: String centerTop = notSet;
57: String centerCenter = notSet;
58: String centerBottom = notSet;
59: String leftTop = notSet;
60: String rightTop = notSet;
61: String leftBottom = notSet;
62: String rightBottom = notSet;
63:
64: String segment0 = "";
65: String segment1 = "";
66: String segment2 = "";
67: String segment3 = "";
68: String segment4 = "";
69: String segment5 = "";
70: String segment6 = "";
71: String segment7 = "";
72: String segment8 = "";
73: String segment9 = "";
74:
75: String[] letters = { "a", "b", "c", "d", "e", "f", "g" };
76:
77: Map<String, List<String>> char_inputs = new HashMap<>();
78: Map<String, List<String>> missingChar_inputs = new HashMap<>();
79: Map<Integer, List<String>> length_inputs = new HashMap<>();
80:
81: List<String> inputsToResolve = new ArrayList<>( 10 );
82: List<String> termsToResolve = new ArrayList<>( 4 );
83:
84:
85: SegmentSolver(
86: String line
87: ) {
88: String[] input_terms = line.split( " \\| " );
89: int inputInd = 0, termInd = inputInd +1;
90: String[] inputsSplit = input_terms[ inputInd ].split( " " );
91: String[] termsSplit = input_terms[ termInd ].split( " " );
92: for ( String input : inputsSplit )
93: {
94: char[] charsOfInput = input.toCharArray();
95: Arrays.sort( charsOfInput );
96: inputsToResolve.add( new String( charsOfInput ) );
97: }
98: for ( String term : termsSplit )
99: {
100: char[] charsOfTerm = term.toCharArray();
101: Arrays.sort( charsOfTerm );
102: termsToResolve.add( new String( charsOfTerm ) );
103: }
104: }
105:
106:
107: int shownValue(
108: ) {
109: characterizeByContent();
110: characterizeByLength();
111: deriveTopCenter();
112: deriveTwoAndRightBottom();
113: deriveRightTop();
114: deriveSixAndFive();
115: deriveLeftBottom();
116: deriveZeroAndNine();
117: deriveThree();
118: translateTerms();
119:
120: return value;
121: }
122:
123:
124: private void characterizeByContent(
125: ) {
126: for ( String letter : letters )
127: {
128: List<String> inputsWithLetter = new LinkedList<>();
129: List<String> inputsWithoutLetter = new LinkedList<>();
130: for ( String input : inputsToResolve )
131: {
132: if ( input.contains( letter ) )
133: inputsWithLetter.add( input );
134: else
135: inputsWithoutLetter.add( input );
136: }
137: char_inputs.put( letter, inputsWithLetter );
138: missingChar_inputs.put( letter, inputsWithoutLetter );
139: }
140: }
141:
142:
143: private void characterizeByLength(
144: ) {
145: for ( String input : inputsToResolve )
146: {
147: int len = input.length();
148: if ( len == 2 )
149: segment1 = input;
150: else if ( len == 4 )
151: segment4 = input;
152: else if ( len == 3 )
153: segment7 = input;
154: else if ( len == 7 )
155: segment8 = input;
156: else
157: {
158: if ( ! length_inputs.containsKey( len ) )
159: length_inputs.put( len, new LinkedList<>() );
160: List<String> inputsOfLen = length_inputs.get( len );
161: inputsOfLen.add( input );
162: }
163: }
164: }
165:
166:
167: private void deriveTopCenter(
168: ) {
169: String inSevenNotOne = null;
170: for ( int ind = 0; ind < segment7.length(); ind++ )
171: {
172: String there = segment7.substring( ind, ind +1 );
173: if ( segment1.contains( there ) )
174: continue;
175: else
176: inSevenNotOne = there;
177: }
178: if ( inSevenNotOne != null )
179: centerTop = inSevenNotOne;
180: else
181: throw new RuntimeException( "could not derive tcenter" );
182: }
183:
184:
185: private void deriveTwoAndRightBottom(
186: ) {
187: String bottomRight = null, two = null;
188: for ( String candidate : missingChar_inputs.keySet() )
189: {
190: List<String> inputsThatLack = missingChar_inputs.get( candidate );
191: if ( inputsThatLack.size() == 1 )
192: {
193: bottomRight = candidate;
194: two = inputsThatLack.get( 0 );
195: break;
196: }
197: }
198: if ( bottomRight == null )
199: throw new RuntimeException( "could not derive 2" );
200: {
201: segment2 = two;
202: rightBottom = bottomRight;
203: }
204: }
205:
206:
207: private void deriveRightTop(
208: ) {
209: String firstChar = segment1.substring( 0, 1 );
210: rightTop = ! rightBottom.equals( firstChar )
211: ? firstChar : segment1.substring( 1, 2 );
212: }
213:
214:
215: private void deriveSixAndFive(
216: ) {
217: String six = null;
218: List<String> inputsThatLackRightTop = missingChar_inputs.get( rightTop );
219: if ( inputsThatLackRightTop.get( 0 ).length() == 5 )
220: {
221: segment6 = inputsThatLackRightTop.get( 1 );
222: segment5 = inputsThatLackRightTop.get( 0 );
223: }
224: else
225: {
226: segment6 = inputsThatLackRightTop.get( 0 );
227: segment5 = inputsThatLackRightTop.get( 1 );
228: }
229: }
230:
231:
232: private void deriveLeftBottom(
233: ) {
234: String bottomLeft = null;
235: for ( int ind = 0; ind < segment6.length(); ind++ )
236: {
237: if ( ! segment5.contains( segment6.substring( ind, ind +1 ) ) )
238: {
239: bottomLeft = segment6.substring( ind, ind +1 );
240: break;
241: }
242: }
243: if ( bottomLeft == null )
244: throw new RuntimeException( "could not derive lbottom" );
245: else
246: leftBottom = bottomLeft;
247: }
248:
249:
250: private void deriveZeroAndNine(
251: ) {
252: List<String> inputsLen6 = length_inputs.get( 6 );
253: for ( String input : inputsLen6 )
254: {
255: if ( input.equals( segment6 ) )
256: continue;
257: else if ( ! input.contains( leftBottom ) )
258: segment9 = input;
259: else
260: segment0 = input;
261: }
262: }
263:
264:
265: private void deriveThree(
266: ) {
267: List<String> inputsLen5 = length_inputs.get( 5 );
268: for ( String input : inputsLen5 )
269: {
270: if ( input.equals( segment2 ) || input.equals( segment5 ) )
271: continue;
272: else
273: {
274: segment3 = input;
275: break;
276: }
277: }
278: }
279:
280:
281: private void translateTerms(
282: ) {
283: int[] digits = { -1, -1, -1, -1 };
284: for ( int ind = 0; ind < termsToResolve.size(); ind++ )
285: {
286: String term = termsToResolve.get( ind );
287: if ( term.equals( segment0 ) )
288: digits[ ind ] = 0;
289: else if ( term.equals( segment1 ) )
290: digits[ ind ] = 1;
291: else if ( term.equals( segment2 ) )
292: digits[ ind ] = 2;
293: else if ( term.equals( segment3 ) )
294: digits[ ind ] = 3;
295: else if ( term.equals( segment4 ) )
296: digits[ ind ] = 4;
297: else if ( term.equals( segment5 ) )
298: digits[ ind ] = 5;
299: else if ( term.equals( segment6 ) )
300: digits[ ind ] = 6;
301: else if ( term.equals( segment7 ) )
302: digits[ ind ] = 7;
303: else if ( term.equals( segment8 ) )
304: digits[ ind ] = 8;
305: else if ( term.equals( segment9 ) )
306: digits[ ind ] = 9;
307: }
308: value = 0;
309: value += digits[ 0 ] * 1_000;
310: value += digits[ 1 ] * 100;
311: value += digits[ 2 ] * 10;
312: value += digits[ 3 ];
313: }
314:
315:
316: /*
317:
318:
319: private void (
320: ) {
321: }
322: */
323:
324:
325: }
326:
327:
328: }
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
Generated by git2html.