Skip to content

Commit d06f615

Browse files
committed
refactoring
1 parent 8944483 commit d06f615

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

src/main/java/com/sbaars/adventofcode/year15/days/Day1.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ public static void main(String[] args) {
1616

1717
@Override
1818
public Object part1() {
19-
String in = day().trim();
20-
return in.replace(")", "").length() - in.replace("(", "").length();
19+
return dayStream()
20+
.flatMapToInt(String::chars)
21+
.map(c -> c == '(' ? 1 : -1)
22+
.sum();
2123
}
2224

2325
@Override
2426
public Object part2() {
25-
String in = day().trim();
2627
return findReduce(
27-
zipWithIndex(in.chars().boxed()),
28+
zipWithIndex(dayStream().flatMapToInt(String::chars).boxed()),
2829
0,
2930
(c, acc) -> acc + (c.e() == '(' ? 1 : -1),
3031
acc -> acc < 0

src/main/java/com/sbaars/adventofcode/year15/days/Day2.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ private long[] smallestSides() {
2020
}
2121

2222
private long smallestArea() {
23-
long[] s = smallestSides();
23+
var s = smallestSides();
2424
return s[0] * s[1];
2525
}
2626

2727
private long area() {
28-
return 2 * l * w + 2 * w * h + 2 * h * l;
28+
return 2 * (l * w + w * h + h * l);
2929
}
3030

3131
private long wrappingPaper() {
3232
return smallestArea() + area();
3333
}
3434

3535
private long sideDistance() {
36-
long[] s = smallestSides();
37-
return s[0] * 2 + s[1] * 2;
36+
var s = smallestSides();
37+
return 2 * (s[0] + s[1]);
3838
}
3939

4040
private long volume() {
@@ -64,7 +64,6 @@ public Object part2() {
6464

6565
private long getResult(ToLongFunction<Dimension> func) {
6666
return dayStream()
67-
.map(String::trim)
6867
.map(s -> readString(s, "%nx%nx%n", Dimension.class))
6968
.mapToLong(func)
7069
.sum();

src/main/java/com/sbaars/adventofcode/year15/days/Day3.java

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.sbaars.adventofcode.year15.days;
22

33
import com.sbaars.adventofcode.common.Direction;
4+
import com.sbaars.adventofcode.common.location.Loc;
45
import com.sbaars.adventofcode.common.location.MutableLoc;
56
import com.sbaars.adventofcode.year15.Day2015;
67

8+
import java.util.HashSet;
9+
import java.util.Set;
10+
import java.util.stream.Stream;
11+
712
import static com.sbaars.adventofcode.common.Direction.*;
813
import static com.sbaars.adventofcode.util.AoCUtils.zipWithIndex;
914

@@ -18,26 +23,41 @@ public static void main(String[] args) {
1823

1924
@Override
2025
public Object part1() {
21-
MutableLoc loc = new MutableLoc();
22-
return day()
23-
.chars()
24-
.mapToObj(c -> charToDir((char) c))
25-
.map(d -> loc.set(d.move(loc.get())))
26-
.distinct()
27-
.count();
26+
return countVisitedHouses(dayStream()
27+
.flatMapToInt(String::chars)
28+
.mapToObj(c -> charToDir((char) c)));
2829
}
2930

3031
@Override
3132
public Object part2() {
33+
return countVisitedHousesWithRobo(dayStream()
34+
.flatMapToInt(String::chars)
35+
.mapToObj(c -> charToDir((char) c)));
36+
}
37+
38+
private long countVisitedHouses(Stream<Direction> directions) {
39+
MutableLoc loc = new MutableLoc();
40+
Set<Loc> visited = new HashSet<>();
41+
visited.add(loc.get());
42+
directions.forEach(d -> visited.add(loc.set(d.move(loc.get()))));
43+
return visited.size();
44+
}
45+
46+
private long countVisitedHousesWithRobo(Stream<Direction> directions) {
3247
MutableLoc santa = new MutableLoc();
3348
MutableLoc robo = new MutableLoc();
34-
return zipWithIndex(day().chars().mapToObj(c -> charToDir((char) c)))
35-
.map(d -> d.i() % 2 == 0 ? santa.set(d.e().move(santa.get())) : robo.set(d.e().move(robo.get())))
36-
.distinct()
37-
.count();
49+
Set<Loc> visited = new HashSet<>();
50+
visited.add(santa.get());
51+
52+
zipWithIndex(directions)
53+
.forEach(d -> {
54+
var current = d.i() % 2 == 0 ? santa : robo;
55+
visited.add(current.set(d.e().move(current.get())));
56+
});
57+
return visited.size();
3858
}
3959

40-
public Direction charToDir(char c) {
60+
private static Direction charToDir(char c) {
4161
return switch (c) {
4262
case '^' -> NORTH;
4363
case '>' -> EAST;

0 commit comments

Comments
 (0)