diff --git a/day6/c/day6 b/day6/c/day6 new file mode 100755 index 0000000..b6d44d2 Binary files /dev/null and b/day6/c/day6 differ diff --git a/day6/c/day6.c b/day6/c/day6.c new file mode 100644 index 0000000..891e7ea --- /dev/null +++ b/day6/c/day6.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +#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); +} diff --git a/day6/input.txt b/day6/input.txt new file mode 100644 index 0000000..52c7359 --- /dev/null +++ b/day6/input.txt @@ -0,0 +1,2 @@ +Time: 41 66 72 66 +Distance: 244 1047 1228 1040 diff --git a/day6/input2.txt b/day6/input2.txt new file mode 100644 index 0000000..2719a83 --- /dev/null +++ b/day6/input2.txt @@ -0,0 +1,2 @@ +Time: 41667266 +Distance: 244104712281040 diff --git a/day6/sample.txt b/day6/sample.txt new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/day6/sample.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200