C 2017 day 1
This commit is contained in:
parent
a5c6971028
commit
949dfdcea0
2
bin/run
2
bin/run
@ -16,7 +16,7 @@ fi
|
||||
|
||||
year=$(($2))
|
||||
|
||||
if [[ $year -gt 2015 ]]
|
||||
if [[ $year -lt 2015 ]]
|
||||
then
|
||||
echo "invalid year: $2"
|
||||
echo "must be 2015 or later"
|
||||
|
43
c/2017/1/problem.c
Normal file
43
c/2017/1/problem.c
Normal file
@ -0,0 +1,43 @@
|
||||
#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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user