my-solutions/advent-of-code/2024/day_09/main.c

44 lines
977 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib.h"
#define LINE_LEN 20000
int main() {
FILE* file_ptr = fopen("input.txt", "r");
if (file_ptr == NULL) {
printf("Can't open input file\n");
return -1;
}
char input[LINE_LEN];
fread(input, LINE_LEN, 1, file_ptr);
size_t input_length = strlen(input);
if (input_length < 1) {
printf("Empty input.\n");
return -1;
}
if (input_length > 0 && input[input_length-1] == '\n') {
input[input_length-1] = '\0';
}
struct Vec blocks = expand_disk_map(input);
struct Vec blocks_copy = copy_vec(blocks);
compress_fs(blocks);
long checksum = calc_checksum(blocks);
printf("Part 1: %ld\n", checksum);
compress_fs_by_blocks(blocks_copy);
checksum = calc_checksum(blocks_copy);
printf("Part 2: %ld\n", checksum);
free(blocks.data);
free(blocks_copy.data);
fclose(file_ptr);
return 0;
}