-
Notifications
You must be signed in to change notification settings - Fork 2
/
14.cpp
149 lines (123 loc) · 3.49 KB
/
14.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <bitset>
#include <chrono>
#include <iostream>
#include <vector>
using std::bitset;
using std::string;
using std::vector;
void reverse(vector<int> &list, unsigned int start, unsigned int length) {
for (unsigned int i = 0; i < length / 2; i++) {
vector<int>::size_type idx_a = (start + i) % (list.size());
vector<int>::size_type idx_b = (start + length - i - 1) % (list.size());
std::swap(list[idx_a], list[idx_b]);
}
}
bitset<128> create_row(const vector<int> &lengths, unsigned int row_idx) {
vector<int> list(256);
for (int i = 0; i < 256; i++) {
list[i] = i;
}
int pos = 0;
int skip = 0;
const auto row_lengths = std::to_string(row_idx);
for (unsigned int i = 0; i < 64; i++) {
for (const int length : lengths) {
reverse(list, pos, length);
pos += length + skip;
skip += 1;
}
for (const int length : row_lengths) {
reverse(list, pos, length);
pos += length + skip;
skip += 1;
}
for (const int length : {17, 31, 73, 47, 23}) {
reverse(list, pos, length);
pos += length + skip;
skip += 1;
}
}
// we now have a "sparse hash" of 256 elements
// turn it into a "dense hash" of 16 elements
int dense_hash[16] = {0};
for (unsigned int i = 0; i < 16; i++) {
for (unsigned int j = i * 16, jend = (i + 1) * 16; j < jend; j++) {
dense_hash[i] ^= list[j];
}
}
// now, dense hash to bitset
std::bitset<128> bits;
for (unsigned int i = 0; i < 16; i++) {
for (unsigned int j = 0; j < 8; j++) {
bits[(i * 8) + j] = (dense_hash[i] >> (7 - j)) & 1;
}
}
return bits;
}
vector<int> parse_input() {
vector<int> lengths;
std::string input;
std::getline(std::cin, input);
for (const char ch : input) {
lengths.push_back(ch);
}
lengths.push_back('-');
return lengths;
}
void mark_group(const vector<bitset<128>> &grid, bitset<128 * 128> &seen,
const unsigned int row, const unsigned int col) {
if (row >= 128 || col >= 128) {
return;
}
if (grid[row][col] != true) {
return;
}
if (seen[(row * 128) + col]) {
return;
}
seen[(row * 128) + col] = true;
mark_group(grid, seen, row + 1, col);
mark_group(grid, seen, row - 1, col);
mark_group(grid, seen, row, col + 1);
mark_group(grid, seen, row, col - 1);
}
int solve_pt2(const vector<bitset<128>> &grid) {
int regions = 0;
bitset<128 * 128> seen;
for (unsigned int row = 0; row < 128; row++) {
for (unsigned int col = 0; col < 128; col++) {
if (seen[(row * 128) + col]) {
continue;
}
if (grid[row][col] == false) {
continue;
}
regions++;
mark_group(grid, seen, row, col);
}
}
return regions;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
unsigned int pt1 = 0;
unsigned int pt2 = 0;
vector<int> lengths = parse_input();
vector<bitset<128>> grid;
for (unsigned int i = 0; i < 128; i++) {
bitset<128> row = create_row(lengths, i);
grid.push_back(row);
pt1 += static_cast<unsigned int>(row.count());
}
pt2 = solve_pt2(grid);
std::cout << "--- Day 14: Disk Defragmentation ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}