aoc-2021/src/day_6.c

68 lines
1.5 KiB
C

#include <stdio.h>
#include <stdint.h>
#include "puzzle_input.h"
#include "day.h"
#define STATES_MAX_LEN 500
#define FISH_DAYS 9
uint8_t states_g[STATES_MAX_LEN];
typedef struct {
uint64_t spawn_count[FISH_DAYS];
uint8_t day_ndx;
} fish_buffer_t;
void insert_fish_at_ndx(fish_buffer_t * buff, uint8_t day_ndx, uint64_t count) {
uint8_t norm_ndx = (buff->day_ndx + day_ndx) % FISH_DAYS;
buff->spawn_count[norm_ndx] += count;
}
uint64_t fish_today(fish_buffer_t * buff) {
return buff->spawn_count[buff->day_ndx];
}
void inc_day(fish_buffer_t * buff) {
buff->day_ndx = (buff->day_ndx + 1) % FISH_DAYS;
}
int day_6() {
parse_ret_t res;
uint16_t len;
uint16_t day = 0;
uint64_t fish_count;
fish_buffer_t fish_buffer = {0};
res = read_input_split_on("../inputs/day_6.txt", (void *)states_g, 500, parse_byte_from_buff, &len, ',');
if (res != PARSER_OK) {
printf("Unable to parse input data: %d", res);
return -1;
}
for (int initial_ndx = 0; initial_ndx < len; initial_ndx++) {
fish_buffer.spawn_count[states_g[initial_ndx]]++;
}
fish_count = len;
while (day < 256) {
uint64_t spawns_today = fish_today(&fish_buffer);
inc_day(&fish_buffer);
insert_fish_at_ndx(&fish_buffer, 6, spawns_today);
fish_count += spawns_today;
day++;
if (day == 80) {
printf("PART 1: Fish count: %lu\n", fish_count);
}
}
printf("PART 2: Fish count: %lu\n", fish_count);
return 0;
}