AoC2024/11/'
2024-12-11 12:49:47 +01:00

92 lines
1.8 KiB
Text

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
struct list {
int num;
struct list *next;
};
bool even_digits(int x)
{
int i = 0;
while (x /= 10)
i++;
return i % 2;
}
void split_stone(struct list *stones) {
int digits = 0;
int x = stones->num;
while (x /= 10)
digits++;
int a = stones->num;
int b = 0;
for (int i = 0; i < digits; i++) {
b = b * 10 + a % 10;
a /= 10;
}
struct list *tmp = stones->next;
stones->next = malloc(sizeof(struct list));
stones->num = a;
stones->next->num = b;
stones->next->next = tmp;
}
int main ()
{
// figure out data dimensions
FILE *input = fopen("./inputDemo", "r");
unsigned int tmp;
unsigned int array[10];
for (int i = 0; i < 10; i++)
array[i] = -1;
for (int i = 0; fscanf(input, "%d", &tmp) == 1; i++) {
array[i] = tmp;
}
fclose(input);
// put stones into list
struct list *stones = malloc(sizeof(struct list));
stones->num = array[0];
stones->next = NULL;
struct list *current = stones;
for (int i = 1; array[i] != -1; i++) {
while (current->next != NULL)
current = current->next;
current->next = malloc(sizeof(struct list));
current->next->next = NULL;
current->next->num = array[i];
}
// loop through list
for (int i = 0; i < 6; i++) {
current = stones;
while (current) {
// 0 => make 1
if (current->num == 0)
current->num = 1;
// even number of digits => split, fix list shit
else if (even_digits(current->num))
split_stone(current);
// (point first split onto second split, second split onto old ptr)
// else => mult 2024
else
current->num *= 2024;
current = current->next;
}
}
current = stones;
int sum = 0;
printf("Stones: ");
while (current) {
printf("%d ", current->num);
sum++;
current = current->next;
}
printf("%d\n", sum);
}