-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.c
81 lines (64 loc) · 1.59 KB
/
5.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 10
#define LINE_COUNT 845
int parse(char *line);
void removeTrailingLineBreak(char *line);
void sort(int *in, int n);
int main(void)
{
FILE *in = fopen("in5", "r");
char *line = malloc((MAX_LINE + 1) * sizeof(*line));
size_t n = MAX_LINE;
int max = 0;
int all[LINE_COUNT];
int i = 0;
while (getline(&line, &n, in) > 1)
{
removeTrailingLineBreak(line);
int parsed = parse(line);
if (parsed > max)
max = parsed;
all[i++] = parsed;
}
sort(all, LINE_COUNT);
printf("Highest seat ID is: %d\n", max);
for (int i = 1; i < LINE_COUNT; i++)
if (all[i] != all[i-1] + 1)
printf("%d -> %d => Suspect: %d\n", all[i-1], all[i], all[i-1]+1);
free(line);
fclose(in);
return 0;
}
int parse(char *line)
{
int res = 0;
int shift = strlen(line) - 1;
for (; *line != '\0'; line++)
res += (*line == 'B' || *line == 'R') << shift--;
return res;
}
void removeTrailingLineBreak(char *line)
{
for (char *pointer = line; *pointer != '\0'; pointer++)
if (*pointer == '\n' || *pointer == '\r')
*pointer = '\0';
}
// Mergesort
void sort(int *in, int n)
{
if (n <= 1)
// Already sorted
return;
int *a = malloc(n/2 * sizeof(*a));
int *b = malloc((n/2 + n%2) * sizeof(*b));
memcpy(a, in, n/2 * sizeof(*a));
memcpy(b, in+n/2, (n/2 + n%2) * sizeof(*b));
sort(a, n/2);
sort(b, n/2 + n%2);
for (int i = 0, j = 0; i < n/2 || j < n/2 + n%2;)
in[i+j] = j == (n/2 + n%2) || i != n/2 && a[i] < b[j] ? a[i++] : b[j++];
free(a);
free(b);
}