-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday08_draw_screen.dart
160 lines (137 loc) · 3.62 KB
/
day08_draw_screen.dart
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import 'dart:io';
import 'dart:math';
// ########
// SOLUTION
// ########
class Screen {
int rows;
int columns;
late Map<Point, String> _grid;
Screen(this.columns, this.rows) {
this._grid = {};
for (int x = 0; x < this.columns; x++) {
for (int y = 0; y < this.rows; y++) {
this._grid[Point(x, y)] = " ";
}
}
}
Map<Point, String> get grid {
return this._grid;
}
bool draw(int x, int y, String value) {
try {
this._grid[Point(x, y)] = value;
} catch (RangeError) {
return false;
}
return true;
}
int countLitPixels() {
int counter = 0;
for (int y = 0; y < this.rows; y++) {
String output = "";
for (int x = 0; x < this.columns; x++) {
if (this._grid[Point(x, y)]! == "#") counter++;
}
}
return counter;
}
void writeToScreen() {
for (int y = 0; y < this.rows; y++) {
String output = "";
for (int x = 0; x < this.columns; x++) {
output += this._grid[Point(x, y)]!;
}
print(output);
}
print("");
}
}
abstract class Command {
void draw(Screen screen) {}
}
class RectangleCommand extends Command {
int rows;
int columns;
RectangleCommand(this.columns, this.rows);
void draw(Screen screen) {
for (int x = 0; x < this.columns; x++) {
for (int y = 0; y < this.rows; y++) {
screen.draw(x, y, "#");
}
}
}
}
class RotationCommand extends Command {
String type;
int axisValueToRotate;
int rotationDistance;
RotationCommand(this.type, this.axisValueToRotate, this.rotationDistance);
void draw(screen) {
// get points on screen
List<String> pixels = [];
if (this.type == "column") {
for (int y = 0; y < screen.rows; y++) {
pixels.add(screen.grid[Point(this.axisValueToRotate, y)]!);
}
} else {
for (int x = 0; x < screen.columns; x++) {
pixels.add(screen.grid[Point(x, this.axisValueToRotate)]!);
}
}
// rotate
var updatedPixels = List.from(pixels);
for (int i = 0; i < pixels.length; i++) {
var indexToUpdate = (i + this.rotationDistance) % pixels.length;
updatedPixels[indexToUpdate] = pixels[i];
}
// place back on screen
if (this.type == "column") {
for (int y = 0; y < pixels.length; y++) {
screen.draw(this.axisValueToRotate, y, updatedPixels[y]);
}
} else {
for (int x = 0; x < pixels.length; x++) {
screen.draw(x, this.axisValueToRotate, updatedPixels[x]);
}
}
}
}
List<Command> parseInput(String puzzleInput) {
return puzzleInput.trim().split("\n").map((line) {
var parts = line.split(" ");
if (parts[0] == "rect") {
var dimension = parts[1].split("x");
return RectangleCommand(int.parse(dimension[0]), int.parse(dimension[1]));
}
var stepSize = parts[2].split("=");
return RotationCommand(
parts[1],
int.parse(stepSize[1]),
int.parse(parts[4]),
);
}).toList();
}
int solveProblem(puzzleInput) {
var commands = parseInput(puzzleInput);
var screen = new Screen(50, 6);
for (final command in commands) {
command.draw(screen);
}
screen.writeToScreen();
return screen.countLitPixels();
}
// ###########
// RUN PROGRAM
// ###########
void main() {
String puzzleInput = File('data/day08_input.txt').readAsStringSync();
String TEST_INPUT = File('data/day08_test.txt').readAsStringSync();
// part 1
assert(solveProblem(TEST_INPUT) == 6);
final stopwatchPart1 = Stopwatch()..start();
print("part 1: ${solveProblem(puzzleInput)}");
stopwatchPart1.stop();
print("Elapsed time: ${stopwatchPart1.elapsed}");
// part 2 is drawn to the screen
}