-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart1.java
More file actions
76 lines (59 loc) · 2.45 KB
/
Copy pathPart1.java
File metadata and controls
76 lines (59 loc) · 2.45 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
package y2016.d10;
import common.Files;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.IntBinaryOperator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Part1 {
private static HashMap<Integer, Bot> botMap = new HashMap<>();
private static HashMap<Integer, Output> outputMap = new HashMap<>();
public static void main(String[] args) throws FileNotFoundException {
ArrayList<String> in = Files.readByLines("src/main/java/y2016/d10/in.txt");
Pattern pattern1 = Pattern.compile("value (\\d+) goes to bot (\\d+)");
Pattern pattern2 = Pattern.compile("bot (\\d+) gives low to (bot|output) (\\d+) and high to (bot|output) (\\d+)");
for (String line: in) {
Matcher m = pattern1.matcher(line);
if (m.find()) {
System.out.println("value " + line);
getBot(Integer.parseInt(m.group(2))).setValue(Integer.parseInt(m.group(1)));
continue;
}
m = pattern2.matcher(line);
if (m.find()) {
System.out.println("command " + line);
Receiver lowReceiver = getReceiver(m.group(2), m.group(3));
Receiver highReceiver = getReceiver(m.group(4), m.group(5));
getBot(Integer.parseInt(m.group(1))).setHighReceiver(highReceiver);
getBot(Integer.parseInt(m.group(1))).setLowReceiver(lowReceiver);
continue;
}
System.out.println("NOTHING!!! " + line);
}
int out = getOutput(0).getValue();
out *= getOutput(1).getValue();
out *= getOutput(2).getValue();
System.out.println(out);
}
private static Receiver getReceiver(String type, String id) {
if ("bot".equals(type)) {
return getBot(Integer.parseInt(id));
} else if ("output".equals(type)) {
return getOutput(Integer.parseInt(id));
}
throw new RuntimeException("no dude, just no!");
}
private static Bot getBot(Integer botId) {
if (!botMap.containsKey(botId)) {
botMap.put(botId, new Bot(botId));
}
return botMap.get(botId);
}
private static Output getOutput(Integer outputId) {
if (!outputMap.containsKey(outputId)) {
outputMap.put(outputId, new Output(outputId));
}
return outputMap.get(outputId);
}
}