134 lines
2.7 KiB
C
134 lines
2.7 KiB
C
#include<stdlib.h>
|
|
#include<stdio.h>
|
|
#include<string.h>
|
|
#include<stdbool.h>
|
|
|
|
int put_it_in(unsigned int *memory, char *diskMap, unsigned int dataID, unsigned long int dataSum)
|
|
{
|
|
int dataLen = diskMap[dataID * 2] - '0';
|
|
int availableIndex = 0;
|
|
int avail = 0;
|
|
|
|
// find start of dataID
|
|
int start = 0;
|
|
for (int i = 0; i < dataSum; i++) {
|
|
if (memory[i] == dataID) {
|
|
start = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// find free space
|
|
for (int i = 0; i <= start; i++) {
|
|
if ( i == start)
|
|
return 0;
|
|
if (memory[i] != -1) {
|
|
availableIndex = 0;
|
|
avail = 0;
|
|
continue;
|
|
}
|
|
if (memory[i] == -1) {
|
|
if (!availableIndex)
|
|
availableIndex = i;
|
|
avail++;
|
|
}
|
|
if (avail >= dataLen)
|
|
break;
|
|
}
|
|
// write the file to this space
|
|
for (int i = 0; i < dataLen; i++) {
|
|
memory[availableIndex + i] = dataID;
|
|
}
|
|
|
|
// remove the original file
|
|
for (int i = dataSum - 1; i >= availableIndex + dataLen; i--) {
|
|
if (memory[i] == dataID) {
|
|
for (int j = 0; j < dataLen; j++) {
|
|
memory[i - j] = -1;
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main ()
|
|
{
|
|
// figure out data dimensions
|
|
FILE *input = fopen("./input", "r");
|
|
int width = 0;
|
|
bool flip = 1;
|
|
|
|
unsigned long int dataSum = 0;
|
|
unsigned int dataID = 0;
|
|
int dataSlot = 0;
|
|
|
|
for (char c = fgetc(input); c != EOF && c != '\n'; c = fgetc(input)) {
|
|
dataSum += c - '0';
|
|
width++;
|
|
}
|
|
rewind(input);
|
|
printf("Length: %d\ndataSum: %u\n", width, dataSum);
|
|
// save diskMap, cuz diskMap[dataID * 2] is the file length
|
|
unsigned int *memory = malloc(dataSum * sizeof(int));
|
|
char *diskMap = malloc(width * sizeof(char));
|
|
//
|
|
// -1 represents empty memory
|
|
for (int i = 0; i < dataSum; i++) {
|
|
memory[i] = -1;
|
|
}
|
|
|
|
// time to fill the array
|
|
for (int i = 0; i < width; i++) {
|
|
char c = fgetc(input);
|
|
diskMap[i] = c;
|
|
if (c == EOF) {
|
|
printf("breakbreakbreak!\n");
|
|
break;
|
|
}
|
|
// fan ska ju bara vara varannan som är data, resten free space
|
|
// flipflopflipflop ^
|
|
for (int j = 0; j < (c - '0'); j++) {
|
|
if (flip) {
|
|
memory[dataSlot] = dataID;
|
|
printf("%u", memory[dataSlot]);
|
|
} else {
|
|
memory[dataSlot] = -1;
|
|
printf(".");
|
|
}
|
|
dataSlot++;
|
|
}
|
|
if (flip)
|
|
dataID++;
|
|
flip ^= true;
|
|
}
|
|
fclose(input);
|
|
printf("\n");
|
|
|
|
// it's compacting time!
|
|
// start at end of array, save id, overwrite, put in first free spot
|
|
unsigned int lastID = - 1;
|
|
for (int i = dataSum - 1; i >= 0; i--) {
|
|
if (memory[i] < lastID) {
|
|
lastID = memory[i];
|
|
put_it_in(memory, diskMap, lastID, dataSum);
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < dataSum; i++) {
|
|
if (memory[i] != -1)
|
|
printf("%u ", memory[i]);
|
|
else
|
|
printf(".");
|
|
}
|
|
|
|
// now hash time
|
|
unsigned long int hash = 0;
|
|
for (int i = 0; i < dataSum; i++) {
|
|
if (memory[i] != -1)
|
|
hash += memory[i] * i;
|
|
}
|
|
|
|
free(memory);
|
|
printf("\n%lu\n", hash);
|
|
}
|