-
Notifications
You must be signed in to change notification settings - Fork 2
/
23.c
98 lines (84 loc) · 2.16 KB
/
23.c
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
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/cdefs.h>
#include "hugemem.h"
// const char *test_input = "389125467";
const char* input = "327465189";
static void print_raw(const int* restrict cups);
static int
parse_input(int* restrict cups, const char* restrict s) {
int i = 1;
int first = (*s++ - '0');
int prev = first;
for (; *s != '\0'; s++, i++) {
cups[prev] = (*s - '0');
prev = cups[prev];
}
// fill in remainder with incrementing values
// for (i=i+1; i < 1000000; i++) {
for (; i < 1000000; i++) {
cups[prev] = i + 1;
prev = cups[prev];
}
// last element should point to first
cups[prev] = first;
return first;
}
__attribute((unused))
static void
print_raw(const int* restrict cups) {
printf("| ");
for (int i = 1; i <= 9; i++) {
printf("%2d | ", i);
}
printf("\n| ");
for (int i = 1; i <= 9; i++) {
printf("%2d | ", cups[i]);
}
printf("\n\n");
}
__attribute((unused))
static void
print_cups(const int* restrict cups, const int first, const size_t n, const char* restrict before) {
printf("%s", before);
int c = first;
for (size_t i = 0; i < n; i++, c = cups[c]) {
printf("%s%d", i > 0 ? ", " : "", c);
}
printf("\n");
}
int day23(void) {
unsigned int *cups = (unsigned int *) hugemem(1000000 * sizeof(unsigned int));
if (!cups) {
perror("hugemem error");
return 1;
}
unsigned int current_cup = parse_input(cups, input);
unsigned int a, b, c;
unsigned int destination;
assert(current_cup == 3);
unsigned int next_cur;
for (int m = 0; m < 10000000; m++) {
a = cups[current_cup];
b = cups[a];
c = cups[b];
cups[current_cup] = next_cur = cups[c];
destination = current_cup;
do {
destination--;
if (destination < 1) {
destination = 1000000;
}
} while (destination == a || destination == b || destination == c);
cups[c] = cups[destination];
cups[destination] = a;
current_cup = next_cur;
}
unsigned int c1 = cups[1];
unsigned long r = (unsigned long)c1 * (unsigned long)cups[c1];
printf("%ld\n", r);
assert(r == 474600314018);
hugemem_free(cups, 1000000 * sizeof(int));
return 0;
}