1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#include "aoc.h"
#include "allocator.h"
#include "hmap.h"
#include "dvec_s.h"
#include "util.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
struct planet {
char *name;
struct planet *parent;
struct dvec children;
};
struct hmap planets;
struct planet *
planets_get(const char *name)
{
struct hmap_link *link;
struct hmap_key key;
key.p = name;
link = hmap_get(&planets, key);
return link ? link->value._p : NULL;
}
struct planet *
planets_add(const char *name)
{
struct planet *planet;
char *key;
planet = malloc(sizeof(struct planet));
if (!planet) die("main: planets_add: malloc");
planet->name = aprintf("%s", name);
planet->parent = NULL;
dvec_init(&planet->children, sizeof(struct planet *),
3, &stdlib_heap_allocator);
key = aprintf("%s", name);
assert(!hmap_add(&planets, (struct hmap_key) {.p = key},
(struct hmap_val) {.p = planet}));
return planet;
}
void
planets_init()
{
struct planet *parent, *child, **slot;
const char *pos, *end;
char buf[256], *sep;
hmap_init(&planets, 100, hmap_str_hash,
hmap_str_keycmp, &stdlib_heap_allocator);
pos = aoc.input;
end = aoc.input + aoc.input_size;
while (readtok(buf, sizeof(buf), '\n', &pos, end)) {
sep = strchr(buf, ')');
if (!sep) die("main: bad format");
*sep = '\0';
parent = planets_get(buf);
if (!parent) parent = planets_add(buf);
child = planets_get(sep + 1);
if (!child) child = planets_add(sep + 1);
slot = dvec_add_slot(&parent->children);
*slot = child;
if (child->parent) die("main unexpected parent");
child->parent = parent;
}
}
void
planets_deinit()
{
struct hmap_iter iter;
struct planet *p;
for (HMAP_ITER(&planets, iter)) {
free(iter.link->key._p);
p = iter.link->value._p;
dvec_deinit(&p->children);
free(p->name);
free(p);
}
hmap_deinit(&planets);
}
void
part1(void)
{
struct hmap_iter iter;
const struct planet *p;
int orbits;
planets_init();
orbits = 0;
for (HMAP_ITER(&planets, iter)) {
p = iter.link->value.p;
while (p->parent) {
orbits += 1;
p = p->parent;
}
}
aoc.answer = aprintf("%i", orbits);
aoc.solution = "119831";
planets_deinit();
}
void
part2(void)
{
struct planet *p1, *p2, *san, *you;
int s1, s2;
planets_init();
san = planets_get("SAN");
if (!san) die("main: missing planet SAN");
you = planets_get("YOU");
if (!you) die("main: missing planet YOU");
for (s1 = 0, p1 = you; p1; p1 = p1->parent, s1++) {
for (s2 = 0, p2 = san; p2; p2 = p2->parent, s2++) {
if (!strcmp(p1->name, p2->name))
goto exit;
}
}
exit:
aoc.answer = aprintf("%i", s1 + s2 - 2);
aoc.solution = "322";
planets_deinit();
}
|