-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHolidayCode12.java
More file actions
166 lines (124 loc) · 3.65 KB
/
HolidayCode12.java
File metadata and controls
166 lines (124 loc) · 3.65 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
127
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
161
162
163
164
165
166
import java.io.*;
import java.util.*;
public class HolidayCode12 {
public static boolean isParsable(String input){
boolean parsable = true;
try{
Integer.parseInt(input);
}catch(NumberFormatException e){
parsable = false;
}
return parsable;
}
public static void main(String[] args){
try{
File file = new File("assembunny.txt");
FileReader fileReader = new FileReader(file);
BufferedReader input = new BufferedReader(fileReader);
ArrayList<String> instructions = new ArrayList<String>();
String line;
while((line = input.readLine()) != null){
instructions.add(line);
}
//Now execute
int a = 0;
int b = 0;
int c = 1;
int d = 0;
for(int i = 0; i < instructions.size(); i++){
String current = instructions.get(i);
String[] parts = current.split(" ");
//System.out.println(parts.toString());
if(parts[0].equals("cpy")){
String name = parts[2];
if(isParsable(parts[1])){
if(name.equals("a")){
a = Integer.parseInt(parts[1]);
} else if(name.equals("b")){
b = Integer.parseInt(parts[1]);
} else if(name.equals("c")){
c = Integer.parseInt(parts[1]);
} else {
d = Integer.parseInt(parts[1]);
}
} else {
String copyFrom = parts[1];
int value = 0;
if(copyFrom.equals("a")){
value = a;
} else if(copyFrom.equals("b")){
value = b;
} else if(copyFrom.equals("c")){
value = c;
} else {
value = d;
}
if(name.equals("a")){
a = value;
} else if(name.equals("b")){
b = value;
} else if(name.equals("c")){
c = value;
} else {
d = value;
}
}
} else if(parts[0].equals("inc")){
String name = parts[1];
if(name.equals("a")){
a++;
} else if(name.equals("b")){
b++;
} else if(name.equals("c")){
c++;
} else {
d++;
}
} else if(parts[0].equals("dec")){
String name = parts[1];
if(name.equals("a")){
a--;
} else if(name.equals("b")){
b--;
} else if(name.equals("c")){
c--;
} else {
d--;
}
} else {
//Jump
String name = parts[1];
boolean notZero = true;
if(name.equals("a")){
if(a == 0){
notZero = false;
}
} else if(name.equals("b")){
if(b == 0){
notZero = false;
}
} else if(name.equals("c")){
if(c == 0){
notZero = false;
}
} else {
if(d == 0){
notZero = false;
}
}
if(notZero){
int jumpDistance = Integer.parseInt(parts[2]);
jumpDistance -= 1;
i += jumpDistance;
}
}
}
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
} catch (IOException error) { //Need this for using buffered readers
error.printStackTrace();
}
}
}