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

90 lines
1.6 KiB
C

#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include "../lib/util.h"
typedef uint question_bits;
int question_score(question_bits q) {
int score = 0;
while(q) {
score += q & 1;
q >>= 1;
}
return score;
}
int part_1() {
InputWithSize *input = load_input_with_size();
bool questions[26] = {0};
question_bits q = 0;
int sum = 0;
int offset = 0;
do {
char c = *(input->data_buffer + offset);
if (c == '\n') {
if (*(input->data_buffer + offset + 1) == '\n') {
int count = question_score(q);
q = 0;
sum += count;
offset++;
}
} else {
q |= 1 << (c - 'a');
}
offset++;
} while(offset < input->size);
int count = question_score(q);
sum += count;
sum--; // No idea
free_input_with_size(input);
return sum;
}
int part_2() {
InputWithSize *input = load_input_with_size();
bool questions[26] = {0};
question_bits group = UINT_MAX;
question_bits member = 0;
int sum = 0;
int offset = 0;
do {
char c = *(input->data_buffer + offset);
if (c == '\n') {
if (*(input->data_buffer + offset + 1) == '\n') {
group &= member;
int count = question_score(group);
group = UINT_MAX;
member = 0;
sum += count;
offset++;
} else {
group &= member;
member = 0;
}
} else {
member |= (1 << (c - 'a'));
}
offset++;
} while(offset < input->size);
char c = *(input->data_buffer + offset);
member = 0;
int count = question_score(group);
sum += count;
free_input_with_size(input);
return sum;
}
int main() {
printf("Part 1: %d\n", part_1());
printf("Part 2: %d\n", part_2());
}