Skip to content

Commit 1c3e014

Browse files
committed
Extract code
1 parent f70cbb7 commit 1c3e014

File tree

1 file changed

+6
-34
lines changed
  • src/main/java/com/sbaars/adventofcode/year22/days

1 file changed

+6
-34
lines changed

src/main/java/com/sbaars/adventofcode/year22/days/Day9.java

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,46 +31,18 @@ public record Move(String dir, long n){}
3131

3232
@Override
3333
public Object part1() {
34-
List<Move> moves = dayStream().map(s -> readString(s, "%s %n", Move.class)).toList();
35-
Point p1 = new Point();
36-
Point p2 = new Point();
37-
HashSet<Point> vis = new HashSet<>();
38-
vis.add(p2);
39-
for(Move m : moves) {
40-
Direction dir = Direction.getByDirCode(m.dir.charAt(0));
41-
for(int i = 0; i<m.n; i++) {
42-
p1 = dir.move(p1);
43-
Point p12 = p1, p22 = p2;
44-
if(Arrays.stream(Direction.values()).noneMatch(d -> d.move(p22).equals(p12))) {
45-
if(p1.x > p2.x && p1.y == p2.y) {
46-
p2 = Direction.EAST.move(p2);
47-
} else if(p1.x < p2.x && p1.y == p2.y){
48-
p2 = Direction.WEST.move(p2);
49-
} else if(p1.x == p2.x && p1.y > p2.y) {
50-
p2 = Direction.SOUTH.move(p2);
51-
} else if(p1.x == p2.x && p1.y < p2.y){
52-
p2 = Direction.NORTH.move(p2);
53-
} else if(p1.x > p2.x && p1.y > p2.y) {
54-
p2 = Direction.SOUTHEAST.move(p2);
55-
} else if(p1.x < p2.x && p1.y < p2.y){
56-
p2 = Direction.NORTHWEST.move(p2);
57-
} else if(p1.x < p2.x && p1.y > p2.y) {
58-
p2 = Direction.SOUTHWEST.move(p2);
59-
} else if(p1.x > p2.x && p1.y < p2.y){
60-
p2 = Direction.NORTHEAST.move(p2);
61-
} else throw new IllegalStateException();
62-
}
63-
vis.add(p2);
64-
}
65-
}
66-
return vis.size();
34+
return simulateRope(1);
6735
}
6836

6937
@Override
7038
public Object part2() {
39+
return simulateRope(9);
40+
}
41+
42+
private int simulateRope(int size) {
7143
List<Move> moves = dayStream().map(s -> readString(s, "%s %n", Move.class)).toList();
7244
Point p1 = new Point();
73-
List<Point> p2 = new java.util.ArrayList<>(IntStream.range(0, 9).mapToObj(i -> new Point()).toList());
45+
List<Point> p2 = new java.util.ArrayList<>(IntStream.range(0, size).mapToObj(i -> new Point()).toList());
7446
HashSet<Point> vis = new HashSet<>();
7547
vis.add(p1);
7648

0 commit comments

Comments
 (0)