-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01b.c
117 lines (90 loc) · 1.95 KB
/
day01b.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
112
113
114
115
116
117
// Licensed under the MIT License.
// Trebuchet!? Part 2
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUFFER_SIZE 256
typedef char* String;
static int parse(String value)
{
char first = value[0];
if (isdigit(first))
{
return first - '0';
}
switch (first)
{
case 'f':
if (strstr(value + 1, "our") == value + 1)
{
return 4;
}
if (strstr(value + 1, "ive") == value + 1)
{
return 5;
}
return 0;
case 's':
if (strstr(value + 1, "ix") == value + 1)
{
return 6;
}
if (strstr(value + 1, "even") == value + 1)
{
return 7;
}
return 0;
case 't':
if (strstr(value + 1, "wo") == value + 1)
{
return 2;
}
if (strstr(value + 1, "hree") == value + 1)
{
return 3;
}
return 0;
}
if (strstr(value, "one") == value)
{
return 1;
}
if (strstr(value, "eight") == value)
{
return 8;
}
if (strstr(value, "nine") == value)
{
return 9;
}
return 0;
}
int main(void)
{
char buffer[BUFFER_SIZE];
long sum = 0;
clock_t start = clock();
while (fgets(buffer, sizeof buffer, stdin))
{
int tens = 0;
int ones = 0;
for (char* p = buffer; *p; p++)
{
int current = parse(p);
if (!current)
{
continue;
}
if (!tens)
{
tens = current;
}
ones = current;
}
sum += (tens * 10) + ones;
}
printf("01b %ld %lf\n", sum, (double)(clock() - start) / CLOCKS_PER_SEC);
return 0;
}