Author: Nicholas Prado <nmprado@nzen.ws>
Date: Tue Dec 3 05:32:17 UTC 2019
Parent: 38dae3c68fc4be62f3e43c569bdd53a55d60b3b5
Log message:
feat 191202_2 brute forces particular answer
Increments the first noun and first verb until it reaches the
desired value. Renames the inputs to match the parts they
correspond to.
1: diff --git a/src/java/Exercise19022.java b/src/java/Exercise19022.java
2: new file mode 100644
3: index 0000000..cf466c8
4: --- /dev/null
5: +++ b/src/java/Exercise19022.java
6: @@ -0,0 +1,162 @@
7: +
8: +import java.io.IOException;
9: +import java.nio.file.Files;
10: +import java.nio.file.InvalidPathException;
11: +import java.nio.file.Path;
12: +import java.nio.file.Paths;
13: +import java.util.LinkedList;
14: +import java.util.List;
15: +import java.util.Map;
16: +import java.util.TreeMap;
17: +
18: +public class Exercise19022
19: +{
20: +
21: + public static void main( String args[] )
22: + {
23: + final String here = "e19011.m ";
24: + if ( args.length < 1 )
25: + {
26: + throw new RuntimeException( here +"add a filename argument" );
27: + }
28: + String userSaysFile = args[ 0 ];
29: + List<String> fileLines = new LinkedList<>();
30: + try
31: + {
32: + Path where = Paths.get( userSaysFile );
33: + fileLines = Files.readAllLines( where );
34: + }
35: + catch ( IOException | InvalidPathException ie )
36: + {
37: + System.err.println( here +"couldn't read file "+ userSaysFile +" because "+ ie );
38: + return;
39: + }
40: + /*
41: + - interpretation of spec -
42: + file is single line csv of a recurring format with four relevant indicies:
43: + { opCode, left operand, right operand, target index }
44: + opcode 99 is halt
45: + opcode 1 is memory[ target ] = left + right
46: + opcode 2 is memory[ target ] = left * right
47: + after that's done, skip to the next opcode, which is to say, by four
48: + */
49: + int initialInd = 0, loInd = initialInd +1, roInd = loInd +1,
50: + tInd = roInd +1, nextInd = tInd +1;
51: + String times = " * ", plus = " + ", operator = times;
52: + String line = fileLines.get( 0 );
53: + int pcounter = initialInd, nvLim = 135;
54: + entireRun :
55: + for ( int firstNoun = 0; firstNoun < nvLim; firstNoun += 1 )
56: + {
57: + for ( int firstVerb = 0; firstVerb < nvLim; firstVerb += 1 )
58: + {
59: + pcounter = initialInd;
60: + String[] memory = line.split( "," );
61: + System.out.println( "another run, "+ firstNoun +" & "+ firstVerb );
62: + memory[ loInd ] = Integer.valueOf( firstNoun ).toString();
63: + memory[ roInd ] = Integer.valueOf( firstVerb ).toString();
64: + if ( (firstNoun == 0 && firstVerb == 0) )
65: + // || (firstNoun == 0 && firstVerb == 1) )
66: + {
67: + for ( int indP = 0; indP < memory.length; indP += 1 )
68: + {
69: + System.out.print( memory[ indP ] +", " );
70: + if ( (indP % 4) == 3 )
71: + System.out.println();
72: + }
73: + }
74: + // System.out.println( "- - -" );
75: + int sentinel = 1_000_000_000;
76: + int leftAddress = -1, rightAddress = -1, targetAddress = -1,
77: + result = -1, leftOperand = -1, rightOperand = -1;
78: + String opCode;
79: + while ( sentinel > 0 && pcounter < memory.length )
80: + {
81: + sentinel -= 1;
82: + opCode = memory[ pcounter ];
83: + if ( opCode.equals( "99" ) )
84: + {
85: + // System.out.println( here +"halting" );
86: + break;
87: + }
88: + leftAddress = Integer.parseInt( memory[ pcounter + loInd ] );
89: + rightAddress = Integer.parseInt( memory[ pcounter + roInd ] );
90: + targetAddress = Integer.parseInt( memory[ pcounter + tInd ] );
91: + if ( leftAddress <= memory.length )
92: + leftOperand = Integer.parseInt( memory[ leftAddress ] );
93: + else
94: + System.out.println( here +"left address outside at "+ leftAddress );
95: + if ( rightAddress <= memory.length )
96: + rightOperand = Integer.parseInt( memory[ rightAddress ] );
97: + else
98: + System.out.println( here +"right address outside at "+ rightAddress );
99: + if ( opCode.equals( "1" ) )
100: + {
101: + result = leftOperand + rightOperand;
102: + operator = plus;
103: + }
104: + else if ( opCode.equals( "2" ) )
105: + {
106: + result = leftOperand * rightOperand;
107: + operator = times;
108: + }
109: + else
110: + {
111: + System.out.println( here +"unrecognized op code "+ opCode );
112: + }
113: + if ( targetAddress >= memory.length )
114: + {
115: + /*
116: + System.out.println( " "+ here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
117: + + rightOperand +"@"+ rightAddress +" outside to "+ targetAddress );
118: + */
119: + }
120: + else
121: + {
122: + //
123: + /*
124: + System.out.println( here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
125: + + rightOperand +"@"+ rightAddress +" to "
126: + + targetAddress +" ["+ memory[ targetAddress ] +"]" );
127: + */
128: + memory[ targetAddress ] = Integer.valueOf( result ).toString();
129: + }
130: + pcounter += nextInd;
131: + }
132: +
133: + System.out.println( "\t"+ here +"first opcode is "+ memory[ 0 ] );
134: + if ( "19690720".equals( memory[ 0 ] ) )
135: + {
136: + break entireRun;
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: +
166: +
167: +
168: +
169: diff --git a/src/res/19_02_input.txt b/src/res/19_02_input_p1.txt
170: similarity index 100%
171: rename from src/res/19_02_input.txt
172: rename to src/res/19_02_input_p1.txt
173: diff --git a/src/res/19_02_input_original.txt b/src/res/19_02_input_p2.txt
174: similarity index 100%
175: rename from src/res/19_02_input_original.txt
176: rename to src/res/19_02_input_p2.txt