-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay8.java
138 lines (111 loc) · 3.66 KB
/
Day8.java
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
package aoc17;
import java.io.File;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Queue;
import myutils17.Command;
import myutils17.DecreaseCommand;
import myutils17.IncreaseCommand;
import java.util.LinkedList;
public class Day8 {
private List<String> rawInstructions;
private Map<String, Integer> registers;
private int maxValueInRegisters;
public Day8(File input) {
rawInstructions = getInstructions(input);
registers = getRegisters();
maxValueInRegisters = Integer.MIN_VALUE;
}
// part 1
public int run() {
Queue<Command> commands = getCommandQueue();
for(Command command : commands) {
int registerValue = command.execute();
if(registerValue > maxValueInRegisters)
maxValueInRegisters = registerValue;
}
return registers.entrySet().stream().max(Map.Entry.comparingByValue()).get().getValue();
}
private Queue<Command> getCommandQueue()
{
Queue<Command> commands = new LinkedList<Command>();
for(String instruction : rawInstructions) {
String reg1 = "";
String reg2 = "";
int index1 = 0;
while (instruction.charAt(index1) != ' ') {
reg1 += instruction.charAt(index1++);
}
int index2 = instruction.indexOf("if") + 3;
while(instruction.charAt(index2) != ' ') {
reg2 += instruction.charAt(index2++);
}
String value1 = "";
int indexOfValue1 = instruction.indexOf("if") - 2;
while(instruction.charAt(indexOfValue1) != ' ') {
value1 = instruction.charAt(indexOfValue1--) + value1;
}
int value1num = Integer.parseInt(value1);
String value2 = "";
int indexOfValue2 = instruction.length() - 1;
while(instruction.charAt(indexOfValue2) != ' ') {
value2 = instruction.charAt(indexOfValue2--) + value2;
}
int value2num = Integer.parseInt(value2);
String condition = "";
int conditionIndex = indexOfValue2 - 1;
while(instruction.charAt(conditionIndex) != ' ') {
condition = instruction.charAt(conditionIndex--) + condition;
}
//System.out.println(reg1 + " " + reg2 + " " +value1num + " " + value2num + " " + condition);
if(instruction.contains("dec")) {
commands.offer(new DecreaseCommand(reg1, reg2, value1num, value2num, registers, condition));
}
else
commands.offer(new IncreaseCommand(reg1, reg2, value1num, value2num, registers, condition));
}
return commands;
}
private Map<String, Integer> getRegisters() {
registers = new HashMap<String, Integer>();
for (String instruction : rawInstructions) {
String reg1 = "";
String reg2 = "";
int index1 = 0;
while (instruction.charAt(index1) != ' ') {
reg1 += instruction.charAt(index1++);
}
int index2 = instruction.indexOf("if") + 1;
while(instruction.charAt(index2) != ' ') {
reg2 += instruction.charAt(index2++);
}
registers.putIfAbsent(reg1, 0);
registers.putIfAbsent(reg2, 0);
}
return registers;
}
private List<String> getInstructions(File input) {
List<String> instructions = new ArrayList<>();
try {
BufferedReader br = new BufferedReader(new FileReader(input));
String line = "";
while ((line = br.readLine()) != null) {
instructions.add(line);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return instructions;
}
public static void main(String[] args) {
Day8 test = new Day8(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2017\\Day 8\\InputFile1.txt"));
System.out.println(test.run() + " " + test.maxValueInRegisters);
}
}