-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_08b.cpp
130 lines (122 loc) · 4.3 KB
/
day_08b.cpp
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
#include <array>
#include <fstream>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <ranges>
#include <string_view>
#include <algorithm>
#include <unordered_set>
#include <cassert>
constexpr std::size_t rows = 6;
constexpr std::size_t cols = 50;
constexpr std::size_t letter_width = 5;
constexpr std::size_t letter_height = 6;
enum class Instruction{
RECT, ROTATE_ROW, ROTATE_COL
};
struct Parsed {
Instruction instr;
int dim_1;
int dim_2;
};
Parsed parse_input(const std::string& line) {
Parsed p;
if (line[1] == 'e') {
const auto idx = line.find('x');
p.instr = Instruction::RECT;
// std::cout << '|' << line.substr(5, idx - 5) << '|' <<'\n';
p.dim_1 = std::stoi(line.substr(5, idx - 5));
// std::cout << '|' << line.substr(idx+1, line.size() - idx) << '|' <<'\n';
p.dim_2 = std::stoi(line.substr(idx+1, line.size() - idx));
} else {
if (line[7] == 'r') {
p.instr = Instruction::ROTATE_ROW;
} else if (line[7] == 'c'){
p.instr = Instruction::ROTATE_COL;
} else {
std::cout << "This should not happen" << '\n';
exit(0);
}
const auto idx_1 = line.find('=');
const auto idx_2 = line.find(' ', idx_1);
// std::cout << idx_1+1 << ' ' << idx_2 - idx_1 << '\n';
// std::cout << '|' << line.substr(idx_1 + 1, idx_2 - idx_1 - 1) << '|' <<'\n';
p.dim_1 = std::stoi(line.substr(idx_1 + 1, idx_2 - idx_1 - 1));
// std::cout << __LINE__ << '\n';
// std::cout << '|' << line.substr(idx_2 + 4, line.size() - idx_2 - 4) << '|' <<'\n';
p.dim_2 = std::stoi(line.substr(idx_2 + 4, line.size() - idx_2 - 4));
}
// If this assumption is incorrect then the code for modifying the screen will need to be updated
// Might need to use int instead of std::size_t
assert(p.dim_1 >= 0);
assert(p.dim_2 >= 0);
return p;
}
template<typename T, std::size_t d1, std::size_t d2>
void display_screen(const std::array<std::array<T, d2>, d1>& screen, const size_t letter_width, const size_t letter_height) {
for (std::size_t row = 0; row < d1; row++) {
if (row % letter_height == 0) std::cout << '\n';
for (std::size_t col = 0; col < d2; col++) {
if (col % letter_width == 0) std::cout << ' ';
if ((col+1) % letter_width == 0) continue;
std::cout << screen[row][col];
}
std::cout << '\n';
}
std::cout << '\n';
}
template<std::size_t d1, std::size_t d2>
std::size_t count_screen(const std::array<std::array<bool, d2>, d1>& screen) {
std::size_t count = 0;
for (const auto & row : screen) {
for (const auto & ele : row) {
if (ele) count++;
}
}
return count;
}
void apply_instruction(const Parsed& p, std::array<std::array<bool, cols>, rows>& screen) {
if (p.instr == Instruction::RECT) {
for (int row_idx = 0; row_idx < p.dim_2; row_idx++) {
for (int col_idx = 0; col_idx < p.dim_1; col_idx++) {
screen[row_idx][col_idx] = true;
}
}
} else if (p.instr == Instruction::ROTATE_ROW) {
const auto temp = screen[p.dim_1];
for (std::size_t i = 0; i < cols; i++) {
screen[p.dim_1][i] = false;
}
for (std::size_t i = 0; i < cols; i++) {
screen[p.dim_1][(i+p.dim_2) % cols] = temp[i];
}
} else if (p.instr == Instruction::ROTATE_COL) {
std::array<std::size_t, rows> temp {{}};
for (std::size_t i = 0; i < rows; i++) {
temp[i] = screen[i][p.dim_1];
screen[i][p.dim_1] = false;
}
for (std::size_t i = 0; i < rows; i++) {
screen[(i + p.dim_2) % rows][p.dim_1] = temp[i];
}
}
}
int main(int argc, char* argv[]) {
std::string input = "../input/day_08_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::array<std::array<bool,cols>, rows> screen{{}};
while(std::getline(file, line)) {
// std::cout << line << '\n';
const auto p = parse_input(line);
// std::cout << static_cast<int>(p.instr) << ' ' << p.dim_1 << ' ' << p.dim_2 << '\n';
apply_instruction(p, screen);
}
display_screen(screen, letter_width, letter_height);
return 0;
}