Browse Source

day8p2

master
mid-kid 6 years ago
parent
commit
a70f0e01e5
  1. 2
      day8p1/main.c
  2. 12
      day8p2/Makefile
  3. 1
      day8p2/input
  4. 51
      day8p2/main.c

2
day8p1/main.c

@ -38,6 +38,6 @@ int main()
parse_node(strings);
printf("%d\n", result);
g_free(strings);
g_strfreev(strings);
return 0;
}

12
day8p2/Makefile

@ -0,0 +1,12 @@
CFLAGS := -Wall -Wextra -std=c17 -D_GNU_SOURCE
LIBS := glib-2.0
CFLAGS += $(shell pkg-config --cflags $(LIBS))
LDLIBS := $(shell pkg-config --libs $(LIBS))
.PHONY: all
all: main
.PHONY: clean
clean:
rm -f main

1
day8p2/input

File diff suppressed because one or more lines are too long

51
day8p2/main.c

@ -0,0 +1,51 @@
#include <stdio.h>
#include <glib.h>
int parse_node(gchar ***node)
{
int children = strtol(*(*node)++, NULL, 0);
int metadata = strtol(*(*node)++, NULL, 0);
int values[children];
int result = 0;
for (int i = 0; i < children; i++) {
values[i] = parse_node(node);
}
for (int i = 0; i < metadata; i++) {
int offs = strtol(*(*node)++, NULL, 0);
if (!children) {
result += offs;
continue;
}
if (children < offs) continue;
result += values[offs - 1];
}
return result;
}
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);
gchar **node = strings;
printf("%d\n", parse_node(&node));
g_strfreev(strings);
return 0;
}
Loading…
Cancel
Save