-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventofcodeDay8Part2.java
More file actions
128 lines (108 loc) · 3.91 KB
/
Copy pathadventofcodeDay8Part2.java
File metadata and controls
128 lines (108 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File; // Import the File class
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.*;
import java.util.Set;
import java.util.Map;
import java.util.LinkedList;
import java.util.Queue;
public class adventofcodeDay8Part2{
public static class Node{
public String operation;
public int argument ;
public String getAction() {return operation;}
public int getAmount() {return argument;}
public Node(String v , int w){
operation=v;
argument=w;
}
}
public static void rm(Stack t){
t.pop();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("input.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
String [] tokens;
int accumulator = 0;
String operation;
int argument;
int index;
int goestoIndex;
Set<Integer> doneIndex = new HashSet<>();
Stack<Integer> loopIndex = new Stack<Integer>();
ArrayList<Node> instructions = new ArrayList<>();
// Read lines to an Array
while( (line = br.readLine()) != null ){
tokens = line.split(" ");
instructions.add(new Node(tokens[0], Integer.parseInt(tokens[1])));
}
int instructionsSize = instructions.size();
// Find the index of operation to change
for( index = instructionsSize-1; index>=0; index--){
operation = instructions.get(index).operation;
argument = instructions.get(index).argument;
if(operation.equals("jmp") && argument<0){
loopIndex.push(index);
}
if(operation.equals("jmp") && argument>0){
goestoIndex = index+argument;
while ( !loopIndex.empty() ){
System.out.println(loopIndex);
System.out.println(loopIndex.peek());
System.out.println(goestoIndex);
if(loopIndex.peek() >= goestoIndex) break;
loopIndex.pop();
}
}
}
System.out.println(loopIndex);
if(loopIndex.size() == 1){
instructions.set(loopIndex.peek(), new Node("nop", instructions.get(loopIndex.peek()).argument));
}
else{
int minIndex = loopIndex.empty() ? 0 : loopIndex.pop();
int maxIndex = minIndex;
while (!loopIndex.empty()){
maxIndex = loopIndex.pop();
}
for( index = maxIndex; index >=0; index--){
operation = instructions.get(index).operation;
argument = instructions.get(index).argument;
if(operation.equals("nop") && argument+index > maxIndex){
instructions.set(index, new Node("jmp", instructions.get(index).argument));
break;
}
}
}
index = 0;
while( !doneIndex.contains(index)){
// System.out.println(index);
if (index > instructionsSize-1 ) break;
doneIndex.add(index);
operation = instructions.get(index).operation;
argument = instructions.get(index).argument;
if(operation.equals("acc")){
accumulator = accumulator + argument;
index = index + 1;
}
else if(operation.equals("nop")){
index = index + 1;
}
else if(operation.equals("jmp")){
index = index + argument;
}
}
System.out.println(index);
System.out.println(accumulator);
br.close();
}
}