~quf/computers-are-fast-2020

computers-are-fast-2020/src/07.c -rw-r--r-- 6.0 KiB
6f56515eLukas Himbert Draw the rest of the fine owl. 1 year, 7 months ago
                                                                                
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>

#define LENGTH(a) (sizeof (a) / sizeof *(a))

#define die(...) do { \
    fprintf(stderr, __VA_ARGS__); \
    exit(EXIT_FAILURE); \
  } while (0)

#define die_unless(cond, ...) do { \
    if (!(cond)) { \
      die(__VA_ARGS__); \
    } \
  } while (0)

#define die_if(cond, ...) die_unless(!(cond), __VA_ARGS__)

typedef struct {
  uint16_t out_nodes[20];
  uint8_t out_numbers[20];
} rule_t;

const char * const modifiers[] = { "bright", "clear", "dark", "dim", "dotted", "drab", "dull", "faded", "light", "mirrored", "muted", "pale", "plaid", "posh", "shiny", "striped", "vibrant", "wavy", };
const int modifier_bits = 5;
const char * const base_colors[] = { "aqua", "beige", "black", "blue", "bronze", "brown", "chartreuse", "coral", "crimson", "cyan", "fuchsia", "gold", "gray", "green", "indigo", "lavender", "lime", "magenta", "maroon", "olive", "orange", "plum", "purple", "red", "salmon", "silver", "tan", "teal", "tomato", "turquoise", "violet", "white", "yellow", };
const int base_color_bits = 6;

size_t bisect(const char * const arr[], size_t len, const char *s) {
  size_t lo = 0;
  size_t hi = len - 1;
  if (strcmp(s, arr[lo]) == 0) {
    return lo;
  }
  if (strcmp(s, arr[hi]) == 0) {
    return hi;
  }
  while (1) {
    /* we loop forever if "s" is not found. */
    size_t mid = lo + (hi - lo) / 2;
    int x = strcmp(s, arr[mid]);
    if (x == 0) {
      return mid;
    }
    else if (x < 0) {
      hi = mid;
    }
    else {
      lo = mid;
    }
  }
}

uint16_t get_id(uint16_t mod_index, uint16_t base_index) {
  uint16_t x = (mod_index << base_color_bits) | base_index;
  die_unless(x < (1<<11), "Internal error, this should never happen.\n");
  return x;
}

size_t next_space(const char *s) {
  size_t off = 0;
  for (; s[off] && s[off] != ' '; ++off);
  return off;
}

size_t next_line(const char *s) {
  size_t off = 0;
  for (; s[off] && s[off] != '\n'; ++off);
  return off;
}

void read_input(rule_t *rules_contains, rule_t *rules_contained) {
  char line[1<<8];
  while (fgets(line, sizeof line, stdin) != NULL) {
    char *word;
    /* parse rule using a handrolled state machine */
    rule_t r = { { 0, }, { 0, } };
    uint16_t mod = 0;
    uint16_t base = 1;
    uint16_t rule_id = 0;
    uint8_t num = 0;
    int state = 0;
    while ((word = strtok(state ? NULL : line, " \n")) != 0) {
      switch (state) {
        case 0: /* fall through */
        case 8:
          mod = bisect(modifiers, LENGTH(modifiers), word);
          ++state;
          break;
        case 1:
          base = bisect(base_colors, LENGTH(base_colors), word);
          rule_id = get_id(mod, base);
          ++state;
          break;
        case 4:
          {
            int n = atoi(word);
            if (n > 0) {
              die_if(n > UINT8_MAX, "i");
              num = (uint8_t) n;
              state = 8;
            }
            else {
              ++state;
            }
          }
          break;
        case 7:
          die("Error: unexpected word in input: %s\n", word);
          break;
        case 9:
          base = bisect(base_colors, LENGTH(base_colors), word);
          /* got number and color ID of the inner bag, now store it */
          {
            size_t i = 0;
            for (; i < LENGTH(r.out_numbers) && r.out_numbers[i] != 0; ++i);
            die_unless(i < LENGTH(r.out_numbers), "Error: too many for one bag");
            r.out_numbers[i] = num;
            r.out_nodes[i] = get_id(mod, base);
          }
          ++state;
          break;
        case 11:
          num = atoi(word);
          state = 8;
          break;
        case 2: /* fall through*/
        case 3: /* fall through*/
        case 5: /* fall through*/
        case 6: /* fall through*/
        case 10:
          ++state;
          break;
        default:
          die("Internal error: this should never happen.\n");
      }
    }
    die_unless(state == 7 || state == 11, "Unexpected end of input (in state %d)\n",  state);
    /* add rules to the first graph ("contains") */
    memcpy(rules_contains + rule_id, &r, sizeof r);
    /* and the second ("contained") */
    for (size_t i = 0; i < LENGTH(r.out_nodes); ++i) {
      if (r.out_numbers[i] == 0) {
        break;
      }
      uint16_t id = r.out_nodes[i];
      size_t j = 0;
      /* find a place to store the reverse connection */
      for (j = 0; j < LENGTH(r.out_nodes) && rules_contained[id].out_numbers[j] != 0; ++j);
      die_unless(j < LENGTH(r.out_nodes), "Error: too many reverse connections.\n");
      rules_contained[id].out_numbers[j] = 1; /* or any nonzero number */
      rules_contained[id].out_nodes[j] = rule_id;
    }
  }
  die_if(ferror(stdin), "Read error: %s", strerror(errno));
}

size_t part1(rule_t *rules, uint16_t id) {
  bool visited[1<<11] = { 0, };
  uint16_t to_visit[1<<7] = { 0, };
  to_visit[0] = id;
  size_t to_visit_len = 1;
  size_t sum = 0;
  while (to_visit_len > 0) {
    uint16_t id = to_visit[--to_visit_len];
    for (size_t i = 0; i < LENGTH(rules[id].out_nodes) && rules[id].out_numbers[i] != 0; ++i) {
      die_unless(to_visit_len < LENGTH(to_visit) - 1, "Error: ran out of space.\n");
      size_t new_id = rules[id].out_nodes[i];
      if (!visited[new_id]) {
        visited[new_id] = true;
        sum += 1;
        to_visit[to_visit_len++] = new_id;
        continue;
      }
    }
  }
  return sum;
}

size_t part2(const rule_t *rules, uint16_t id) {
  size_t sum = 1;
  for (size_t i = 0; i < LENGTH(rules[id].out_nodes) && rules[id].out_nodes[i] != 0; ++i) {
    sum += rules[id].out_numbers[i] * part2(rules, rules[id].out_nodes[i]);
  }
  return sum;
}

int main(void) {
  rule_t rules_contains[1<<11] = { 0, };
  rule_t rules_contained[1<<11] = { 0, };
  read_input(rules_contains, rules_contained);
  const uint16_t shiny_gold_id = get_id(bisect(modifiers, LENGTH(modifiers), "shiny"), bisect(base_colors, LENGTH(base_colors), "gold"));
  printf("%zu\n", part1(rules_contained, shiny_gold_id));
  printf("%zu\n", part2(rules_contains, shiny_gold_id) - 1);
  return 0;
}