-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path201701.c
56 lines (43 loc) · 974 Bytes
/
201701.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include "advent.h"
// probably big enough
#define BUFSIZE 4096
/* Builds an array of ints from input
* return the number of ints parsed
*/
static int
build_buffer(int *buffer, int bufsize, FILE *input)
{
int count = 0;
char c;
while (count < bufsize) {
c = fgetc(input);
if (c == '\n' || feof(input)) { break; }
buffer[count++] = c - '0'; // acii to digit
}
return count;
}
static int
solve(int *buffer, int count, int inc)
{
int x, y, sum = 0;
for (int i = 0; i < count; i++) {
x = buffer[i];
y = buffer[(i + inc) % count];
if (x == y)
sum += x;
}
return sum;
}
Solution
solve201701(FILE *input)
{
int buffer[BUFSIZE];
int count = build_buffer(buffer, BUFSIZE, input);
Solution solution = {
.part1=solve(buffer, count, 1),
.part2=solve(buffer, count, count / 2),
};
return solution;
}