aoc_omni/c/2017/1/problem.c

44 lines
763 B
C
Raw Normal View History

2024-07-18 21:06:39 -04:00
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE* input = fopen("../data/2017/1/input.txt", "r");
char c1, c2, first_char;
long total;
fread(&first_char, 1, 1, input);
c2 = first_char;
while (fread(&c1, 1, 1, input) > 0 && c1 != '\n') {
if (c1 == c2) {
total += c1 - '0';
}
c2 = c1;
}
if (c2 == first_char) {
total += c2 - '0';
}
printf("Part 1: %d\n", total);
int num_chars = ftell(input);
rewind(input);
total = 0;
char *buffer = malloc(num_chars);
fread(buffer, 1, num_chars, input);
for(int i = 0; i < num_chars / 2; i++) {
c1 = buffer[i];
c2 = buffer[i + (num_chars / 2)];
if (c1 == c2) {
total += c1 - '0';
}
}
total <<= 1;
printf("Part 2: %d\n", total);
}