-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay14.java
88 lines (81 loc) · 2.7 KB
/
Day14.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
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* @author Zachary Cockshutt
* @since 2023-12-14
*/
public class Day14 extends Day
{
private static final Direction N = Direction.N, S = Direction.S,
E = Direction.E, W = Direction.W;
@Override
public String partOne() throws IOException
{
var platform = this.parsePlatform();
platform.tilt(N);
var load = platform.getLoad();
return String.valueOf(load);
}
@Override
public String partTwo() throws IOException
{
var platform = this.parsePlatform();
for (int i = 0; i < 1000; i++)
{
platform.tilt(N);
platform.tilt(W);
platform.tilt(S);
platform.tilt(E);
}
var load = platform.getLoad();
return String.valueOf(load);
}
private Platform parsePlatform() throws IOException
{
var matrix = new ArrayList<char[]>();
this.input((s) -> matrix.add(s.toCharArray()));
return new Platform(matrix.toArray(new char[matrix.size()][]));
}
private record Platform(char[][] matrix)
{
private void tilt(Direction dir)
{
var rows = IntStream.range(0, matrix.length).boxed().collect(Collectors.toCollection(ArrayList::new));
var cols = IntStream.range(0, matrix[0].length).boxed().collect(Collectors.toCollection(ArrayList::new));
if (dir == S) { Collections.reverse(rows); }
if (dir == E) { Collections.reverse(cols); }
for (int i : rows)
for (int j : cols)
if (matrix[i][j] == 'O')
{
int y = i+dir.y, x = j+dir.x;
while (y>=0 && y<matrix.length
&& x>=0 && x<matrix[0].length
&& matrix[y][x] == '.')
{
matrix[y-dir.y][x-dir.x] = '.';
matrix[y][x] = 'O';
x += dir.x;
y += dir.y;
}
}
}
private int getLoad()
{
int load = 0;
for (int i=0; i<matrix.length; i++)
for (int j=0; j<matrix[0].length; j++)
if (matrix[i][j] == 'O') { load += matrix.length-i; }
return load;
}
}
private static enum Direction
{
N(0,-1), S(0, 1), E(1, 0), W(-1,0);
private final int x, y;
Direction(int x, int y) { this.x = x; this.y = y; }
}
}