-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8.java
More file actions
133 lines (106 loc) · 3.14 KB
/
Day8.java
File metadata and controls
133 lines (106 loc) · 3.14 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.ArrayList;
import java.util.List;
public class Day8 {
private static final char ON = '#';
private static final char OFF = ' ';
private static final char RECT = 'T';
private static final char ROTX = 'X';
private static final char ROTY = 'Y';
static class Command {
char op;
int a;
int b;
Command(char op, int a, int b) {
this.op = op;
this.a = a;
this.b = b;
}
}
static int part1(List<String> input) {
List<Command> commands = convert(input);
char[][] grid = createGrid(50, 6);
char[] _tmp = new char[grid[0].length];
for (Command command : commands) {
if (command.op == RECT) {
for (int y = 0; y < command.b; y++) {
for (int x = 0; x < command.a; x++) {
grid[y][x] = ON;
}
}
}
else if (command.op == ROTX) {
int x = command.a;
int offset = command.b;
for (int y = 0; y < grid.length; y++) {
_tmp[y] = grid[y][x];
}
for (int y = 0; y < grid.length; y++) {
grid[(y + offset) % grid.length][x] = _tmp[y];
}
}
else if (command.op == ROTY) {
int y = command.a;
int offset = command.b;
System.arraycopy(grid[y], grid[y].length - offset, _tmp, 0, offset);
System.arraycopy(grid[y], 0, grid[y], offset, grid[y].length - offset);
System.arraycopy(_tmp, 0, grid[y], 0, offset);
}
}
int countOn = 0;
for (char[] y : grid) {
for (char x : y) {
if (x == ON) {
countOn++;
}
}
}
System.out.println(printGrid(grid));
return countOn;
}
private static String printGrid(char[][] grid) {
StringBuilder b = new StringBuilder();
for (char[] y : grid) {
for (char x : y) {
b.append(x);
}
b.append('\n');
}
return b.toString();
}
private static char[][] createGrid(int xSize, int ySize) {
char[][] grid = new char[ySize][xSize];
for (int y = 0; y < ySize; y++) {
for (int x = 0; x < xSize; x++) {
grid[y][x] = OFF;
}
}
return grid;
}
private static List<Command> convert(List<String> input) {
List<Command> commands = new ArrayList<>(input.size());
for (String instruction : input) {
String[] inst = instruction.split(" ");
String command = inst[0];
if (command.equals("rect")) {
int indexX = inst[1].indexOf('x');
commands.add(new Command(RECT, Integer.parseInt(inst[1].substring(0, indexX)),
Integer.parseInt(inst[1].substring(indexX + 1))));
}
else if (inst[1].equals("row")) {
int row = Integer.parseInt(inst[2].substring(2));
int by = Integer.parseInt(inst[4]);
commands.add(new Command(ROTY, row, by));
}
else if (inst[1].equals("column")) {
int col = Integer.parseInt(inst[2].substring(2));
int by = Integer.parseInt(inst[4]);
commands.add(new Command(ROTX, col, by));
}
}
return commands;
}
public static void main(String[] args) {
List<String> input = Util.readInput("day8.input");
System.out.println(part1(input));
}
}