Skip to content

Instantly share code, notes, and snippets.

@st3fan
Created November 14, 2021 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st3fan/d00ee9f8693ccd95795eefd6abc80d98 to your computer and use it in GitHub Desktop.
Save st3fan/d00ee9f8693ccd95795eefd6abc80d98 to your computer and use it in GitHub Desktop.
Advent of Code - 2015 Day 20
/* Advent of Code - 2015 Day 20 */
#include <stdio.h>
#include <dispatch/dispatch.h>
int number_of_presents(int house) {
int n = 0;
for (int elf = 1; elf <= house; elf++) {
if ((house % elf) == 0) {
n += (10 * elf);
}
}
return n;
}
int main() {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
__block int lowest_house = 10000000;
for (int i = 0; i < (10000000 / 50000); i++) {
dispatch_group_async(group, queue, ^{
int start = i * 50000;
for (int house = start; house <= start + 49999; house++) {
int n = number_of_presents(house);
if (n >= 33100000) {
dispatch_async(queue, ^{
printf("Done with %d\n", i);
if (house < lowest_house) {
lowest_house = house;
}
});
return;
}
}
});
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
dispatch_release(group);
printf("Part one: %d\n", lowest_house);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment