some-advent-of-code: 325affd260e709fcd593fe1dd69ef1364f18b2f7
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.LinkedList;
8: import java.util.List;
9: import java.util.Map;
10: import java.util.TreeMap;
11:
12: public class Exercise19022
13: {
14:
15: public static void main( String args[] )
16: {
17: final String here = "e19022.m ";
18: if ( args.length < 1 )
19: {
20: throw new RuntimeException( here +"add a filename argument" );
21: }
22: String userSaysFile = args[ 0 ];
23: List<String> fileLines = new LinkedList<>();
24: try
25: {
26: Path where = Paths.get( userSaysFile );
27: fileLines = Files.readAllLines( where );
28: }
29: catch ( IOException | InvalidPathException ie )
30: {
31: System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
32: return;
33: }
34: /*
35: - interpretation of spec -
36: file is single line csv of a recurring format with four relevant indicies:
37: { opCode, left operand, right operand, target index }
38: opcode 99 is halt
39: opcode 1 is memory[ target ] = left + right
40: opcode 2 is memory[ target ] = left * right
41: after that's done, skip to the next opcode, which is to say, by four
42: */
43: int initialInd = 0, loInd = initialInd +1, roInd = loInd +1,
44: tInd = roInd +1, nextInd = tInd +1;
45: String times = " * ", plus = " + ", operator = times;
46: String line = fileLines.get( 0 );
47: int pcounter = initialInd, nvLim = 135;
48: entireRun :
49: for ( int firstNoun = 0; firstNoun < nvLim; firstNoun += 1 )
50: {
51: for ( int firstVerb = 0; firstVerb < nvLim; firstVerb += 1 )
52: {
53: pcounter = initialInd;
54: String[] memory = line.split( "," );
55: System.out.println( "another run, "+ firstNoun +" & "+ firstVerb );
56: memory[ loInd ] = Integer.valueOf( firstNoun ).toString();
57: memory[ roInd ] = Integer.valueOf( firstVerb ).toString();
58: if ( (firstNoun == 0 && firstVerb == 0) )
59: // || (firstNoun == 0 && firstVerb == 1) )
60: {
61: for ( int indP = 0; indP < memory.length; indP += 1 )
62: {
63: System.out.print( memory[ indP ] +", " );
64: if ( (indP % 4) == 3 )
65: System.out.println();
66: }
67: }
68: // System.out.println( "- - -" );
69: int sentinel = 1_000_000_000;
70: int leftAddress = -1, rightAddress = -1, targetAddress = -1,
71: result = -1, leftOperand = -1, rightOperand = -1;
72: String opCode;
73: while ( sentinel > 0 && pcounter < memory.length )
74: {
75: sentinel -= 1;
76: opCode = memory[ pcounter ];
77: if ( opCode.equals( "99" ) )
78: {
79: // System.out.println( here +"halting" );
80: break;
81: }
82: leftAddress = Integer.parseInt( memory[ pcounter + loInd ] );
83: rightAddress = Integer.parseInt( memory[ pcounter + roInd ] );
84: targetAddress = Integer.parseInt( memory[ pcounter + tInd ] );
85: if ( leftAddress <= memory.length )
86: leftOperand = Integer.parseInt( memory[ leftAddress ] );
87: else
88: System.out.println( here +"left address outside at "+ leftAddress );
89: if ( rightAddress <= memory.length )
90: rightOperand = Integer.parseInt( memory[ rightAddress ] );
91: else
92: System.out.println( here +"right address outside at "+ rightAddress );
93: if ( opCode.equals( "1" ) )
94: {
95: result = leftOperand + rightOperand;
96: operator = plus;
97: }
98: else if ( opCode.equals( "2" ) )
99: {
100: result = leftOperand * rightOperand;
101: operator = times;
102: }
103: else
104: {
105: System.out.println( here +"unrecognized op code "+ opCode );
106: }
107: if ( targetAddress >= memory.length )
108: {
109: /*
110: System.out.println( " "+ here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
111: + rightOperand +"@"+ rightAddress +" outside to "+ targetAddress );
112: */
113: }
114: else
115: {
116: //
117: /*
118: System.out.println( here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
119: + rightOperand +"@"+ rightAddress +" to "
120: + targetAddress +" ["+ memory[ targetAddress ] +"]" );
121: */
122: memory[ targetAddress ] = Integer.valueOf( result ).toString();
123: }
124: pcounter += nextInd;
125: }
126:
127: System.out.println( "\t"+ here +"first opcode is "+ memory[ 0 ] );
128: if ( "19690720".equals( memory[ 0 ] ) )
129: {
130: break entireRun;
131: }
132: }
133: }
134: }
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:
Generated by git2html.