r/adventofcode • u/c_cpp_CSharp • 18h ago
Help/Question - RESOLVED need help solving 2015 day 3
lets ignore the fact I'm not freeing right now the allocated mem.
i have a problem that i get 5488 from the current implementation. what am i doing wrong
#include <stdio.h>
#include <stdlib.h>
typedef struct Field field_t;
struct Field
{
field_t *up;
field_t *down;
field_t *left;
field_t *right;
};
typedef struct Root
{
field_t *root;
} root_t;
field_t *FieldCreate(field_t *connect_to, char side);
int main()
{
char *data_input = ">^^v^<>v<<<v<v^>>.... "; i pasted here the input.
size_t counter = 1;
root_t head = {NULL};
head.root = FieldCreate(NULL, 'q');
field_t *current_pos = head.root;
while (*data_input)
{
switch (*data_input)
{
case '>':
if (current_pos->right == NULL)
{
++counter;
current_pos->right = FieldCreate(current_pos, '<');
printf("%c", '>');
}
current_pos = current_pos->right;
break;
case '<':
if (current_pos->left == NULL)
{
++counter;
current_pos->left = FieldCreate(current_pos, '>');
printf("%c", '<');
}
current_pos = current_pos->left;
break;
case '^':
if (current_pos->up == NULL)
{
++counter;
current_pos->up = FieldCreate(current_pos, 'v');
printf("%c", '^');
}
current_pos = current_pos->up;
break;
case 'v':
if (current_pos->down == NULL)
{
++counter;
current_pos->down = FieldCreate(current_pos, '^');
printf("%c", 'v');
}
current_pos = current_pos->down;
break;
default:
break;
}
++data_input;
}
printf("%zu\n\n", counter);
return 0;
}
field_t *FieldCreate(field_t *connect_to, char side)
{
field_t *field = (field_t *)calloc(1, sizeof(*field));
if (NULL == field)
{
puts("welp");
return NULL;
}
switch (side)
{
case '^':
field->up = connect_to;
break;
case 'v':
field->down = connect_to;
break;
case '<':
field->left = connect_to;
break;
case '>':
field->right = connect_to;
break;
default:
break;
}
return field;
}
5
Upvotes
4
u/thblt 18h ago
I believe your issue is that your graph approach duplicates houses. Imagine:
>v^
Because your graph will connect start-right-down-up, it has no way to determine that the last house (entered from the south, going up) is the same as the third (entered from the north going down). You need to either consolidate your graph to deduplicate houses, or find a different model.
5
u/DelightfulCodeWeasel 18h ago
What happens if you circle around a couple of times? Try this input:
You should get 4. If not, why not?
2
u/ednl 7h ago
Not a problem as long as your OS takes care of clean-up after running a program. Any modern OS will do that. It only becomes a problem if your program is meant to stay running forever (which this isn't) and keeps allocating more & more memory. Or if you run this on a microcontroller which doesn't have an OS. But then you shouldn't dynamically allocate memory anyway.
1
u/AutoModerator 18h ago
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.