-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathDay18.java
62 lines (53 loc) · 1.92 KB
/
Day18.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
package com.sbaars.adventofcode.year23.days;
import com.sbaars.adventofcode.common.Direction;
import com.sbaars.adventofcode.common.grid.InfiniteGrid;
import com.sbaars.adventofcode.common.location.Loc;
import com.sbaars.adventofcode.year23.Day2023;
import java.util.ArrayList;
import java.util.List;
import static com.sbaars.adventofcode.common.Direction.*;
import static com.sbaars.adventofcode.util.AoCUtils.connectedPairs;
import static com.sbaars.adventofcode.util.DataMapper.readString;
public class Day18 extends Day2023 {
public Day18() {
super(18);
}
public static void main(String[] args) {
new Day18().printParts();
}
public record Dig(Direction dir, int n, long part2N, Direction part2Dir) {
public Dig(char dir, int n, String hex) {
this(Direction.getByDirCode(dir), n, Long.parseLong(hex.substring(0, hex.length() - 1), 16), hex.charAt(hex.length() - 1) == '0' ? EAST : hex.charAt(hex.length() - 1) == '1' ? SOUTH : hex.charAt(hex.length() - 1) == '2' ? WEST : NORTH);
}
}
@Override
public Object part1() {
var in = input();
Loc start = new Loc(0, 0);
InfiniteGrid grid = new InfiniteGrid();
for (Dig dig : in) {
Loc end = dig.dir.move(start, dig.n);
grid.draw(start, end, '#');
start = end;
}
grid.floodFill(new Loc(1, 1), c -> c != '#').forEach(l -> grid.set(l, '#'));
return grid.grid.size();
}
@Override
public Object part2() {
var in = input();
Loc start = new Loc(0, 0);
List<Loc> route = new ArrayList<>();
long outline = 0;
for (Dig dig : in) {
Loc end = dig.part2Dir.move(start, dig.part2N);
outline += start.distance(end);
route.add(end);
start = end;
}
return outline / 2 + connectedPairs(route).mapToLong(p -> (p.a().y + p.b().y) * (p.a().x - p.b().x)).sum() / 2 + 1;
}
private List<Dig> input() {
return dayStream().map(s -> readString(s, "%c %i (#%s)", Dig.class)).toList();
}
}