-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventofcodeDay8Part1.java
More file actions
79 lines (63 loc) · 2.11 KB
/
Copy pathadventofcodeDay8Part1.java
File metadata and controls
79 lines (63 loc) · 2.11 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
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.Set;
import java.util.Map;
import java.util.LinkedList;
import java.util.Queue;
public class adventofcodeDay8Part1{
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 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;
ArrayList<Node> instructions = new ArrayList<>();
// directedGraph g = new directedGraph();
while( (line = br.readLine()) != null ){
tokens = line.split(" ");
instructions.add(new Node(tokens[0], Integer.parseInt(tokens[1])));
}
int accumulator = 0;
int index = 0;
String operation;
int argument;
Set<Integer> doneIndex = new HashSet<>();
while( !doneIndex.contains(index)){
// System.out.println(index);
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(index);
System.out.println(accumulator);
br.close();
}
}