summaryrefslogtreecommitdiff
path: root/6a.c
blob: 2c0031d178eeb0d38a96e9e78f60339cb0e63187 (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct node
{
    struct node *next;
    int i;
} node;

static node *head = NULL;
static node *tail = NULL;

static char *ReadLine(char *p, size_t size, FILE *fp)
{
    if ((p=fgets(p, size, fp)))
    {
        size_t l = strlen(p);

        while(l && p[l-1] == '\n')
        {
            p[--l] = 0;
        }
    }

    return p;
}

static void Add(int i)
{
    node *new;

    new = malloc(sizeof *new);

    new->next = NULL;

    if (tail)
    {
        tail->next = new;
    }

    tail = new;

    if (!head)
    {
        head = new;
    }

    new->i = i;
}

int main(void)
{
    char buff[1024];
    unsigned long long num_count = 0;
    char *p = NULL;
    unsigned long long f = 0;
    int cycle = 0;

    ReadLine(buff, sizeof buff, stdin);

    p = strtok(buff, ",");

    while(p)
    {
        Add(atoi(p));
        p = strtok(NULL, ",");
    }

    for(cycle = 0; cycle < 256; cycle++)
    {
        int to_add = 0;
        node *list;

        list = head;

        while(list)
        {
            if (list->i == 0)
            {
                to_add++;
                list->i = 6;
            }
            else
            {
                list->i--;
            }

            list = list->next;
        }

        if (to_add)
        {
            num_count += to_add;

            for(f = 0; f < to_add; f++)
            {
                Add(8);
            }
        }

        printf("cycle %d/%llu\n", cycle, num_count);
    }

    printf("num_count=%llu\n", num_count);

    return 0;
}