-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.c
More file actions
54 lines (45 loc) · 1.38 KB
/
Copy path23.c
File metadata and controls
54 lines (45 loc) · 1.38 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define VAL(n) (n - list) // assumes the list is visible
int INPUT[] = {4,6,3,5,2,8,1,7,9};
typedef struct Node { struct Node *next; } Node;
Node* build_list(int size) {
Node *list = malloc(sizeof(Node) * size);
Node *curr = list + INPUT[0];
for (int i = 1; i < size; curr = curr->next, ++i)
curr->next = list + (i < 9 ? INPUT[i] : i + 1);
curr->next = list + INPUT[0];
return list;
}
Node* simulate_game(int size, int rounds) {
Node *list = build_list(size);
Node *curr = list + INPUT[0];
for (int i = 0; i < rounds; curr = curr->next, ++i) {
Node *a = curr->next, *b = a->next, *c = b->next;
int t = (VAL(curr) == 1 ? size : VAL(curr) - 1);
while (t == VAL(a) || t == VAL(b) || t == VAL(c))
t = (t == 1 ? size : t - 1);
Node *dest = list + t;
curr->next = c->next;
c->next = dest->next;
dest->next = a;
}
return list;
}
int part_one(void) {
Node *list = simulate_game(9, 100);
int ans = 0;
for (Node *n = list[1].next; n != list + 1; n = n->next)
ans = ans * 10 + VAL(n);
return ans;
}
long part_two(void) {
Node *list = simulate_game(1000000, 10000000);
return VAL(list[1].next) * VAL(list[1].next->next);
}
int main() {
printf("Part one: %d\n", part_one());
printf("Part two: %ld\n", part_two());
printf("Time: %ldms\n", clock() / (CLOCKS_PER_SEC / 1000));
}