You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
788 B
43 lines
788 B
#include <stdio.h>
|
|
|
|
#include <glib.h>
|
|
|
|
int result = 0;
|
|
|
|
gchar **parse_node(gchar **node)
|
|
{
|
|
int children = strtol(*node++, NULL, 0);
|
|
int metadata = strtol(*node++, NULL, 0);
|
|
|
|
for (int i = 0; i < children; i++) {
|
|
node = parse_node(node);
|
|
}
|
|
|
|
for (int i = 0; i < metadata; i++) {
|
|
result += strtol(*node++, NULL, 0);
|
|
}
|
|
|
|
return node;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
gchar *contents;
|
|
gchar **strings;
|
|
GError *err = NULL;
|
|
|
|
g_file_get_contents("input", &contents, NULL, &err);
|
|
if (err) {
|
|
fprintf(stderr, "%s\n", err->message);
|
|
g_error_free(err);
|
|
return 1;
|
|
}
|
|
strings = g_strsplit(contents, " ", -1);
|
|
g_free(contents);
|
|
|
|
parse_node(strings);
|
|
printf("%d\n", result);
|
|
|
|
g_strfreev(strings);
|
|
return 0;
|
|
}
|
|
|