Skip to content

Commit f850919

Browse files
committed
java/2016/22: add solution for part 1
1 parent a9127b9 commit f850919

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

java/src/main/java/com/github/saser/adventofcode/year2016/day22/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ package(default_visibility = ["//visibility:public"])
33
java_library(
44
name = "day22",
55
srcs = glob(["*.java"]),
6-
deps = ["//java/src/main/java/com/github/saser/adventofcode:adventofcode"],
6+
deps = [
7+
"//java/src/main/java/com/github/saser/adventofcode/geo:geo",
8+
"//java/src/main/java/com/github/saser/adventofcode:adventofcode",
9+
],
710
)

java/src/main/java/com/github/saser/adventofcode/year2016/day22/Day22.java

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.github.saser.adventofcode.year2016.day22;
22

3+
import java.io.BufferedReader;
34
import java.io.Reader;
4-
import java.util.stream.Stream;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Set;
8+
import java.util.regex.Pattern;
9+
import java.util.stream.Collectors;
510

611
import com.github.saser.adventofcode.Result;
12+
import com.github.saser.adventofcode.geo.Point2D;
713

814
public final class Day22 {
915
public static Result part1(Reader r) {
@@ -15,6 +21,81 @@ public static Result part2(Reader r) {
1521
}
1622

1723
private static Result solve(Reader r, int part) {
18-
return Result.err("not implemented yet");
24+
var grid = Grid.parse(r);
25+
var count = grid.findViable().size();
26+
return Result.ok(Integer.toString(count));
27+
}
28+
29+
private static class Node {
30+
public final int used;
31+
public final int available;
32+
33+
public Node(int used, int available) {
34+
this.used = used;
35+
this.available = available;
36+
}
37+
38+
public static boolean viable(Node a, Node b) {
39+
if (a.used == 0) {
40+
return false;
41+
}
42+
return a.used <= b.available;
43+
}
44+
}
45+
46+
private static class Grid {
47+
public final Map<Point2D, Node> nodes;
48+
49+
public Grid(Map<Point2D, Node> nodes) {
50+
this.nodes = nodes;
51+
}
52+
53+
public static Grid parse(Reader r) {
54+
var re = Pattern.compile("/dev/grid/node-x(\\d+)-y(\\d+)\\s+(\\d+)T\\s+(\\d+)T\\s+(\\d+)T\\s+(\\d+)%");
55+
var nodes = new HashMap<Point2D, Node>();
56+
var maxX = 0;
57+
var maxY = 0;
58+
var it = new BufferedReader(r)
59+
.lines()
60+
.iterator();
61+
while (it.hasNext()) {
62+
var line = it.next();
63+
var matcher = re.matcher(line);
64+
if (!matcher.matches()) {
65+
continue;
66+
}
67+
var x = Integer.parseInt(matcher.group(1));
68+
maxX = Math.max(maxX, x);
69+
var y = Integer.parseInt(matcher.group(2));
70+
maxY = Math.max(maxY, y);
71+
var point = new Point2D(x, y);
72+
var used = Integer.parseInt(matcher.group(4));
73+
var available = Integer.parseInt(matcher.group(5));
74+
nodes.put(point, new Node(used, available));
75+
}
76+
return new Grid(nodes);
77+
}
78+
79+
private Point2D findEmpty() {
80+
return this.nodes
81+
.entrySet()
82+
.stream()
83+
.filter(entry -> entry.getValue().used == 0)
84+
.map(Map.Entry::getKey)
85+
.findFirst()
86+
.get();
87+
}
88+
89+
public Set<Point2D> findViable() {
90+
var empty = this.findEmpty();
91+
var emptyNode = this.nodes.get(empty);
92+
return this.nodes
93+
.entrySet()
94+
.stream()
95+
.filter(entry -> !entry.getKey().equals(empty))
96+
.filter(entry -> Node.viable(entry.getValue(), emptyNode))
97+
.map(Map.Entry::getKey)
98+
.collect(Collectors.toSet());
99+
}
19100
}
20101
}

java/src/test/java/com/github/saser/adventofcode/year2016/day22/Day22Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class Day22Test {
1010
@Test
1111
public void part1Actual() throws IOException {
1212
try (var input = new FileReader("inputs/2016/22")) {
13-
var output = "";
13+
var output = "888";
1414
var result = Day22.part1(input);
1515
Assert.assertEquals("no error", "", result.error);
1616
Assert.assertEquals("correct output", output, result.answer);

0 commit comments

Comments
 (0)