aboutsummaryrefslogtreecommitdiff
path: root/10/part1.c
blob: 53488303673828404d3709a4d6e321d7659bd6fc (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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <algds/sort.h>
#include <algds/type_alias.h>
#include <errno.h>
#include <ctype.h>

#include <algds/vec.h>
#include <algds/str.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>

#define PANIC do { \
    fprintf(stderr, "panic at %s:%d\n", __FILE__, __LINE__); \
    abort(); \
} while (0)

void expect_char(char e) {
    int c = fpeek(stdin);
    if (c != e) {
        PANIC;
    }
    fgetc(stdin);
}

bool string2long(const char *s, long *num) {
    char *end;
    errno = 0;
    *num = strtol(s, &end, 10);
    if (end == s) {
        return false;
    }
    if (errno != 0) {
        return false;
    }
    return true;
}

int64_t parse_integer() {
    long ret;
    StrBuilder sb;
    StrBuilder_init(&sb);
    while (1) {
        int c = fpeek(stdin);
        if (isdigit(c)) {
            fgetc(stdin);
            StrBuilder_append_char(&sb, c);
        } else {
            break;
        }
    }
    if (strlen(sb.buf) == 0) {
        PANIC;
    }
    StrBuilder_append_char(&sb, '\0');
    if (!string2long(sb.buf, &ret)) {
        PANIC;
    }
    free(sb.buf);
    return ret;
}

uint32_t parse_indicator() {
    expect_char('[');
    uint32_t ret = 0;
    int n = 0;
    while (1) {
        int c = fpeek(stdin);
        if (c == '.') {
            getchar();
        } else if (c == '#') {
            getchar();
            // printf("n=%d\n", n);
            ret |= (1 << n);
            // printf("ret=%d\n", ret);
        } else {
            break;
        }
        n++;
    }
    expect_char(']');
    return ret;
}

uint32_t parse_button() {
    uint32_t ret = 0;
    expect_char('(');
    while (1) {
        long num = parse_integer();
        ret |= (1 << num);
        int c = fpeek(stdin);
        if (c == ',') {
            getchar();
        } else {
            break;
        }
    }
    expect_char(')');
    return ret;
}

void skip_space() {
    while (1) {
        int c = fpeek(stdin);
        if (c == EOF) return;
        if (isspace(c)) {
            fgetc(stdin);
        } else {
            return;
        }
    }
}

UIntVector parse_buttons() {
    skip_space();
    UIntVector ret;
    UIntVector_init(&ret);
    while (1) {
        int c = fpeek(stdin);
        if (c == '(') {
            uint32_t btn = parse_button();
            UIntVector_push_back(&ret, btn);
            skip_space();
        } else {
            break;
        }
    }
    return ret;
}

void skip_to_line_end() {
    while (1) {
        int c = getchar();
        if (c != '\n' && c != EOF) {
            continue;
        } else {
            break;
        }
    }
}

void solve_impl(uint32_t target, UIntVector *buttons, uint32_t curind, int curbtn, int curtimes, int *mintimes) {
    if (target == curind) {
        if (curtimes < *mintimes) {
            *mintimes = curtimes;
        }
    }
    if (curbtn >= buttons->size) {
        return;
    }
    uint32_t btn = buttons->buffer[curbtn];
    solve_impl(target, buttons, curind, curbtn+1, curtimes, mintimes);
    solve_impl(target, buttons, curind ^ btn, curbtn+1, curtimes+1, mintimes);
}

int solve(uint32_t indicator, UIntVector *buttons) {
    int mintimes = INT_MAX;
    solve_impl(indicator, buttons, 0, 0, 0, &mintimes);
    if (mintimes == INT_MAX) {
        PANIC;
    }
    return mintimes;
}

int main() {
    int r = 0;
    while (1) {
        int c = fpeek(stdin);
        if (c == '[') {
            uint32_t indicator = parse_indicator();
            UIntVector buttons = parse_buttons();
            // printf("%d\n", indicator);
            // UIntVector_show(buttons, stdout); puts("");
            fflush(stdout);
            int solution = solve(indicator, &buttons);
            r += solution;
            // printf("solution=%d\n", solution);
            skip_to_line_end();
        } else {
            break;
        }
    }
    printf("%d\n", r);
    return 0;
}