-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18b.c
65 lines (49 loc) · 1.17 KB
/
day18b.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
// Licensed under the MIT License.
// Lavaduct Lagoon Part 2
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUFFER_SIZE 16
long long math_pick_theorem_a(long long b, long long i)
{
return i + (b / 2) - 1;
}
int main(void)
{
long long b = 0;
long long i = 0;
long long x = 0;
char buffer[BUFFER_SIZE];
clock_t start = clock();
while (fgets(buffer, sizeof buffer, stdin))
{
char* token = strchr(buffer, '#');
if (!token)
{
fprintf(stderr, "Error: Format.\n");
return 1;
}
long n = strtol(token + 1, NULL, 16);
int length = n >> 4;
switch (n & 0xf)
{
case 0:
x -= length;
break;
case 1:
i -= x * length;
break;
case 2:
x += length;
break;
case 3:
i += x * length;
break;
}
b += length;
}
long long area = math_pick_theorem_a(b, i) + 2;
printf("18b %lld %lf\n", area, (double)(clock() - start) / CLOCKS_PER_SEC);
return 0;
}