aboutsummaryrefslogtreecommitdiff
path: root/12/part1.c
blob: e7f8fc25c72ec83b88b30ecd78202ac9b906b429 (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
#include <algds/vec.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <ctype.h>

#include <algds/str.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;
}

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

int parse_piece() {
    skip_to_line_end();
    int count = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            int c = getchar();
            if (c == '#') count++;
        }
        expect_char('\n');
    }
    expect_char('\n');
    return count;
}

typedef struct {
    int width;
    int height;
    int pieces[6];
} Case;

Case parse_case() {
    Case ret;
    ret.width = parse_integer();
    expect_char('x');
    ret.height = parse_integer();
    expect_char(':');
    expect_char(' ');
    ret.pieces[0] = parse_integer();
    expect_char(' ');
    ret.pieces[1] = parse_integer();
    expect_char(' ');
    ret.pieces[2] = parse_integer();
    expect_char(' ');
    ret.pieces[3] = parse_integer();
    expect_char(' ');
    ret.pieces[4] = parse_integer();
    expect_char(' ');
    ret.pieces[5] = parse_integer();
    int c = fpeek(stdin);
    if (c == '\n') {
        getchar();
    } else if (c == EOF) {
        return ret;
    } else {
        PANIC;
    }
    return ret;
}

int main () {
    int pieces[6];
    for (int i = 0; i < 6; i++) {
        pieces[i] = parse_piece();
    }
    int ret = 0;
    while (1) {
        int c = fpeek(stdin);
        if (c == EOF) {
            break;
        }
        Case cur = parse_case();
        int need = 0;
        for (int i = 0; i < 6; i++) {
            need += cur.pieces[i] * pieces[i];
        }
        if (cur.height * cur.width > need) {
            ret++;
        }
    }
    printf("%d\n", ret);
}