-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.c
110 lines (98 loc) · 2.22 KB
/
day9.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Advent of Code - Day 9
// @curiouskiwi
// 10 Dec 2020
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// assumptions based on looking at data file
#define NUMS 1000
#define LENGTH 16
#define HISTORY 25
bool valid(long num[], long n);
long parttwo (long nums[], long target, int max);
long weakness(long nums[], int length);
int main(void)
{
long nums[NUMS];
char tmp[LENGTH];
FILE *f = fopen("data.txt", "r");
if (!f) return 1;
for (int i = 0; i < NUMS; i++)
{
fgets(tmp, LENGTH, f);
nums[i] = atol(tmp);
}
long answer = 0;
int i;
for (i = HISTORY; i < NUMS; i++)
{
// find first one that doesn't meet requirements (sum of 2 in previous 25)
if (!valid(&nums[i-HISTORY], nums[i]))
{
answer = nums[i];
printf("Part 1: %ld\n", answer);
break;
}
}
// part 2
for (i = 0; i < NUMS; i++)
{
// we test each consecutive list
long result = parttwo(&nums[i], answer, NUMS - i);
if (result)
{
printf("Part 2: %ld\n", result);
break;
}
}
}
// check if n is the sum of 2 digits in previous 25 buckets
bool valid(long nums[], long n)
{
for (int i = 0; i < HISTORY; i++)
{
for (int j = i + 1; j < HISTORY; j++)
{
if (nums[i] + nums[j] == n)
{
return true;
}
}
}
return false;
}
// given an array, add numbers until we hit the target sum (or return 0)
long parttwo (long nums[], long target, int max)
{
long sum = 0;
for (int i = 0; i < max; i++)
{
sum += nums[i];
if (sum > target)
return 0;
if (sum == target)
{
// range is 0 to i inclusive
return weakness(nums, i);
}
}
return 0;
}
// gets the min and max values in a given array and returns sum
long weakness(long nums[], int length)
{
long min, max;
min = max = nums[0];
for (int i = 1; i <= length; i++)
{
if (nums[i] < min)
{
min = nums[i];
}
else if (nums[i] > max)
{
max = nums[i];
}
}
return(min + max);
}