Day6 C
This commit is contained in:
BIN
day6/c/day6
Executable file
BIN
day6/c/day6
Executable file
Binary file not shown.
66
day6/c/day6.c
Normal file
66
day6/c/day6.c
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#define LINE_MAX_LENGTH 256
|
||||
#define MAX_TIMES 16
|
||||
#define NUM_0_CHARCODE 48
|
||||
#define NUM_9_CHARCODE NUM_0_CHARCODE + 9
|
||||
|
||||
int score(int max_duration, int hold_duration);
|
||||
|
||||
int main() {
|
||||
char *p, *buf, c;
|
||||
|
||||
buf = (char *)malloc(LINE_MAX_LENGTH);
|
||||
memset(buf, 0, LINE_MAX_LENGTH);
|
||||
p = buf;
|
||||
|
||||
unsigned long first = 1;
|
||||
unsigned long times[MAX_TIMES], times_num = 0;
|
||||
unsigned long records[MAX_TIMES], records_num = 0;
|
||||
|
||||
while ((c = getchar()) != EOF) {
|
||||
*p++ = c;
|
||||
if (c == '\n') {
|
||||
p = buf;
|
||||
while (*p != '\n') {
|
||||
while (*p < NUM_0_CHARCODE || *p > NUM_9_CHARCODE) p++;
|
||||
|
||||
if (first) {
|
||||
sscanf(p, "%lu", ×[times_num]);
|
||||
times_num++;
|
||||
} else {
|
||||
sscanf(p, "%lu", &records[records_num]);
|
||||
records_num++;
|
||||
}
|
||||
|
||||
while (*p >= NUM_0_CHARCODE && *p <= NUM_9_CHARCODE) p++;
|
||||
}
|
||||
|
||||
if (first) {
|
||||
first = 0;
|
||||
}
|
||||
memset(buf, 0, LINE_MAX_LENGTH);
|
||||
p = buf;
|
||||
}
|
||||
}
|
||||
|
||||
// Part 1
|
||||
unsigned long part1 = 1;
|
||||
for (unsigned long i = 0; i < times_num; i++) {
|
||||
// quick mafs
|
||||
unsigned long min_d = (unsigned long)ceil((times[i] - sqrt(times[i] * times[i] - 4 * (records[i]+1))) / 2);
|
||||
unsigned long max_d = (unsigned long)floor((times[i] + sqrt(times[i] * times[i] - 4 * (records[i]+1))) / 2);
|
||||
part1 *= (max_d - min_d + 1);
|
||||
}
|
||||
|
||||
printf("%lu\n", part1);
|
||||
|
||||
free(buf);
|
||||
}
|
||||
|
||||
int score(int max_duration, int hold_duration) {
|
||||
return hold_duration * (max_duration - hold_duration);
|
||||
}
|
||||
2
day6/input.txt
Normal file
2
day6/input.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Time: 41 66 72 66
|
||||
Distance: 244 1047 1228 1040
|
||||
2
day6/input2.txt
Normal file
2
day6/input2.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Time: 41667266
|
||||
Distance: 244104712281040
|
||||
2
day6/sample.txt
Normal file
2
day6/sample.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
Time: 7 15 30
|
||||
Distance: 9 40 200
|
||||
Reference in New Issue
Block a user