~quf/computers-are-fast-2020

computers-are-fast-2020/src/22.c -rw-r--r-- 4.8 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
201
202
203
204
205
206
207
208
209
210
211
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>

#define MAX_DECK_SIZE 50
#define MAX_BIN_SIZE 5
#define N_BINS (1<<12) /* NEEDS TO BE A POWER OF TWO */

typedef struct {
  uint8_t size;
  uint8_t data[MAX_DECK_SIZE];
} deck_t;

typedef struct {
  size_t size;
  deck_t data1[MAX_BIN_SIZE];
  deck_t data2[MAX_BIN_SIZE];
} bin_t;

typedef struct {
  bin_t *bins[N_BINS];
} hashset_t;

void *ensure_allocated(void *p, size_t size) {
  if (p != NULL) {
    return p;
  }
  void *ptr = malloc(size);
  if (ptr == NULL) {
    fprintf(stderr, "Allocation error: %s\n", strerror(errno));
    exit(1);
  }
  memset(ptr, 0, size);
  return ptr;
}

bool deck_eq(const deck_t *d1, const deck_t *d2) {
  if (d1->size != d2->size) {
    return false;
  }
  for (size_t i = 0; i < d1->size; ++i) {
    if (d1->data[i] != d2->data[i]) {
      return false;
    }
  }
  return true;
}

uint32_t hs_hash(const deck_t *deck1, const deck_t *deck2) {
  /* specialized djb2 from http://www.cse.yorku.ca/~oz/hash.html */
  uint32_t h = 5381;
  h = ((h << 5) + h) + (uint32_t) deck1->size;
  h = ((h << 5) + h) + (uint32_t) deck2->size;
  for (size_t i = 0; i < (size_t) deck1->size; ++i) {
    h = ((h << 5) + h) + deck1->data[i];
    h = ((h << 5) + h) + deck2->data[i];
  }
  return h;
}

bool insert_unless_present(hashset_t *hs, const deck_t *deck1, const deck_t *deck2) {
  size_t i_bin = hs_hash(deck1, deck2) & (N_BINS-1);
  hs->bins[i_bin] = ensure_allocated(hs->bins[i_bin], sizeof *hs->bins[i_bin]);
  for (size_t i = 0; i < hs->bins[i_bin]->size; ++i) {
    if (deck_eq(deck1, hs->bins[i_bin]->data1 + i) && deck_eq(deck2, hs->bins[i_bin]->data2 + i)) {
      return false;
    }
  }
  if (hs->bins[i_bin]->size >= MAX_BIN_SIZE) {
    fprintf(stderr, "Error: Bin overflow.\n");
    exit(EXIT_FAILURE);
  }
  hs->bins[i_bin]->data1[hs->bins[i_bin]->size] = *deck1;
  hs->bins[i_bin]->data2[hs->bins[i_bin]->size] = *deck2;
  ++hs->bins[i_bin]->size;
  return true;
}

void hs_free(hashset_t *hs) {
  for (size_t i = 0; i < N_BINS; ++i) {
    free(hs->bins[i]);
  }
  free(hs);
}

unsigned long score(const deck_t *deck) {
  size_t s = 0;
  for (size_t i = 0; i < deck->size; ++i) {
    s += deck->data[i] * (deck->size - i);
  }
  return s;
}

unsigned char pop_front(deck_t *d) {
  unsigned char n = d->data[0];
  for (size_t i = 1; i < d->size; ++i) {
    d->data[i-1] = d->data[i];
  }
  --d->size;
  return n;
}

void push_back(deck_t *d, unsigned char a, unsigned char b) {
  d->data[d->size++] = a;
  d->data[d->size++] = b;
}

deck_t take(const deck_t *d, unsigned char n) {
  deck_t ret = { .size = n };
  for (size_t i = 0; i < n; ++i) {
    ret.data[i] = d->data[i];
  }
  return ret;
}

unsigned char max_of_first(const deck_t *deck, unsigned char n) {
  unsigned char max = 0;
  for (size_t i = 0; i < n; ++i) {
    max = (deck->data[i] > max)? deck->data[i] : max;
  }
  return max;
}

int run(deck_t *deck1, deck_t *deck2, bool recur) {
  hashset_t *seen = ensure_allocated(NULL, sizeof *seen);
  while (deck1->size > 0 && deck2->size > 0) {
    if (!insert_unless_present(seen, deck1, deck2)) {
      hs_free(seen);
      return 1;
    }
    unsigned char c1 = pop_front(deck1);
    unsigned char c2 = pop_front(deck2);
    if (recur && c1 <= deck1->size && c2 <= deck2->size) {
      int winner = 0;
      if (max_of_first(deck1, c1) > max_of_first(deck2, c2)) {
        winner = 1;
      }
      else {
        deck_t d1 = take(deck1, c1);
        deck_t d2 = take(deck2, c2);
        winner = run(&d1, &d2, recur);
      }
      if (winner == 1) {
        push_back(deck1, c1, c2);
      }
      else {
        push_back(deck2, c2, c1);
      }
    }
    else if (c1 > c2) {
      push_back(deck1, c1, c2);
    }
    else {
      push_back(deck2, c2, c1);
    }
  }
  hs_free(seen);
  if (deck1->size > 0) {
    return 1;
  }
  else {
    return 2;
  }
}

unsigned long part1(deck_t deck1, deck_t deck2) {
  if (run(&deck1, &deck2, false) == 1) {
    return score(&deck1);
  }
  else {
    return score(&deck2);
  }
}

unsigned long part2(deck_t deck1, deck_t deck2) {
  if (run(&deck1, &deck2, true) == 1) {
    return score(&deck1);
  }
  else {
    return score(&deck2);
  }
}

int main(void) {
  deck_t d1 = { .size = 0 };
  deck_t d2 = { .size = 0 };

  {
    size_t n = 0;
    (void) scanf("%*s %*s\n");
    while (d1.size < MAX_DECK_SIZE && scanf("%zu\n", &n) == 1) {
      d1.data[d1.size++] = (unsigned char) n;
    }
    (void) scanf("%*s %*s\n");
    while (d2.size < MAX_DECK_SIZE && scanf("%zu\n", &n) == 1) {
      d2.data[d2.size++] = (unsigned char) n;
    }
    if (ferror(stdin) || !feof(stdin)) {
      fprintf(stderr, "Input error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  }

  printf("%lu\n", part1(d1, d2));
  printf("%lu\n", part2(d1, d2));

  return 0;
}