Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String fileName = "input.txt";
- //String fileName = "testdata.txt";
- BufferedReader reader;
- String line;
- IntList values;
- void setup() {
- reader = createReader(fileName);
- try {
- line = reader.readLine();
- }
- catch (IOException e) {
- line = null;
- }
- values = new IntList();
- String[] listSplit = split(line, ',');
- for( int value : int(listSplit) ) {
- values.append(value);
- }
- /* day 2 _ 1 - answer 4138658
- values.set(1, 12);
- values.set(2, 2);
- int i = 0;
- int Opcode = values.get(i);
- while(Opcode != 99) {
- switch(Opcode) {
- case 1:
- values.set(values.get(i+3), operation(values.get(i+1), values.get(i+2), Opcode));
- i = i+4;
- Opcode = values.get(i);
- break;
- case 2:
- values.set(values.get(i+3), operation(values.get(i+1), values.get(i+2), Opcode));
- i = i+4;
- Opcode = values.get(i);
- break;
- }
- }
- println(values.get(0));
- */
- // day 2 _ 2 input answer 7264
- boolean stop = false;
- int noun = 0;
- int verb = 0;
- int output = 0;
- int expectedOutput = 19690720;
- int result = 0;
- int Opcode = 0;
- while(output != expectedOutput && !stop ) {
- IntList copyOfValues = new IntList();
- for( int value : values) {
- copyOfValues.append(value);
- }
- int i = 0;
- Opcode = copyOfValues.get(i);
- copyOfValues.set(1, noun);
- copyOfValues.set(2, verb);
- while(Opcode != 99) {
- switch(Opcode) {
- case 1:
- copyOfValues.set(copyOfValues.get(i+3), copyOfValues.get(copyOfValues.get(i+1)) + copyOfValues.get(copyOfValues.get(i+2)));
- i = i+4;
- Opcode = copyOfValues.get(i);
- break;
- case 2:
- copyOfValues.set(copyOfValues.get(i+3), copyOfValues.get(copyOfValues.get(i+1)) * copyOfValues.get(copyOfValues.get(i+2)));
- i = i+4;
- Opcode = copyOfValues.get(i);
- break;
- }
- }
- result = 100*noun+verb;
- if(noun < 99) {
- noun++;
- } else {
- verb++;
- noun = 0;
- }
- if (verb > 99) {
- stop = true;
- }
- output = copyOfValues.get(0);
- }
- println(result);
- }
- int operation(int pos1, int pos2, int Opcode){
- switch(Opcode) {
- case 1:
- return values.get(pos1) + values.get(pos2);
- case 2:
- return values.get(pos1) * values.get(pos2);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement