fuck this
This commit is contained in:
parent
73ce54256e
commit
75f8b35464
88
day9/second.cpp
Normal file
88
day9/second.cpp
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#include "../include/color.h"
|
||||||
|
#include "../include/useful_funcs.h"
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
void printDiskMap(std::vector<int> &diskmap) {
|
||||||
|
std::string numC = BOLDGREEN;
|
||||||
|
for (int i = 0; i < diskmap.size(); i++) {
|
||||||
|
if (diskmap[i] == -1) {
|
||||||
|
numC = BOLDYELLOW;
|
||||||
|
std::clog << RED "." << RESET;
|
||||||
|
} else {
|
||||||
|
std::clog << numC << diskmap[i] << RESET;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::clog << RESET << std::endl;
|
||||||
|
}
|
||||||
|
int main() {
|
||||||
|
std::ifstream inputfile("example");
|
||||||
|
|
||||||
|
unsigned long long res = 0;
|
||||||
|
if (!inputfile.is_open()) {
|
||||||
|
std::cerr << "Could not open the file" << std::endl;
|
||||||
|
return ENOENT;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> diskmap;
|
||||||
|
|
||||||
|
// create the disk map
|
||||||
|
{
|
||||||
|
std::string line;
|
||||||
|
inputfile >> line;
|
||||||
|
int id = 0;
|
||||||
|
std::vector<char> lineSplited = std::vector<char>(line.begin(), line.end());
|
||||||
|
for (int i = 0; i < lineSplited.size(); i++) {
|
||||||
|
|
||||||
|
for (int j = 0; j < lineSplited[i] - '0'; j++) {
|
||||||
|
if (i % 2 == 0) {
|
||||||
|
diskmap.push_back(id);
|
||||||
|
} else {
|
||||||
|
diskmap.push_back(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i % 2 == 0) {
|
||||||
|
id++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printDiskMap(diskmap);
|
||||||
|
{
|
||||||
|
int Dot;
|
||||||
|
auto offset = diskmap.begin();
|
||||||
|
int block_count;
|
||||||
|
|
||||||
|
while (std::find(diskmap.begin(), diskmap.end(), -1) != diskmap.end()) {
|
||||||
|
while (true) {Dot =
|
||||||
|
std::find(diskmap.begin(), diskmap.end(), -1) - diskmap.begin();
|
||||||
|
if(diskmap.back() == -1) {
|
||||||
|
diskmap.pop_back();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
block_count =
|
||||||
|
std::find(diskmap.begin(), diskmap.end(), -1) - diskmap.begin();count(diskmap.end(),diskmap.begin(),diskmap.back());
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Dot =
|
||||||
|
std::find(offset, diskmap.end(), -1) - diskmap.begin();
|
||||||
|
if(std::count(diskmap.begin() + Dot,diskmap.end(), -1) == block_count) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Dot =
|
||||||
|
std::find(diskmap.begin(), diskmap.end(), -1) - diskmap.begin();
|
||||||
|
std::cout << "first dot: " << Dot << std::endl;
|
||||||
|
|
||||||
|
diskmap[Dot] = diskmap.back();
|
||||||
|
diskmap.pop_back();
|
||||||
|
printDiskMap(diskmap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < diskmap.size(); i++) {
|
||||||
|
res += i * diskmap[i];
|
||||||
|
}
|
||||||
|
std::cout << CYAN "\nfilesystem checksum is: " << BOLDYELLOW << res << RESET
|
||||||
|
<< std::endl;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user