116 lines
2.3 KiB
C
116 lines
2.3 KiB
C
#include<stdlib.h>
|
|
#include<stdio.h>
|
|
#include<string.h>
|
|
#include<stdbool.h>
|
|
|
|
struct list {
|
|
unsigned long int num;
|
|
struct list *next;
|
|
};
|
|
|
|
bool even_digits(unsigned long int x)
|
|
{
|
|
int i = 0;
|
|
while (x /= 10)
|
|
i++;
|
|
return i % 2;
|
|
}
|
|
// power! x to the y!
|
|
int pow(int x, int y)
|
|
{
|
|
int ans = 1;
|
|
for (int i = 0; i < y; i++) ans *= x;
|
|
return ans;
|
|
}
|
|
void split_stone(struct list *stones) {
|
|
int digits = 1;
|
|
unsigned long int x = stones->num;
|
|
while (x /= 10)
|
|
digits++;
|
|
|
|
unsigned long int a = stones->num;
|
|
int b = 0;
|
|
for (int i = 0; i < digits / 2; i++) {
|
|
b += (a % 10) * pow(10, i);
|
|
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("./input", "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 < 75; i++) {
|
|
current = stones;
|
|
bool last = false;
|
|
while (current) {
|
|
if (current->next == NULL)
|
|
last = true;
|
|
// 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);
|
|
current = current->next;
|
|
}
|
|
// (point first split onto second split, second split onto old ptr)
|
|
// else => mult 2024
|
|
else
|
|
current->num *= 2024;
|
|
|
|
current = current->next;
|
|
if (last)
|
|
break;
|
|
}
|
|
struct list *tmp = stones;
|
|
/*
|
|
int sum = 0;
|
|
printf("%d blinks: \n", i + 1);
|
|
while (tmp) {
|
|
printf("%lu ", tmp->num);
|
|
sum++;
|
|
tmp = tmp->next;
|
|
}
|
|
printf("\ntotal: %d\n", sum);
|
|
*/
|
|
}
|
|
current = stones;
|
|
unsigned long int sum = 0;
|
|
printf("Stones: ");
|
|
while (current) {
|
|
printf("%lu ", current->num);
|
|
sum++;
|
|
current = current->next;
|
|
}
|
|
printf("\ntotal: %d\n", sum);
|
|
}
|