aoc_omni/c/2020/10/main.c

72 lines
1.6 KiB
C
Raw Normal View History

2025-11-30 21:22:21 -05:00
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "../lib/util.h"
#define ADAPTER_LENGTH 99
void int_array_quicksort(int *array, size_t length) {
// printf("Length: %d\n", length);
if (length < 2) return;
int *low = malloc((length - 1) * sizeof(int));
size_t low_index = 0;
int *high = malloc((length - 1) * sizeof(int));
size_t high_index = 0;
size_t pivot_index = length / 2;
int pivot = array[pivot_index];
for (size_t i = 0; i < length; i++) {
int element = array[i];
if (element > pivot) {
low[low_index++] = element;
} else {
high[high_index++] = element;
}
}
// printf("low: %d\nhigh: %d\n\n", low_index, high_index);
if (low_index && high_index) {
int_array_quicksort(low, low_index);
int_array_quicksort(high, high_index);
}
for (size_t i = 0; i < low_index; i++) {
array[i] = low[i];
}
array[pivot_index] = pivot;
for (size_t i = 0; i < high_index; i++) {
array[i + low_index + 1] = high[i];
}
free(low);
free(high);
}
int part_1() {
char *data_buffer = load_input();
int *adapters = malloc(sizeof(int) * ADAPTER_LENGTH);
size_t adapter_index = 0;
char *adapter_text = strtok(data_buffer, "\n");
do {
adapters[adapter_index++] = atoi(adapter_text);
} while (adapter_text = strtok(NULL, "\n"));
int_array_quicksort(adapters, ADAPTER_LENGTH);
free(data_buffer);
free(adapters);
return 1;
}
int part_2() {
return 0;
char *data_buffer = load_input();
}
int main() {
printf("Part 1: %d\n", part_1());
printf("Part 2: %d\n", part_2());
}