-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.c
57 lines (52 loc) · 2.43 KB
/
day2.c
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
55
56
57
#include <stdio.h>
#define ROW_LENGTH 16
const int input[][ROW_LENGTH] = {{121, 59, 141, 21, 120, 67, 58, 49, 22, 46, 56, 112, 53, 111, 104, 130},
{1926, 1910, 760, 2055, 28, 2242, 146, 1485, 163, 976, 1842, 1982, 137, 1387, 162, 789},
{4088, 258, 2060, 1014, 4420, 177, 4159, 194, 2794, 4673, 4092, 681, 174, 2924, 170, 3548},
{191, 407, 253, 192, 207, 425, 580, 231, 197, 382, 404, 472, 164, 571, 500, 216},
{4700, 1161, 168, 5398, 5227, 5119, 252, 2552, 4887, 5060, 1152, 3297, 847, 4525, 220, 262},
{2417, 992, 1445, 184, 554, 2940, 209, 2574, 2262, 1911, 2923, 204, 2273, 2760, 506, 157},
{644, 155, 638, 78, 385, 408, 152, 360, 588, 618, 313, 126, 172, 220, 217, 161},
{227, 1047, 117, 500, 1445, 222, 29, 913, 190, 791, 230, 1281, 1385, 226, 856, 1380},
{436, 46, 141, 545, 122, 86, 283, 124, 249, 511, 347, 502, 168, 468, 117, 94},
{2949, 3286, 2492, 2145, 1615, 159, 663, 1158, 154, 939, 166, 2867, 141, 324, 2862, 641},
{1394, 151, 90, 548, 767, 1572, 150, 913, 141, 1646, 154, 1351, 1506, 1510, 707, 400},
{646, 178, 1228, 1229, 270, 167, 161, 1134, 193, 1312, 1428, 131, 1457, 719, 1288, 989},
{1108, 1042, 93, 140, 822, 124, 1037, 1075, 125, 941, 1125, 298, 136, 94, 135, 711},
{112, 2429, 1987, 2129, 2557, 1827, 477, 100, 78, 634, 352, 1637, 588, 77, 1624, 2500},
{514, 218, 209, 185, 197, 137, 393, 555, 588, 569, 710, 537, 48, 309, 519, 138},
{1567, 3246, 4194, 151, 3112, 903, 1575, 134, 150, 4184, 3718, 4077, 180, 4307, 4097, 1705}};
int part1(const int (*row)[ROW_LENGTH]) {
int min = (*row)[0], max = (*row)[0];
for(size_t i = 0; i < ROW_LENGTH; i++) {
if((*row)[i] < min)
min = (*row)[i];
else if((*row)[i] > max)
max = (*row)[i];
}
return max - min;
}
int part2(const int (*row)[ROW_LENGTH]) {
for(size_t i = 0; i < ROW_LENGTH - 1; i++) {
for(size_t j = i + 1; j < ROW_LENGTH; j++) {
int dividend, divisor;
if((*row)[i] < (*row)[j]) {
divisor = (*row)[i];
dividend = (*row)[j];
}
else {
divisor = (*row)[j];
dividend = (*row)[i];
}
if(!(dividend % divisor))
return dividend / divisor;
}
}
}
int main() {
int checksum = 0;
for(size_t i = 0; i < sizeof(input) / sizeof(input[0]); i++) {
checksum += part2(&input[i]);
}
printf("%d", checksum);
}