some-advent-of-code: cc6f3e18cd9a4a8748ae34af43249332dc58e409
1:
2: import java.io.IOException;
3: import java.nio.file.*;
4: import java.util.*;
5:
6: public class Exercise210302
7: {
8:
9: public static void main(
10: String args[]
11: ) {
12: final String here = "e210302.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 -
31: sum the bits individually, discard and resum as necessary
32: */
33: Exercise210302.interpretDiagnostic( fileLines );
34: }
35:
36:
37: private static void interpretDiagnostic(
38: List<String> fileLines
39: ) {
40: int ones = 0, nils = 0;
41: int oxygenRating = 0, carbonRating = 0;
42: List<String> previousCandidates = new ArrayList<>( fileLines.size() );
43: previousCandidates.addAll( fileLines );
44: List<String> currentCandidates = new LinkedList<>();
45: int len = fileLines.get( 0 ).length();
46:
47: // oxygen
48: for ( int charInd = 0; charInd < len; charInd++ )
49: {
50: ones = 0;
51: nils = 0;
52: for ( String line : previousCandidates )
53: {
54: if ( line.charAt( charInd ) == '1' )
55: ones += 1;
56: else // == 0
57: nils += 1;
58: }
59:
60: char toMatch;
61: if ( ones > nils )
62: toMatch = '1';
63: else if ( ones < nils )
64: toMatch = '0';
65: else
66: toMatch = '='; // for clarity when comparing == below
67: for ( String candidate : previousCandidates )
68: {
69: if ( candidate.charAt( charInd ) == toMatch
70: || ( ones == nils
71: && candidate.charAt( charInd ) == '1' ) )
72: currentCandidates.add( candidate );
73: }
74: if ( currentCandidates.size() == 1 )
75: break;
76: previousCandidates.clear();
77: previousCandidates.addAll( currentCandidates );
78: currentCandidates.clear();
79: }
80: oxygenRating = Exercise210302.asInt( currentCandidates.get( 0 ) );
81:
82: // carbon
83: previousCandidates.clear();
84: previousCandidates.addAll( fileLines );
85: currentCandidates.clear();
86: for ( int charInd = 0; charInd < len; charInd++ )
87: {
88: ones = 0;
89: nils = 0;
90: for ( String line : previousCandidates )
91: {
92: if ( line.charAt( charInd ) == '1' )
93: ones += 1;
94: else // == 0
95: nils += 1;
96: }
97:
98: char toMatch;
99: if ( ones < nils )
100: toMatch = '1';
101: else if ( ones > nils )
102: toMatch = '0';
103: else
104: toMatch = '='; // for clarity when comparing == below
105: for ( String candidate : previousCandidates )
106: {
107: if ( candidate.charAt( charInd ) == toMatch
108: || ( ones == nils
109: && candidate.charAt( charInd ) == '0' ) )
110: currentCandidates.add( candidate );
111: }
112: if ( currentCandidates.size() == 1 )
113: break;
114: previousCandidates.clear();
115: previousCandidates.addAll( currentCandidates );
116: currentCandidates.clear();
117: }
118: carbonRating = Exercise210302.asInt( currentCandidates.get( 0 ) );
119:
120: System.out.println( "\to"+ oxygenRating +" c"+ carbonRating +" t"+ oxygenRating * carbonRating );
121: }
122:
123:
124: private static int asInt(
125: String bitChars
126: ) {
127: int value = 0, multiplier = 1;
128: for ( int charInd = bitChars.length() -1; charInd >= 0; charInd-- )
129: {
130: if ( bitChars.charAt( charInd ) == '1' )
131: value += multiplier;
132: multiplier *= 2;
133: }
134: return value;
135: }
136:
137:
138: }
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
Generated by git2html.