-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9b.cpp
137 lines (134 loc) · 2.82 KB
/
day9b.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include "split.h"
}
#include <unordered_set>
#include <cstdint>
int process_fun(char* map, int size){
size_t* offset_arr = find_splits_str(map, size, "\n");
char* charptr;
if (!offset_arr){puts("something wrong"); exit(1);}
int t = 0;
while(charptr = load_next_split(map, offset_arr, size)){//use set_pos(1) to ignore first segment
++t;
// printf("%ld\n", atol(charptr));
//printf("line number %d; offset = %lu\n",t, offset_arr[t]);
//printf("%s", charptr);
}
free_ebuf();
free(offset_arr);
return 0;
}
void calculate_new_pos(int16_t x, int16_t y, int16_t* x0, int16_t* y0){
if (x-*x0 > 1 && y-*y0 > 1){
*x0 = x-1;
*y0 = y-1;
return;
}
if (x-*x0 > 1 && y-*y0 < -1){
*x0 = x-1;
*y0 = y+1;
return;
}
if (x-*x0 < -1 && y-*y0 > 1){
*x0 = x+1;
*y0 = y-1;
return;
}
if (x-*x0 < -1 && y-*y0 < -1){
*x0 = x+1;
*y0 = y+1;
return;
}
if (x-*x0 > 1){
*y0 = y;
*x0 = x-1;
}
else if (x-*x0 < -1){
*y0 = y;
*x0 = x+1;
}
else if (y-*y0 < -1){
*x0 = x;
*y0 = y+1;
}
else if (y-*y0 > 1){
*x0 = x;
*y0 = y-1;
}
}
void process_fgets(){
FILE* fp = fopen("input", "rw");
char buf[32768];
char dir;
int dist;
int16_t x = 0;
int16_t y = 0;
int16_t x0 = 0;
int16_t y0 = 0;
int16_t arrx[9];
int16_t arry[9];
std::unordered_set<uint32_t> set;
// set.insert(0);
uint32_t temp;
while(fgets(buf, 32768, fp)){
*(buf + strlen(buf) -1) = '\0';//remove '\n' character
if (strlen(buf) < 3){
printf("ignoring line\n");
continue;
}
dir = *buf;
dist = atoi(buf+2);
while(dist){
switch(dir){
case 'U':
y++;
break;
case 'D':
y--;
break;
case 'L':
x--;
break;
case 'R':
x++;
break;
}
// int16_t arry[9];
calculate_new_pos(x, y, arrx, arry);
for(int i = 1; i < 9; ++i){
calculate_new_pos(arrx[i-1], arry[i-1], arrx+i, arry+i);
}
// printf(" head (%d, %d) tail (%d,%d)\n", x, y, x0, y0);
temp = (uint16_t)arrx[8];
temp |= (uint16_t)arry[8] << 16ul;
set.insert(temp);
// printf("inserting %X\n", temp);
//printf("num = %lu\n", set.size());
dist--;
}
for(int i = 0; i < 9; ++i){
printf("%d %d %d\n", i, arrx[i], arry[i]);
}
}
printf("num = %lu\n", set.size());
fclose(fp);
}
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);
// process_fun(input_map, filesize);
process_fgets();
munmap(input_map, filesize);
return 0;
}