mirror of
https://github.com/peter-tanner/advent-of-code-2021.git
synced 2024-11-30 11:10:20 +08:00
Day 1 solutions
This commit is contained in:
commit
f717130799
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
*
|
||||
!/**/
|
||||
!*.*
|
||||
|
||||
*.out
|
||||
*.swp
|
||||
.vscode/
|
21
1/1-1.c
Normal file
21
1/1-1.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
FILE *p_file = fopen("input", "r");
|
||||
char line[50];
|
||||
uint increases = 0;
|
||||
uint previous_depth = 0;
|
||||
while (fgets(line, sizeof line, p_file) != NULL)
|
||||
{
|
||||
if (atoi(line) > previous_depth)
|
||||
{
|
||||
increases++;
|
||||
}
|
||||
previous_depth = atoi(line);
|
||||
}
|
||||
printf("%u\n", increases - 1);
|
||||
return 0;
|
||||
}
|
64
1/1-2.c
Normal file
64
1/1-2.c
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define N_WINDOWS 4
|
||||
#define WINDOW_LENGTH 3
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint sum;
|
||||
uint count;
|
||||
bool written; // IGNORE COMPARISONS AT THE START.
|
||||
} MEASUREMENT_WINDOW;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
FILE *p_file = fopen("input", "r");
|
||||
char line[50];
|
||||
uint increases = 0;
|
||||
|
||||
MEASUREMENT_WINDOW windows[N_WINDOWS];
|
||||
|
||||
uint i = 0;
|
||||
while (fgets(line, sizeof line, p_file) != NULL)
|
||||
{
|
||||
uint n = atoi(line);
|
||||
// UPDATE MEASURING WINDOWS
|
||||
for (size_t j = 0; j < N_WINDOWS; j++)
|
||||
{
|
||||
MEASUREMENT_WINDOW *window = &windows[j];
|
||||
if ((i - j) % N_WINDOWS == 0)
|
||||
{
|
||||
window->count = 0;
|
||||
window->sum = 0;
|
||||
window->written = true;
|
||||
}
|
||||
if (window->written && window->count < 3)
|
||||
{
|
||||
window->sum += n;
|
||||
window->count++;
|
||||
}
|
||||
}
|
||||
// COMPARE WINDOWS
|
||||
for (size_t j = 0; j < N_WINDOWS; j++)
|
||||
{
|
||||
MEASUREMENT_WINDOW *window = &windows[j];
|
||||
// ONE WINDOW IS READY TO BE COMPARED
|
||||
if (window->count == 3)
|
||||
{
|
||||
window->count++; // PREVENT DOUBLE-COMPARE OF WINDOW.
|
||||
MEASUREMENT_WINDOW last_window = windows[(j + N_WINDOWS - 1) % 4];
|
||||
if (last_window.written && window->written && window->sum > last_window.sum)
|
||||
{
|
||||
increases++;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
printf("%u\n", increases);
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user