Advent of Code - 2015 Day 20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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