-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1.c
111 lines (107 loc) · 1.86 KB
/
day1.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
long int process_fun(char* map, size_t size){
int i = 0;
long int max = 0;
long int max2 = 0;
long int max3 = 0;
char* ptr = map;
int x = 0;
long int current = 0;
int z = 1;
x = 0;
while (ptr != map+size-1){
int y;
switch(*ptr++){
case '0':
y = 0;
break;
case '1':
y = 1;
break;
case '2':
y = 2;
break;
case '3':
y = 3;
break;
case '4':
y = 4;
break;
case '5':
y = 5;
break;
case '6':
y = 6;
break;
case '7':
y = 7;
break;
case '8':
y = 8;
break;
case '9':
y = 9;
break;
case '\n':
y = -1;
--z;
break;
default:
printf("got unexpected char %x\n", *(ptr-1));
y = -2;
break;
}
if (y == -2){
continue;
}
if (y == -1){
//puts("newline");
if (z == 0){
current += x;
x = 0;
//printf("current = %d\n", current);
}
if (z < 0){
if (current > max){
max3 = max2;
max2 = max;
max = current;
//printf("new max = %ld\n", max);
}
else if (current > max2){
max3 = max2;
max2 = current;
}
else if (current > max3){
max3 = current;
}
current = 0;
x = 0;
z = 1;
}
continue;
}
x *= 10;
x += y;
z = 1;
//printf("x = %d\n", x);
}
printf("max = %ld, sum of 3 highest = %ld\n", max, max+max2+max3);
return max;
}
int main(int argc, char** argv){
struct stat statbuf;
int fd = open("input", O_RDONLY);
fstat(fd, &statbuf);
size_t filesize = statbuf.st_size;
char* input_map = (char*)mmap(NULL, filesize, PROT_READ, MAP_SHARED, fd, 0);
close(fd);
long int ans = process_fun(input_map, filesize);
munmap(input_map, filesize);
return 0;
}