-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23a.c
81 lines (72 loc) · 2.2 KB
/
day23a.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
#include "common.h"
#include "vector.h"
int main (int argc, char ** argv) {
Vector_t cups;
vector_init(&cups, 9);
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
while (isdigit(*s)) {
vector_push_back(&cups, *s-'0');
++s;
}
intptr_t minlabel = vector_min(&cups);
intptr_t maxlabel = vector_max(&cups);
size_t Icurrent = 0;
for (long int move = 1; move <= 100; ++move) {
printf("-- move %li --\ncups:", move);
for (size_t j = 0; j < cups.count; ++j) {
if (j == Icurrent) {
printf(" (%zi)", cups.data[j]);
} else {
printf(" %zi", cups.data[j]);
}
}
printf("\n");
intptr_t removed [3];
for (size_t i = 0; i < 3; ++i) {
size_t ind = (Icurrent+1) % cups.count;
removed[i] = cups.data[ind];
vector_erase(&cups, ind);
if (ind < Icurrent) {
--Icurrent;
}
}
printf("pick up:");
for (size_t j = 0; j < 3; ++j) {
printf("%s %zi", (j)?",":"", removed[j]);
}
printf("\n");
intptr_t currentlabel = cups.data[Icurrent];
intptr_t destlabel = currentlabel;
ptrdiff_t Idest;
do {
destlabel -= 1;
if (destlabel < minlabel) {
destlabel = maxlabel;
}
Idest = vector_find(&cups, destlabel);
} while (Idest < 0);
vector_insert(&cups, removed[2], Idest+1);
if (Idest+1 <= (ptrdiff_t)Icurrent) {
++Icurrent;
}
vector_insert(&cups, removed[1], Idest+1);
if (Idest+1 <= (ptrdiff_t)Icurrent) {
++Icurrent;
}
vector_insert(&cups, removed[0], Idest+1);
if (Idest+1 <= (ptrdiff_t)Icurrent) {
++Icurrent;
}
Icurrent = (Icurrent + 1) % cups.count;
printf("destination: %zi\n", destlabel);
printf("\n");
}
ptrdiff_t I1 = vector_find(&cups, 1);
ASSERT(I1 >= 0);
for (size_t i = 1; i < cups.count; ++i) {
size_t ind = (I1+i) % cups.count;
printf("%zi", cups.data[ind]);
}
printf("\n");
}