solved 11a

This commit is contained in:
kirreen 2024-12-11 12:49:47 +01:00
parent 2293c7a08f
commit a985e96bf8
5 changed files with 210 additions and 0 deletions

92
11/' Normal file
View file

@ -0,0 +1,92 @@
#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);
}

BIN
11/a.out Executable file

Binary file not shown.

1
11/input Normal file
View file

@ -0,0 +1 @@
510613 358 84 40702 4373582 2 0 1584

1
11/inputDemo Normal file
View file

@ -0,0 +1 @@
125 17

116
11/main.c Normal file
View file

@ -0,0 +1,116 @@
#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);
}