WTF
This commit is contained in:
parent
d8d2bcc379
commit
b9bed47c73
72
day9/day.cpp
Normal file
72
day9/day.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "../include/color.h"
|
||||
#include "../include/useful_funcs.h"
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
void printDiskMap(std::vector<char> &diskmap) {
|
||||
std::string numC = BOLDGREEN;
|
||||
for (int i = 0; i < diskmap.size(); i++) {
|
||||
if (diskmap[i] == '.') {
|
||||
numC = BOLDYELLOW;
|
||||
std::clog << RED "." << RESET;
|
||||
} else {
|
||||
std::clog << numC << diskmap[i] << RESET;
|
||||
}
|
||||
}
|
||||
std::cout << RESET << std::endl;
|
||||
}
|
||||
int main() {
|
||||
std::ifstream inputfile("input");
|
||||
|
||||
unsigned long long res = 0;
|
||||
if (!inputfile.is_open()) {
|
||||
std::cerr << "Could not open the file" << std::endl;
|
||||
return ENOENT;
|
||||
}
|
||||
|
||||
std::vector<char> 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 + '0');
|
||||
} else {
|
||||
diskmap.push_back('.');
|
||||
}
|
||||
}
|
||||
if (i % 2 == 0) {
|
||||
id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
printDiskMap(diskmap);
|
||||
{
|
||||
int firstDot;
|
||||
while (std::find(diskmap.begin(), diskmap.end(), '.') != diskmap.end()) {
|
||||
firstDot =
|
||||
std::find(diskmap.begin(), diskmap.end(), '.') - diskmap.begin();
|
||||
diskmap[firstDot] = diskmap.back();
|
||||
diskmap.pop_back();
|
||||
printDiskMap(diskmap);
|
||||
}
|
||||
}
|
||||
std::vector<int> diskmapInt;
|
||||
|
||||
std::transform(diskmap.begin(), diskmap.end(), std::back_inserter(diskmapInt),
|
||||
[](const char &s) { return s - '0'; });
|
||||
|
||||
for (int i = 0; i < diskmapInt.size(); i++) {
|
||||
res += i * diskmapInt[i];
|
||||
}
|
||||
std::cout << CYAN "\nfilesystem checksum is: " << BOLDYELLOW << res << RESET
|
||||
<< std::endl;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user