aoc_omni/c/2020/3/main.c
2025-11-30 21:22:21 -05:00

55 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../lib/util.h"
int process_slope(int slope[]) {
InputWithSize *input_with_size = load_input_with_size();
char *data_buffer = input_with_size->data_buffer;
int input_size = input_with_size -> size;
char *first_line = strtok(data_buffer, "\n");
const int course_width = strlen(first_line) + 1; // Account for \n
int position[2] = {0, 0};
int index = (position[0] * course_width) + position[1];
int trees_hit = 0;
do {
if (data_buffer[index] == '#') trees_hit++;
position[0] += slope[0];
position[1] += slope[1];
position[1] %= (course_width - 1);
index = (position[0] * course_width) + position[1];
} while (index < input_size);
free_input_with_size(input_with_size);
return trees_hit;
}
int part_1() {
int slope[2] = {1, 3};
return process_slope(slope);
}
int part_2() {
int slopes[5][2] = {
1, 1,
1, 3,
1, 5,
1, 7,
2, 1
};
int product = 1;
for (int i = 0; i < 5; i++) {
product *= process_slope(slopes[i]);
}
return product;
}
int main() {
printf("Part 1: %d\n", part_1());
printf("Part 2: %d\n", part_2());
return 0;
}