summaryrefslogtreecommitdiffstats
path: root/20/solution.c
blob: 73beb80b67781343be4ca4316f9234be52de32da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

void *emalloc(size_t s)
{
	void *p = malloc(s);
	if (p != NULL) return p;
	fprintf(stderr, "Ran out of memory allocating a buffer of size %zu\n", s);
	exit(EXIT_FAILURE);
}

long part1(long input)
{
	long res = -1;
	long *houses = emalloc(sizeof(long) * input / 10);
	#pragma omp parallel for
	for (long i = 1; i < input / 10; i++)
		for (long j = i; j < input / 10; j += i)
			houses[j] += i * 10;
	for (long i = 1; i < input / 10; i++) {
		if (houses[i] >= input) {
			res = i;
			break;
		}
	}
	free(houses);
	return res;
}

long part2(long input)
{
	long res = -1;
	long *houses = emalloc(sizeof(long) * input / 10);
	#pragma omp parallel for
	for (long i = 1; i < input / 10; i++)
		for (long j = i, k = 0; j < input / 10 && k < 50; j += i, k++)
			houses[j] += i * 11;
	for (long i = 1; i < input / 10; i++) {
		if (houses[i] >= input) {
			res = i;
			break;
		}
	}
	free(houses);
	return res;
}

int main(void)
{
	long input = 33100000;
	printf("%ld\n", part1(input));
	printf("%ld\n", part2(input));
}