-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2017_19.java
106 lines (92 loc) 路 3.51 KB
/
AoC2017_19.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import static com.github.pareronia.aoc.Utils.toAString;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import com.github.pareronia.aoc.CharGrid;
import com.github.pareronia.aoc.Grid.Cell;
import com.github.pareronia.aocd.Aocd;
import com.github.pareronia.aocd.Puzzle;
public final class AoC2017_19 extends AoCBase {
private static final char CROSSING = '+';
private static final char HORIZONTAL = '-';
private static final char VERTICAL = '|';
private static final char EMPTY = ' ';
private final CharGrid grid;
private AoC2017_19(final List<String> inputs, final boolean debug) {
super(debug);
this.grid = new CharGrid(inputs);
}
public static AoC2017_19 create(final List<String> input) {
return new AoC2017_19(input, false);
}
public static AoC2017_19 createDebug(final List<String> input) {
return new AoC2017_19(input, true);
}
private Stream<Cell> getPath() {
final Deque<Cell> seen = new ArrayDeque<>();
final Cell start = Cell.at(0, this.grid.getRowAsString(0).indexOf(VERTICAL));
seen.addLast(start);
log(start);
Stream<Cell> stream = this.grid.getCellsS(start);
while (true) {
stream
.peek(c -> log(c))
.takeWhile(c -> this.grid.getValue(c) != EMPTY)
.forEach(seen::addLast);
final Cell last = seen.pollLast();
final Cell prev = seen.peekLast();
seen.addLast(last);
final Optional<Cell> _next = this.grid.getCapitalNeighbours(last)
.filter(c -> !c.equals(prev))
.filter(c -> this.grid.getValue(c) != EMPTY)
.findFirst();
if (_next.isEmpty()) {
break;
}
final Cell next = _next.get();
seen.addLast(next);
log(next);
if (next.getCol() < last.getCol()) {
stream = this.grid.getCellsW(next);
} else if (next.getCol() > last.getCol()) {
stream = this.grid.getCellsE(next);
} else if (next.getRow() < last.getRow()) {
stream = this.grid.getCellsN(next);
} else {
stream = this.grid.getCellsS(next);
}
}
return seen.stream();
}
@Override
public String solvePart1() {
return getPath()
.map(this.grid::getValue)
.filter(ch -> !Set.of(CROSSING, VERTICAL, HORIZONTAL).contains(ch))
.collect(toAString());
}
@Override
public Integer solvePart2() {
return (int) getPath().count();
}
public static void main(final String[] args) throws Exception {
assert AoC2017_19.createDebug(TEST).solvePart1().equals("ABCDEF");
assert AoC2017_19.createDebug(TEST).solvePart2().equals(38);
final Puzzle puzzle = Aocd.puzzle(2017, 19);
puzzle.check(
() -> lap("Part 1", () -> AoC2017_19.create(puzzle.getInputData()).solvePart1()),
() -> lap("Part 2", () -> AoC2017_19.create(puzzle.getInputData()).solvePart2())
);
}
private static final List<String> TEST = splitLines(
" | \r\n" +
" | +--+ \r\n" +
" A | C \r\n" +
" F---|----E|--+ \r\n" +
" | | | D \r\n" +
" +B-+ +--+ "
);
}