-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-1.c
115 lines (99 loc) · 2.42 KB
/
06-1.c
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
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#define HEADS_NUM 500
#define END_DAY 80
typedef struct list_s
{
int8_t count;
struct list_s **branches;
} list;
list * init_node(int8_t val);
void print_list(list *head);
int count_branches(list * head);
void update(list * head);
int days_left = END_DAY;
int main(void)
{
list *heads[HEADS_NUM] = {NULL};
int64_t count = 0, day;
FILE * fin;
char line[701], *tmp;
if ((fin = fopen("06-input.txt", "r")))
{
fscanf(fin, "%700s", line);
tmp = malloc(sizeof(char) * 10);
tmp = strtok(line, ",");
for (int32_t i = 0; tmp != NULL; ++i)
{
heads[i] = init_node(atoi(tmp));
tmp = strtok(NULL, ",");
}
for (day = 0; day < END_DAY; ++day)
{
printf("Day %ld : ", day + 1);
for (int32_t i = 0; heads[i] != NULL; ++i)
{
update(heads[i]);
print_list(heads[i]);
}
printf("\n");
days_left--;
}
for (int32_t i = 0; heads[i] != NULL; ++i)
{
count += count_branches(heads[i]);
}
printf("%ld\n", count);
} else {
printf("Cannot open input file\n");
}
return 0;
}
list * init_node(int8_t val)
{
assert(val);
list * branch = NULL;
if ((branch = (list *) malloc(sizeof(list))))
{
branch->count = val;
branch->branches = (list ** )malloc(sizeof(list) * (days_left / 8 + 1));
} else {
printf("Could not allocate memory for %d\n", val);
exit(1);
}
return branch;
}
void print_list(list *head)
{
printf("%d ", head->count);
for (int8_t i = 0; head->branches[i] != NULL; ++i)
print_list(head->branches[i]);
}
int count_branches(list * head)
{
int64_t counter = 1;
for (int8_t i = 0; head->branches[i] != NULL; ++i)
{
counter += count_branches(head->branches[i]);
}
return counter;
}
void update(list * head)
{
head->count--;
for (int8_t i = 0; head->branches[i] != NULL; ++i)
update(head->branches[i]);
if (head->count < 0)
{
head->count = 6;
int8_t index;
for (index = 0; head->branches[index] != NULL; ++index)
;
head->branches[index] = init_node(8);
}
}