-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_09a.cpp
51 lines (47 loc) · 1.29 KB
/
day_09a.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
#include <fstream>
#include <iostream>
#include <ranges>
#include <string>
#include <vector>
// Replace with linked list/tree/map
int main(int argc, char* argv[]) {
std::string input = "../input/day_09_input";
if (argc > 1) {
input = argv[1];
}
std::ifstream file(input);
std::string line;
std::getline(file, line);
std::cout << line << '\n';
std::vector<int> blocks;
blocks.reserve(9 * line.size());
int n_file = 0;
int is_free = false;
for (const auto c : line) {
const int c_num = c - '0';
if (is_free) {
for (int i = 0; i < c_num; i++) {
blocks.push_back(-1);
}
is_free = false;
} else {
for (int i = 0; i < c_num; i++) {
blocks.push_back(n_file);
}
is_free = true;
n_file++;
}
}
for (int idx = 0, r_idx = blocks.size() - 1; idx < r_idx; idx++,r_idx--) {
while(blocks[idx] != -1) idx++;
while(blocks[r_idx] == -1) r_idx--;
std::swap(blocks[idx], blocks[r_idx]);
}
unsigned long long ans = 0;
for (const auto [idx, val] : std::views::enumerate(blocks)) {
if (val == -1) break;
ans += idx * (val);
}
std::cout << ans << '\n';
return 0;
}