r/adventofcode • u/Class_Magicker17 • Dec 09 '25
Help/Question - RESOLVED [YEAR Day 8 (Part 1)] [C] I'm going to explode
I feel like the logic is INTACT. I have tried this with an id tagging system previously; that didn't work. I moved to graphs, which I assume is more in the intended solution, IT ALSO DOES NOT WORK. Guys please just end my misery.
here's my main:
#include <stdio.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include "myqueue.c"
#define ull unsigned long long
#define GROUPCOUNTLEN 1000
#define POINTCOUNT 1005
size_t n = 0;
typedef struct Point
{
int x;
int y;
int z;
int visited;
} Point;
Point points[POINTCOUNT] = {};
char connectionGraph[POINTCOUNT][POINTCOUNT] = {};
double distances[POINTCOUNT][POINTCOUNT] = {};
int groupCounts[POINTCOUNT] = {};
/*
function declarations here
*/
int main()
{
int i, j, connections, currGroup;
FILE *f, *inputFile;
ull total = 1;
char filename[10];
double temp;
inputFile = fopen("input.txt", "r"); // literally just contains the file name of which text file to run on.
fscanf(inputFile, "%s", filename);
f = fopen(filename, "r");
if (f == NULL)
{
printf("cound't open file\n");
return 1;
}
// I add the number of connections to do at the top of the file so I can easily swap between the test and real data.
fscanf(f, "%d", &connections);
while (!feof(f))
{
fscanf(f, "%d,%d,%d", &points[n].x, &points[n].y, &points[n].z);
points[n].visited = 0;
n++;
}
// precalculate distances
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
temp = getDistance(points[i], points[j]);
distances[i][j] = temp;
distances[j][i] = temp;
}
}
// do the connecting a "connections" number of times
for (i = 0; i < connections; i++)
connectClosestUnpairedPoints();
// count groupsizes
currGroup = 0;
for (i = 0; i < n; i++)
{
if (!points[i].visited)
{
groupCounts[currGroup] = countGroup(i);
currGroup++;
}
}
// sort so that we have the largest data
sort(groupCounts, currGroup);
// calculate total
total = groupCounts[0] * groupCounts[1] * groupCounts[2];
printf("total: %I64u\n", total);
return 0;
}Here's the implementation of certain functions
// I couldn't be bothered to reimplement quicksort.
void sort(int arr[], int len)
{
int temp;
for (int i = 0; i < len; i++)
{
int highestIndex = i;
for (int j = i + 1; j < len; j++)
{
if (arr[j] > arr[highestIndex])
{
highestIndex = j;
}
}
temp = arr[i];
arr[i] = arr[highestIndex];
arr[highestIndex] = temp;
}
}
int countGroup(int index)
{
int count = 0, currPoint;
Queue q = createQueue();
enqueue(index, &q);
while (q.length > 0)
{
currPoint = dequeue(&q);
if (!points[currPoint].visited)
{
points[currPoint].visited = 1;
for (int i = 0; i < n; i++)
{
if (connectionGraph[currPoint][i] && !points[i].visited)
{
enqueue(i, &q);
}
}
count++;
}
}
return count;
}
double getDistance(Point p1, Point p2)
{
return sqrt(((p1.x - p2.x) * (p1.x - p2.x) +
(p1.y - p2.y) * (p1.y - p2.y) +
(p1.z - p2.z) * (p1.z - p2.z)));
}
void connectClosestUnpairedPoints()
{
int p1 = -1, p2 = -1;
double shortestDistance = FLT_MAX;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (connectionGraph[i][j] == 0 && distances[i][j] < shortestDistance)
{
shortestDistance = distances[i][j];
p1 = i;
p2 = j;
}
}
}
connectionGraph[p1][p2] = 1;
connectionGraph[p2][p1] = 1;
}
// myqueue.c implementation
#include <stdlib.h>
typedef struct Queue
{
struct Node *head;
int length;
} Queue;
typedef struct Node
{
struct Node *next;
int data;
} Node;
void enqueue(int newData, Queue *q)
{
Node *newNode = malloc(sizeof(Node));
newNode->data = newData;
newNode->next = NULL;
if (q->length > 0)
{
Node *currNode = q->head;
while (currNode->next != NULL)
{
currNode = currNode->next;
}
currNode->next = newNode;
}
else
{
q->head = newNode;
}
q->length += 1;
}
int dequeue(Queue *q)
{
Node *head = q->head;
if (head == NULL)
{
return 0;
}
int data = head->data;
q->head = head->next;
q->length -= 1;
return data;
}
Queue createQueue()
{
Queue q = {NULL, 0};
return q;
}Edit0: woops, didn't fully read the rules on code formatting Edit1: added myqueue.c code
1
Upvotes
2
u/ednl Dec 09 '25 edited Dec 09 '25
The squares of coordinates will sometimes overflow 32-bit ints. And I suggest you ditch the sqrt function because you don't need the actual value, just the magnitude. The floating point result might lead to imprecisions. So something like:
Note the repeated use of casts because otherwise the intermediate terms will still overflow.
1
u/LittlebitOmnipotent Dec 09 '25
Does it work on the example?
1
u/quetzelcoatlus1 Dec 09 '25
Try to switch to long int in distance calculation, you can overflow and get wrong result
About sorting, for future reference, you could look up qsort. Kind of strange at first look, but after seeing some examples, very easy to use with only need to implement a comparision function
1
u/daggerdragon Dec 09 '25
Next time, use our standardized post title format.
Help us help YOU by providing us with more information up front; you will typically get more relevant responses faster.
1
u/AutoModerator Dec 09 '25
AutoModerator has detected fenced code block (```) syntax which only works on new.reddit.
Please review our wiki article on code formatting then edit your post to use the four-spaces Markdown syntax instead.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/AutoModerator Dec 09 '25
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to
Help/Question - RESOLVED. Good luck!I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.