Skip to content

Instantly share code, notes, and snippets.

@xavierLowmiller
Created December 23, 2020 23:26
Show Gist options
  • Save xavierLowmiller/f2be4bd1b14e38467aa864fcec1e4821 to your computer and use it in GitHub Desktop.
Save xavierLowmiller/f2be4bd1b14e38467aa864fcec1e4821 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// https://adventofcode.com/2020/day/23
int lut[1000001];
int current = 0;
void setup(const char *numbers) {
for (int i = 0; i < 1000001; i++) {
lut[i] = i + 1;
}
int nf = (int)strlen(numbers);
current = 3;
for (int i = 0; i < nf; i++) {
int value1 = numbers[i] - 48;
int value2 = numbers[i + 1] - 48;
if (value2 == -48) {
lut[value1] = nf + 1;
lut[1000000] = numbers[0] - 48;
} else {
lut[value1] = value2;
}
}
}
void playRound() {
int numberToRelocate1 = lut[current];
int numberToRelocate2 = lut[numberToRelocate1];
int numberToRelocate3 = lut[numberToRelocate2];
int modulo = 1000000;
int destination = (current + modulo - 2) % modulo + 1;
while (numberToRelocate1 == destination ||
numberToRelocate2 == destination ||
numberToRelocate3 == destination) {
destination = (destination + modulo - 2) % modulo + 1;
}
lut[current] = lut[numberToRelocate3];
lut[numberToRelocate3] = lut[destination];
lut[destination] = numberToRelocate1;
current = lut[current];
}
int main(int argc, const char * argv[]) {
if (argc < 2) {
printf("usage: %s SINGLE_DIGIT_CUP_LABELS\n", *argv);
return 1;
}
setup(argv[1]);
for (int i = 0; i < 10000000; i++) {
playRound();
}
long number1 = lut[1];
long number2 = lut[number1];
printf("Part 2: %ld\n", number1 * number2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment