Author: Nicholas Prado <nmprado@nzen.ws>
Date: Tue Dec 3 04:38:38 UTC 2019
Parent: 782c300e13844c5502e1f07ca344b234c9ddec29
Log message:
feat i191202 second day part one
Advances through addresses mutating bunches of data. Shows a log
of operations. Handles multiple files for convenience, though
the canon input is a single line.
1: diff --git a/src/java/Exercise19021.java b/src/java/Exercise19021.java
2: new file mode 100644
3: index 0000000..023047b
4: --- /dev/null
5: +++ b/src/java/Exercise19021.java
6: @@ -0,0 +1,153 @@
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 Exercise19021
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: + for ( String line : fileLines )
53: + {
54: + int pcounter = initialInd;
55: + String[] memory = line.split( "," );
56: + System.out.println( "\n\n another run" );
57: + if ( memory.length < 50 )
58: + {
59: + for ( int indP = 0; indP < memory.length; indP += 1 )
60: + {
61: + System.out.print( memory[ indP ] +", " );
62: + if ( (indP % 4) == 3 )
63: + System.out.println();
64: + }
65: + }
66: + System.out.println( "- - -" );
67: + int sentinel = 1_000_000_000;
68: + int leftAddress = -1, rightAddress = -1, targetAddress = -1,
69: + result = -1, leftOperand = -1, rightOperand = -1;
70: + String opCode;
71: + while ( sentinel > 0 && pcounter < memory.length )
72: + {
73: + sentinel -= 1;
74: + opCode = memory[ pcounter ];
75: + if ( opCode.equals( "99" ) )
76: + {
77: + System.out.println( here +"halting" );
78: + break;
79: + }
80: + leftAddress = Integer.parseInt( memory[ pcounter + loInd ] );
81: + rightAddress = Integer.parseInt( memory[ pcounter + roInd ] );
82: + targetAddress = Integer.parseInt( memory[ pcounter + tInd ] );
83: + if ( leftAddress <= memory.length )
84: + leftOperand = Integer.parseInt( memory[ leftAddress ] );
85: + else
86: + System.out.println( here +"left address outside at "+ leftAddress );
87: + if ( rightAddress <= memory.length )
88: + rightOperand = Integer.parseInt( memory[ rightAddress ] );
89: + else
90: + System.out.println( here +"right address outside at "+ rightAddress );
91: + if ( opCode.equals( "1" ) )
92: + {
93: + result = leftOperand + rightOperand;
94: + operator = plus;
95: + }
96: + else if ( opCode.equals( "2" ) )
97: + {
98: + result = leftOperand * rightOperand;
99: + operator = times;
100: + }
101: + else
102: + {
103: + System.out.println( here +"unrecognized op code "+ opCode );
104: + }
105: + if ( targetAddress >= memory.length )
106: + {
107: + System.out.println( " "+ here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
108: + + rightOperand +"@"+ rightAddress +" outside to "+ targetAddress );
109: + }
110: + else
111: + {
112: + System.out.println( here +"r "+ result +" = "+ leftOperand +"@"+ leftAddress + operator
113: + + rightOperand +"@"+ rightAddress +" to "
114: + + targetAddress +" ["+ memory[ targetAddress ] +"]" );
115: + memory[ targetAddress ] = Integer.valueOf( result ).toString();
116: + }
117: + pcounter += nextInd;
118: + }
119: +
120: + System.out.println( "\t"+ here +"first opcode is "+ memory[ 0 ] );
121: + if ( memory.length < 50 )
122: + {
123: + for ( int indP = 0; indP < memory.length; indP += 1 )
124: + {
125: + System.out.print( memory[ indP ] +", " );
126: + if ( (indP % 4) == 3 )
127: + System.out.println();
128: + }
129: + }
130: + }
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: diff --git a/src/res/19_02_input.txt b/src/res/19_02_input.txt
161: new file mode 100644
162: index 0000000..8dcc701
163: --- /dev/null
164: +++ b/src/res/19_02_input.txt
165: @@ -0,0 +1 @@
166: +1,12,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,5,23,1,6,23,27,1,27,5,31,2,31,10,35,2,35,6,39,1,39,5,43,2,43,9,47,1,47,6,51,1,13,51,55,2,9,55,59,1,59,13,63,1,6,63,67,2,67,10,71,1,9,71,75,2,75,6,79,1,79,5,83,1,83,5,87,2,9,87,91,2,9,91,95,1,95,10,99,1,9,99,103,2,103,6,107,2,9,107,111,1,111,5,115,2,6,115,119,1,5,119,123,1,123,2,127,1,127,9,0,99,2,0,14,0
167: diff --git a/src/res/19_02_input_easy.txt b/src/res/19_02_input_easy.txt
168: new file mode 100644
169: index 0000000..d2d7a1f
170: --- /dev/null
171: +++ b/src/res/19_02_input_easy.txt
172: @@ -0,0 +1,5 @@
173: +1,9,10,3,2,3,11,0,99,30,40,50
174: +1,0,0,0,99
175: +2,3,0,3,99
176: +2,4,4,5,99,0
177: +1,1,1,4,99,5,6,0,99
178: \ No newline at end of file
179: diff --git a/src/res/19_02_input_original.txt b/src/res/19_02_input_original.txt
180: new file mode 100644
181: index 0000000..bf940d6
182: --- /dev/null
183: +++ b/src/res/19_02_input_original.txt
184: @@ -0,0 +1 @@
185: +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,1,19,5,23,1,6,23,27,1,27,5,31,2,31,10,35,2,35,6,39,1,39,5,43,2,43,9,47,1,47,6,51,1,13,51,55,2,9,55,59,1,59,13,63,1,6,63,67,2,67,10,71,1,9,71,75,2,75,6,79,1,79,5,83,1,83,5,87,2,9,87,91,2,9,91,95,1,95,10,99,1,9,99,103,2,103,6,107,2,9,107,111,1,111,5,115,2,6,115,119,1,5,119,123,1,123,2,127,1,127,9,0,99,2,0,14,0