-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay3.java
69 lines (58 loc) · 1.89 KB
/
Day3.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
package com.sbaars.adventofcode.year15.days;
import com.sbaars.adventofcode.common.Direction;
import com.sbaars.adventofcode.common.location.Loc;
import com.sbaars.adventofcode.common.location.MutableLoc;
import com.sbaars.adventofcode.year15.Day2015;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import static com.sbaars.adventofcode.common.Direction.*;
import static com.sbaars.adventofcode.util.AoCUtils.zipWithIndex;
public class Day3 extends Day2015 {
public Day3() {
super(3);
}
public static void main(String[] args) {
new Day3().printParts();
}
@Override
public Object part1() {
return countVisitedHouses(dayStream()
.flatMapToInt(String::chars)
.mapToObj(c -> charToDir((char) c)));
}
@Override
public Object part2() {
return countVisitedHousesWithRobo(dayStream()
.flatMapToInt(String::chars)
.mapToObj(c -> charToDir((char) c)));
}
private long countVisitedHouses(Stream<Direction> directions) {
MutableLoc loc = new MutableLoc();
Set<Loc> visited = new HashSet<>();
visited.add(loc.get());
directions.forEach(d -> visited.add(loc.set(d.move(loc.get()))));
return visited.size();
}
private long countVisitedHousesWithRobo(Stream<Direction> directions) {
MutableLoc santa = new MutableLoc();
MutableLoc robo = new MutableLoc();
Set<Loc> visited = new HashSet<>();
visited.add(santa.get());
zipWithIndex(directions)
.forEach(d -> {
var current = d.i() % 2 == 0 ? santa : robo;
visited.add(current.set(d.e().move(current.get())));
});
return visited.size();
}
private static Direction charToDir(char c) {
return switch (c) {
case '^' -> NORTH;
case '>' -> EAST;
case 'v' -> SOUTH;
case '<' -> WEST;
default -> throw new IllegalStateException("Unknown char: " + c);
};
}
}