Day20
This commit is contained in:
5000
day20/input.txt
Normal file
5000
day20/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
BIN
day20/shuffle
Executable file
BIN
day20/shuffle
Executable file
Binary file not shown.
104
day20/shuffle.c
Normal file
104
day20/shuffle.c
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Finally a task that is favoring C
|
||||||
|
#define BUFFER_SIZE 64
|
||||||
|
#define MAX_LINES 8192
|
||||||
|
// MIX_COUNT and DECRYPT_KEY is 1 for Part 1
|
||||||
|
#define MIX_COUNT 10
|
||||||
|
#define DECRYPT_KEY 811589153
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
char buf[BUFFER_SIZE], *p, c;
|
||||||
|
memset(buf, 0, BUFFER_SIZE);
|
||||||
|
p = buf;
|
||||||
|
long long lines[MAX_LINES], line_count = 0;
|
||||||
|
|
||||||
|
while ((c = getchar()) != EOF) {
|
||||||
|
*p++ = c;
|
||||||
|
if (c == '\n') {
|
||||||
|
sscanf(buf, "%lli", &lines[line_count]);
|
||||||
|
lines[line_count] *= DECRYPT_KEY;
|
||||||
|
line_count++;
|
||||||
|
memset(buf, 0, BUFFER_SIZE);
|
||||||
|
p = buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create an array of pointers to keep track of the original order
|
||||||
|
long long **order = (long long**)malloc(line_count * sizeof(long long*));
|
||||||
|
for (int i = 0; i < line_count; i++) {
|
||||||
|
order[i] = &lines[i];
|
||||||
|
};
|
||||||
|
long long *head = &lines[line_count-1];
|
||||||
|
|
||||||
|
// Shuffle
|
||||||
|
for (int ll = 0; ll < MIX_COUNT; ll++) {
|
||||||
|
for (int i = 0; i < line_count; i++) {
|
||||||
|
// Remove element from its original position
|
||||||
|
long long element = *order[i];
|
||||||
|
long long *q = order[i];
|
||||||
|
while (q != head) {
|
||||||
|
// Shift elements back
|
||||||
|
*q = *(q+1);
|
||||||
|
q++;
|
||||||
|
}
|
||||||
|
// Shift pointers
|
||||||
|
for (int j = 0; j < line_count; j++) {
|
||||||
|
if (order[j] > order[i]) {
|
||||||
|
order[j]--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Move where we need to insert
|
||||||
|
order[i] += element;
|
||||||
|
// Wrap around at the end
|
||||||
|
// I don't get this wrapping logic that's in the example
|
||||||
|
// it doesn't make any sense that I can not shift an element to the
|
||||||
|
// last position because it already wraps to the first element
|
||||||
|
if (order[i] > head) {
|
||||||
|
order[i] = &lines[(order[i] - head) % (line_count-1)];
|
||||||
|
} else if (order[i] <= lines) {
|
||||||
|
order[i] = head - (lines - order[i]) % (line_count-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push every element back at insertion point
|
||||||
|
q = head;
|
||||||
|
while (q >= order[i] + 1) {
|
||||||
|
// Shift elements
|
||||||
|
*q = *(q-1);
|
||||||
|
q--;
|
||||||
|
}
|
||||||
|
// Shift pointers
|
||||||
|
for (int j = 0; j < line_count; j++) {
|
||||||
|
if (order[j] >= order[i] && i != j) {
|
||||||
|
order[j]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert element
|
||||||
|
*order[i] = element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find 0
|
||||||
|
long long *q = lines;
|
||||||
|
while (*q != 0) { q++; }
|
||||||
|
|
||||||
|
long long *th[3];
|
||||||
|
|
||||||
|
th[0] = q + 1000;
|
||||||
|
th[1] = q + 2000;
|
||||||
|
th[2] = q + 3000;
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
if (th[i] > head) {
|
||||||
|
th[i] = lines + ((th[i] - head) % line_count) - 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%lli\n", *(th[0])+*(th[1])+*(th[2]));
|
||||||
|
free(order);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user