Skip to content

Instantly share code, notes, and snippets.

@Snektron
Created December 10, 2021 17:55
Show Gist options
  • Save Snektron/bdbdaf428a96d0a14716a8228f5536bf to your computer and use it in GitHub Desktop.
Save Snektron/bdbdaf428a96d0a14716a8228f5536bf to your computer and use it in GitHub Desktop.
aoc 21 day 6 brute force
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
const char start[] = {1,3,4,1,1,1,1,1,1,1,1,2,2,1,4,2,4,1,1,1,1,1,5,4,1,1,2,1,1,1,1,4,1,1,1,4,4,1,1,1,1,1,1,1,2,4,1,3,1,1,2,1,2,1,1,4,1,1,1,4,3,1,3,1,5,1,1,3,4,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,5,5,3,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,1,1,1,1,5,1,1,1,1,1,4,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,2,4,1,5,5,1,1,5,3,4,4,4,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,5,3,1,4,1,1,2,2,1,2,2,5,1,1,1,2,1,1,1,1,3,4,5,1,2,1,1,1,1,1,5,2,1,1,1,1,1,1,5,1,1,1,1,1,1,1,5,1,4,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,5,1,1,1,1,1,1,1,5,1,1,3,1,1,1,3,1,4,2,1,5,1,3,5,5,2,1,3,1,1,1,1,1,3,1,3,1,1,2,4,3,1,4,2,2,1,1,1,1,1,1,1,5,2,1,1,1,2};
size_t num_for(size_t x) {
char* mem = mmap(NULL, 1000000000000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (mem == MAP_FAILED) {
puts(":(");
return -1;
}
mem[0] = x;
size_t fish = 1;
for (size_t i = 0; i < 256; ++i) {
printf("%lu: %lu... (%lu)\n", x, i, fish);
size_t new = fish;
for (size_t j = 0; j < fish; ++j) {
if (mem[j] == 0) {
mem[j] = 6;
++new;
} else {
--mem[j];
}
}
for (size_t j = fish; j < new; ++j) {
mem[j] = 8;
}
fish = new;
}
printf("%lu: %lu\n", x, fish);
munmap(mem, 1000000000000);
return fish;
}
int main() {
size_t aaa[7];
for (size_t i = 0; i < 7; ++i) {
aaa[i] = num_for(i);
}
size_t total = 0;
for (size_t i = 0; i < sizeof(start); ++i) {
total += aaa[start[i]];
}
printf("total: %lu\n", total);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment