-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc6.c
38 lines (33 loc) · 1.21 KB
/
aoc6.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
#include <math.h>
#include <stdio.h>
typedef struct {
double time;
double dist;
}Race;
Race races[ 4 ] = { { 40, 219}, { 81, 1012}, { 77, 1365}, { 72, 1089 }};
void get_solution( double race_time, double previous_record, double* min_hold, double* max_hold ) {
// The the dist is
// dist = -x*x+ race_time*x - previous_record
// where x is hold_time, Use quadratic root finder to find both solutions.
double a = -1.0;
double b = race_time;
double c = -previous_record;
double min_h = (-b + sqrt( b*b - 4*a*c))/(2.0*a);
double max_h = (-b - sqrt( b*b - 4*a*c))/(2.0*a);
// All integer numbers between the roots is the solution
*min_hold = floor( min_h ) + 1.0;
*max_hold = ceil( max_h ) - 1.0;
}
int main()
{
double winning_ways = 1.0;
double min_hold, max_hold;
for( int race_no=0; race_no<4; race_no ++) {
get_solution( races[ race_no].time, races[race_no].dist, &min_hold, &max_hold);
winning_ways *= ( max_hold - min_hold) + 1.0;
}
printf( "Part1 %.0f \n", winning_ways);
get_solution( 40817772.0, 219101213651089.0, &min_hold, &max_hold);
winning_ways = ( max_hold - min_hold) + 1.0;
printf( "Part2 %.0f \n", winning_ways);
}