finish 14, part2 result=6752
This commit is contained in:
parent
d83cf8f50e
commit
49ad6d9af3
41
14/Makefile
Executable file
41
14/Makefile
Executable file
|
@ -0,0 +1,41 @@
|
|||
CC := gcc
|
||||
LD := $(CC)
|
||||
LIBS := -lm
|
||||
LDFLAGS :=
|
||||
CFLAGS := -Wall -Wextra -Wno-override-init -std=c2x -include_.h
|
||||
SRC := part1.c mylib.c
|
||||
ifeq ($(PART),2)
|
||||
SRC := part2.c mylib.c
|
||||
endif
|
||||
OBJ_DIR := build
|
||||
OBJ := $(addprefix $(OBJ_DIR)/, $(patsubst %.c,%.o,$(SRC)))
|
||||
DIR := .
|
||||
RAW_NAME:= a.out
|
||||
NAME := $(RAW_NAME)
|
||||
|
||||
ifeq ($(RELEASE),1)
|
||||
CFLAGS += -O3
|
||||
LDFLAGS += -O3 -s
|
||||
else
|
||||
CFLAGS += -O0 -g3
|
||||
LDFLAGS += -O0 -g
|
||||
endif
|
||||
|
||||
all: $(NAME)
|
||||
|
||||
$(NAME): $(OBJ)
|
||||
$(LD) $(LDFLAGS) -o $(DIR)/$(NAME) $(OBJ) $(LIBS)
|
||||
|
||||
$(OBJ_DIR)/%.o: %.c
|
||||
@mkdir -p $(@D)
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
@rm -rf $(OBJ_DIR)
|
||||
@rm -rf $(RAW_NAME)
|
||||
|
||||
re:
|
||||
@make --no-print clean
|
||||
@make --no-print all
|
||||
|
||||
.PHONY: all clean re
|
27
14/_.h
Normal file
27
14/_.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include <strings.h>
|
||||
#include <wctype.h>
|
||||
#include <math.h>
|
||||
|
||||
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
|
||||
#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
|
||||
#define ABS(X) ((X) >= 0 ? (X) : -(X))
|
||||
|
||||
char** str_split(char* a_str, const char a_delim);
|
||||
|
||||
int str_index(char *str, char c);
|
||||
|
||||
int number_of_element(char *str, char c);
|
||||
|
||||
long gmc(long a, long b);
|
||||
long lmc(long a, long b);
|
||||
long lmcs(int array[], int n);
|
||||
|
||||
long strslen(char **strs);
|
500
14/main.txt
Normal file
500
14/main.txt
Normal file
|
@ -0,0 +1,500 @@
|
|||
10 22 50 -21
|
||||
35 60 -8 52
|
||||
79 50 82 9
|
||||
52 1 -28 -55
|
||||
85 67 -51 -18
|
||||
72 34 82 13
|
||||
57 0 67 75
|
||||
7 52 85 67
|
||||
81 19 -73 -21
|
||||
46 32 -82 29
|
||||
68 101 -73 30
|
||||
23 42 -36 -63
|
||||
25 93 40 -16
|
||||
30 93 -76 -55
|
||||
22 58 72 -9
|
||||
25 84 18 -87
|
||||
38 80 39 -42
|
||||
35 71 -55 -53
|
||||
88 60 -32 -29
|
||||
86 86 -16 98
|
||||
30 102 66 35
|
||||
64 88 54 75
|
||||
24 4 18 93
|
||||
4 6 4 -93
|
||||
57 70 -7 -42
|
||||
23 38 -37 56
|
||||
75 84 -89 -21
|
||||
10 83 30 -33
|
||||
77 66 -73 -2
|
||||
4 15 84 2
|
||||
44 34 -48 31
|
||||
15 78 -4 50
|
||||
31 60 -26 -66
|
||||
62 50 34 -94
|
||||
98 25 37 49
|
||||
74 81 1 59
|
||||
94 81 -38 -60
|
||||
70 20 -54 -60
|
||||
89 51 -85 -32
|
||||
49 51 -48 76
|
||||
53 66 33 83
|
||||
61 29 -47 -63
|
||||
49 43 94 -7
|
||||
17 23 -49 -30
|
||||
15 55 24 -63
|
||||
82 3 -13 77
|
||||
77 44 54 20
|
||||
8 43 30 -76
|
||||
70 72 -34 97
|
||||
58 10 96 45
|
||||
67 95 -20 39
|
||||
92 81 -83 -26
|
||||
44 98 64 -41
|
||||
1 99 71 73
|
||||
81 25 -59 44
|
||||
16 31 91 -32
|
||||
48 44 -41 -63
|
||||
88 11 -72 37
|
||||
31 89 -77 -75
|
||||
42 41 -40 33
|
||||
61 41 -73 -16
|
||||
96 7 -71 -37
|
||||
16 35 -43 -43
|
||||
80 79 -59 70
|
||||
20 35 -70 -5
|
||||
41 47 -50 -70
|
||||
86 37 11 -72
|
||||
61 97 54 -73
|
||||
82 4 90 -3
|
||||
24 35 38 80
|
||||
52 93 -81 2
|
||||
90 37 90 -43
|
||||
18 28 31 11
|
||||
33 58 -2 56
|
||||
100 102 -32 19
|
||||
97 51 -45 -15
|
||||
68 17 76 -93
|
||||
100 13 83 -46
|
||||
88 8 -12 75
|
||||
71 14 -6 -30
|
||||
83 10 15 -37
|
||||
81 84 35 12
|
||||
98 90 -52 43
|
||||
91 60 65 82
|
||||
3 72 -24 -40
|
||||
84 49 -58 22
|
||||
82 75 -99 27
|
||||
4 73 46 79
|
||||
15 32 51 24
|
||||
6 32 -44 -81
|
||||
21 101 -36 28
|
||||
21 100 51 -73
|
||||
50 32 -92 -98
|
||||
1 58 -24 -2
|
||||
52 96 -95 -64
|
||||
83 28 -60 11
|
||||
85 101 -59 -26
|
||||
19 2 -31 -97
|
||||
50 32 -68 31
|
||||
13 66 43 36
|
||||
100 15 -30 -14
|
||||
84 78 -58 88
|
||||
65 50 -53 -21
|
||||
50 75 26 61
|
||||
89 60 -93 47
|
||||
15 39 38 4
|
||||
15 50 -90 -27
|
||||
63 66 29 60
|
||||
17 79 -50 -89
|
||||
21 39 -83 60
|
||||
62 35 6 -51
|
||||
99 44 9 8
|
||||
32 4 -43 -35
|
||||
57 2 -81 39
|
||||
4 7 -44 -10
|
||||
93 12 56 -19
|
||||
33 79 45 -24
|
||||
66 72 86 -41
|
||||
84 83 -28 39
|
||||
11 30 56 -88
|
||||
51 19 -54 -89
|
||||
0 36 -32 -28
|
||||
69 39 41 20
|
||||
75 88 8 79
|
||||
5 12 -64 93
|
||||
38 9 -62 91
|
||||
25 100 64 -15
|
||||
97 60 -12 -69
|
||||
57 76 -7 -13
|
||||
42 48 -69 32
|
||||
31 35 -81 65
|
||||
28 28 46 85
|
||||
77 84 -73 68
|
||||
57 18 -54 44
|
||||
48 98 59 17
|
||||
28 86 92 -17
|
||||
1 93 90 -53
|
||||
0 31 84 -32
|
||||
87 34 9 -65
|
||||
20 20 44 -25
|
||||
65 93 -96 -54
|
||||
45 75 -82 -87
|
||||
48 58 26 65
|
||||
87 76 -86 16
|
||||
31 79 52 34
|
||||
100 101 58 -36
|
||||
76 20 59 -76
|
||||
100 91 -85 -4
|
||||
14 13 92 -46
|
||||
72 66 -60 -65
|
||||
74 41 -60 96
|
||||
73 17 48 80
|
||||
48 50 -48 94
|
||||
76 50 -81 12
|
||||
51 102 80 1
|
||||
44 12 93 -86
|
||||
49 16 40 -43
|
||||
75 97 2 -24
|
||||
54 35 13 -34
|
||||
50 4 -75 75
|
||||
28 87 77 81
|
||||
50 38 13 60
|
||||
51 43 -27 22
|
||||
71 52 -13 29
|
||||
37 74 -29 -40
|
||||
67 82 -13 61
|
||||
4 21 -97 -50
|
||||
36 2 25 -66
|
||||
17 67 -30 -87
|
||||
90 29 96 15
|
||||
90 96 -52 39
|
||||
20 9 78 17
|
||||
70 74 -32 -93
|
||||
77 60 -83 51
|
||||
74 97 92 1
|
||||
75 82 28 61
|
||||
40 4 -58 -94
|
||||
64 26 6 -86
|
||||
71 14 -81 -93
|
||||
11 96 24 70
|
||||
97 26 97 -61
|
||||
77 53 -44 55
|
||||
1 93 83 46
|
||||
11 59 90 54
|
||||
3 2 -85 -8
|
||||
18 90 79 -60
|
||||
82 31 96 -34
|
||||
41 22 -9 -72
|
||||
52 17 86 -95
|
||||
48 39 -41 -63
|
||||
3 61 -16 -35
|
||||
6 26 50 42
|
||||
73 91 -53 99
|
||||
55 89 -54 -35
|
||||
24 22 17 -66
|
||||
80 84 -97 53
|
||||
20 73 69 -40
|
||||
26 100 -83 19
|
||||
68 87 -40 88
|
||||
80 45 -15 -4
|
||||
2 80 -98 23
|
||||
30 71 -62 9
|
||||
2 89 -50 88
|
||||
19 99 21 -1
|
||||
11 81 -3 -13
|
||||
70 59 -32 34
|
||||
84 68 89 -22
|
||||
48 35 -48 -79
|
||||
78 80 -60 34
|
||||
10 50 77 -31
|
||||
94 79 23 90
|
||||
46 47 -68 2
|
||||
20 52 30 -56
|
||||
79 36 2 -97
|
||||
75 21 -74 -22
|
||||
54 53 4 -75
|
||||
100 76 69 43
|
||||
95 33 -64 22
|
||||
26 61 38 63
|
||||
13 20 43 -59
|
||||
20 36 72 47
|
||||
78 98 49 -24
|
||||
83 76 -12 -22
|
||||
79 98 -73 77
|
||||
31 22 -96 33
|
||||
61 22 27 -90
|
||||
83 41 22 40
|
||||
98 15 76 -37
|
||||
23 46 -49 43
|
||||
2 87 -52 -8
|
||||
74 2 74 -57
|
||||
19 36 51 36
|
||||
30 93 -3 -93
|
||||
75 21 90 -28
|
||||
2 32 30 -12
|
||||
69 88 -40 -90
|
||||
1 77 -81 27
|
||||
1 70 -3 56
|
||||
60 70 3 -13
|
||||
54 14 67 8
|
||||
1 61 64 -47
|
||||
50 99 -40 72
|
||||
62 43 74 76
|
||||
63 30 14 98
|
||||
10 3 24 -46
|
||||
25 64 -83 -87
|
||||
8 31 -70 54
|
||||
17 18 -80 -95
|
||||
100 53 42 -6
|
||||
61 101 28 -44
|
||||
91 34 -6 23
|
||||
43 28 -31 62
|
||||
52 88 75 6
|
||||
45 66 -28 -96
|
||||
29 40 5 78
|
||||
21 91 -50 97
|
||||
72 6 48 35
|
||||
4 11 97 48
|
||||
66 89 -72 4
|
||||
63 70 -47 56
|
||||
28 75 10 -78
|
||||
94 47 -65 -27
|
||||
14 74 51 25
|
||||
34 65 5 -29
|
||||
61 65 -73 47
|
||||
88 17 -12 -28
|
||||
46 43 -76 40
|
||||
78 76 21 34
|
||||
54 32 -41 -7
|
||||
48 92 -41 -35
|
||||
89 59 29 9
|
||||
89 66 36 -58
|
||||
38 83 -19 16
|
||||
84 4 38 66
|
||||
39 101 88 54
|
||||
6 32 82 50
|
||||
30 79 -63 -80
|
||||
54 35 41 -16
|
||||
33 102 -49 35
|
||||
23 37 -91 -61
|
||||
77 30 -35 -69
|
||||
79 4 35 -57
|
||||
45 29 -95 -23
|
||||
49 57 87 71
|
||||
53 31 87 -7
|
||||
10 78 -34 35
|
||||
10 16 -37 26
|
||||
46 15 -41 -95
|
||||
2 26 49 -30
|
||||
15 68 -50 52
|
||||
69 40 -47 -90
|
||||
53 39 14 9
|
||||
23 61 11 83
|
||||
44 33 -27 -79
|
||||
81 2 -40 -55
|
||||
89 42 -72 31
|
||||
18 32 -39 19
|
||||
97 91 -76 52
|
||||
51 80 60 -64
|
||||
48 36 53 98
|
||||
86 91 97 1
|
||||
48 88 -22 -42
|
||||
100 17 -24 -90
|
||||
40 52 -36 -85
|
||||
65 52 -19 -25
|
||||
0 45 82 -84
|
||||
12 102 78 -73
|
||||
67 19 87 -43
|
||||
24 78 84 93
|
||||
68 52 -14 71
|
||||
57 10 -7 6
|
||||
4 10 87 62
|
||||
64 38 41 51
|
||||
23 56 12 -38
|
||||
53 25 46 -54
|
||||
16 90 -84 59
|
||||
23 97 -36 -35
|
||||
35 78 85 92
|
||||
53 31 -53 19
|
||||
61 84 54 41
|
||||
86 93 76 10
|
||||
78 80 -46 -24
|
||||
2 6 43 46
|
||||
58 82 20 97
|
||||
46 20 -75 15
|
||||
60 85 30 -54
|
||||
57 29 -7 -96
|
||||
75 98 62 79
|
||||
64 44 54 24
|
||||
42 51 -76 19
|
||||
37 66 -70 -9
|
||||
85 16 -58 60
|
||||
8 70 -57 -69
|
||||
36 55 -73 -96
|
||||
67 93 88 30
|
||||
18 48 54 -94
|
||||
53 14 20 -93
|
||||
28 37 24 -10
|
||||
64 10 -94 -94
|
||||
10 19 -41 -19
|
||||
6 80 -64 -98
|
||||
30 66 52 54
|
||||
35 24 5 -3
|
||||
14 1 18 -60
|
||||
54 0 -61 -17
|
||||
27 2 38 60
|
||||
49 88 48 -17
|
||||
63 1 -7 -94
|
||||
98 75 -78 43
|
||||
88 97 -25 10
|
||||
30 75 -68 26
|
||||
65 57 68 18
|
||||
19 30 3 49
|
||||
52 47 -50 94
|
||||
16 8 27 -52
|
||||
27 64 -84 -29
|
||||
85 30 49 31
|
||||
94 20 10 80
|
||||
57 84 -84 32
|
||||
45 93 -2 -46
|
||||
94 68 -79 83
|
||||
61 27 -73 2
|
||||
15 64 99 -4
|
||||
28 82 46 -51
|
||||
88 14 81 74
|
||||
2 90 67 48
|
||||
8 66 64 -31
|
||||
4 5 -97 48
|
||||
39 60 5 74
|
||||
62 11 81 -75
|
||||
82 54 35 -84
|
||||
89 72 -88 -24
|
||||
42 45 73 67
|
||||
77 78 89 90
|
||||
44 5 80 28
|
||||
58 28 -31 -31
|
||||
91 68 -73 -22
|
||||
68 48 -76 80
|
||||
22 26 24 6
|
||||
34 6 -69 -83
|
||||
64 81 -13 -8
|
||||
15 16 -16 64
|
||||
42 49 93 -56
|
||||
87 38 -59 58
|
||||
95 34 -92 -79
|
||||
66 101 7 -19
|
||||
63 38 82 1
|
||||
95 53 63 -27
|
||||
95 2 -65 -48
|
||||
16 73 -64 -78
|
||||
13 60 -58 27
|
||||
69 55 34 -11
|
||||
32 20 10 -71
|
||||
52 100 -95 -84
|
||||
50 16 -21 35
|
||||
42 44 73 85
|
||||
87 33 -93 74
|
||||
60 75 40 34
|
||||
7 1 27 -13
|
||||
12 63 29 -63
|
||||
18 56 -90 -18
|
||||
30 49 -56 -92
|
||||
49 8 -63 49
|
||||
80 15 64 -79
|
||||
36 66 25 -24
|
||||
76 37 -80 22
|
||||
76 68 29 -31
|
||||
90 2 -92 51
|
||||
14 42 72 49
|
||||
57 89 -94 -53
|
||||
48 63 29 -14
|
||||
80 2 -86 19
|
||||
62 45 -53 36
|
||||
40 36 67 67
|
||||
45 96 62 -66
|
||||
42 61 -81 18
|
||||
54 69 -27 -79
|
||||
72 17 55 -97
|
||||
92 93 -3 3
|
||||
11 97 10 59
|
||||
33 61 5 -65
|
||||
26 31 -48 34
|
||||
1 32 81 -18
|
||||
68 70 27 -54
|
||||
89 10 -99 -12
|
||||
32 102 65 -64
|
||||
52 89 -41 -62
|
||||
100 10 -11 -59
|
||||
93 63 83 34
|
||||
16 67 -23 43
|
||||
58 35 13 -10
|
||||
24 31 85 -81
|
||||
17 94 58 10
|
||||
40 55 -22 -65
|
||||
47 12 -10 54
|
||||
52 29 -68 -25
|
||||
11 83 -71 -80
|
||||
62 29 -46 -25
|
||||
73 95 67 47
|
||||
74 25 81 14
|
||||
99 45 11 -13
|
||||
31 13 76 26
|
||||
27 90 17 -42
|
||||
68 80 89 -95
|
||||
14 54 57 94
|
||||
97 56 29 -9
|
||||
65 8 -68 57
|
||||
86 76 82 83
|
||||
81 43 96 -54
|
||||
86 17 56 26
|
||||
41 92 -82 -10
|
||||
49 41 60 58
|
||||
49 72 93 3
|
||||
50 13 -7 17
|
||||
46 85 -5 81
|
||||
7 40 -51 78
|
||||
88 91 62 59
|
||||
97 14 93 86
|
||||
62 80 98 62
|
||||
22 50 51 -65
|
||||
14 34 77 38
|
||||
34 86 -54 -6
|
||||
2 86 -85 25
|
||||
67 28 -15 51
|
||||
90 87 19 -48
|
||||
13 25 45 -5
|
||||
59 62 13 -67
|
||||
53 40 13 11
|
||||
48 52 37 74
|
||||
82 62 -66 -47
|
||||
74 64 -25 -25
|
||||
72 28 -68 -37
|
||||
51 8 -48 73
|
||||
46 32 -89 75
|
||||
100 15 -34 58
|
||||
34 39 -8 71
|
||||
78 70 47 -22
|
||||
31 50 -49 2
|
||||
15 84 64 -98
|
||||
76 92 75 -6
|
||||
40 18 54 -96
|
||||
81 3 1 -96
|
||||
9 95 -85 -89
|
||||
64 18 34 87
|
||||
72 100 48 12
|
||||
39 77 40 -53
|
||||
74 11 95 -77
|
||||
29 20 72 51
|
||||
96 91 -19 -75
|
||||
11 68 51 -58
|
||||
64 9 67 28
|
||||
73 94 -46 30
|
||||
77 39 15 49
|
||||
100 20 -85 -77
|
||||
92 80 -5 -64
|
||||
80 11 -62 -41
|
||||
78 47 -54 74
|
||||
32 76 24 -12
|
||||
13 22 -10 -52
|
||||
30 78 -49 -53
|
91
14/mylib.c
Normal file
91
14/mylib.c
Normal file
|
@ -0,0 +1,91 @@
|
|||
#include "_.h"
|
||||
|
||||
// NOT MY CODE
|
||||
// https://stackoverflow.com/questions/9210528/split-string-with-delimiters-in-c
|
||||
char** str_split(char* str, const char a_delim)
|
||||
{
|
||||
char *a_str = strdup(str);
|
||||
char** result = 0;
|
||||
size_t count = 0;
|
||||
char* tmp = a_str;
|
||||
char* last_comma = 0;
|
||||
char delim[2];
|
||||
delim[0] = a_delim;
|
||||
delim[1] = 0;
|
||||
|
||||
while (*tmp)
|
||||
{
|
||||
if (a_delim == *tmp)
|
||||
{
|
||||
count++;
|
||||
last_comma = tmp;
|
||||
}
|
||||
tmp++;
|
||||
}
|
||||
|
||||
count += last_comma < (a_str + strlen(a_str) - 1);
|
||||
|
||||
count++;
|
||||
|
||||
result = malloc(sizeof(char*) * count);
|
||||
|
||||
if (result)
|
||||
{
|
||||
size_t idx = 0;
|
||||
char* token = strtok(a_str, delim);
|
||||
|
||||
while (token)
|
||||
{
|
||||
//assert(idx < count);
|
||||
*(result + idx++) = strdup(token);
|
||||
token = strtok(0, delim);
|
||||
}
|
||||
//assert(idx == count - 1);
|
||||
*(result + idx) = 0;
|
||||
}
|
||||
|
||||
free(a_str);
|
||||
return result;
|
||||
}
|
||||
|
||||
int str_index(char *str, char c) {
|
||||
|
||||
int i = 0;
|
||||
while (str[i])
|
||||
if (str[i++] == c)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int number_of_element(char *str, char c) {
|
||||
int nbr = 0;
|
||||
for (int i = 0; str[i] != '\0'; i++)
|
||||
if (str[i] == c)
|
||||
nbr++;
|
||||
return nbr;
|
||||
}
|
||||
|
||||
long gmc(long a, long b) {
|
||||
if (b == 0)
|
||||
return a;
|
||||
return gmc(b, a % b);
|
||||
}
|
||||
|
||||
long lmc(long a, long b) {
|
||||
return (a / gmc(a, b)) * b;
|
||||
}
|
||||
|
||||
long lmcs(int array[], int n) {
|
||||
long result = array[0];
|
||||
for (int i = 1; i < n; i++)
|
||||
result = lmc(result, array[i]);
|
||||
return result;
|
||||
}
|
||||
|
||||
long strslen(char **strs) {
|
||||
long count = 0;
|
||||
while (strs[count])
|
||||
count++;
|
||||
return count;
|
||||
}
|
83
14/part1.c
Normal file
83
14/part1.c
Normal file
|
@ -0,0 +1,83 @@
|
|||
#include "_.h"
|
||||
|
||||
int h = 103;
|
||||
int w = 101;
|
||||
|
||||
void move_robot(int **pos) {
|
||||
int *pos_robot = *pos;
|
||||
|
||||
pos_robot[0] += pos_robot[2];
|
||||
if (pos_robot[0] > w - 1)
|
||||
pos_robot[0] -= w;
|
||||
if (pos_robot[0] < 0)
|
||||
pos_robot[0] += w;
|
||||
pos_robot[1] += pos_robot[3];
|
||||
if (pos_robot[1] > h - 1)
|
||||
pos_robot[1] -= h;
|
||||
if (pos_robot[1] < 0)
|
||||
pos_robot[1] += h;
|
||||
}
|
||||
|
||||
void display(int **pos_robot) {
|
||||
for (int i = 0; i < h; i++) {
|
||||
for (int j = 0; j < w; j++) {
|
||||
int nbr = 0;
|
||||
for (int w = 0; pos_robot[w] != NULL; w++)
|
||||
if (pos_robot[w][0] == i && pos_robot[w][1] == j)
|
||||
nbr++;
|
||||
|
||||
if (nbr <= 0)
|
||||
printf(".");
|
||||
else
|
||||
printf("%d", nbr);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
if (argc != 2)
|
||||
return 1;
|
||||
|
||||
char *input = argv[1];
|
||||
char **inputs = str_split(strdup(input), '\n');
|
||||
|
||||
int **pos_robot = calloc(500 + 1, sizeof(int*));
|
||||
|
||||
for (int i = 0; inputs[i] != NULL; i++) {
|
||||
|
||||
char **inputs_space = str_split(strdup(inputs[i]), ' ');
|
||||
|
||||
pos_robot[i] = calloc(4 + 1, sizeof(int));
|
||||
pos_robot[i][0] = atoi(inputs_space[0]);
|
||||
pos_robot[i][1] = atoi(inputs_space[1]);
|
||||
pos_robot[i][2] = atoi(inputs_space[2]);
|
||||
pos_robot[i][3] = atoi(inputs_space[3]);
|
||||
|
||||
for (int j = 0; j < 100; j++)
|
||||
move_robot(&(pos_robot[i]));
|
||||
}
|
||||
|
||||
int count[4] = { [0 ... 3] = 0};
|
||||
for (int i = 0; pos_robot[i] != NULL; i++) {
|
||||
if (pos_robot[i][0] < w / 2 && pos_robot[i][1] < h / 2)
|
||||
count[0]++;
|
||||
if (pos_robot[i][0] > w / 2 && pos_robot[i][1] < h / 2)
|
||||
count[1]++;
|
||||
if (pos_robot[i][0] < w / 2 && pos_robot[i][1] > h / 2)
|
||||
count[2]++;
|
||||
if (pos_robot[i][0] > w / 2 && pos_robot[i][1] > h / 2)
|
||||
count[3]++;
|
||||
}
|
||||
|
||||
int result = 1;
|
||||
for (int i = 0; i < 4; i++)
|
||||
result *= count[i];
|
||||
|
||||
//display(pos_robot);
|
||||
|
||||
printf("result: %d\n", result);
|
||||
|
||||
return 0;
|
||||
}
|
87
14/part2.c
Normal file
87
14/part2.c
Normal file
|
@ -0,0 +1,87 @@
|
|||
#include "_.h"
|
||||
|
||||
int h = 103;
|
||||
int w = 101;
|
||||
|
||||
void move_robot(int **pos) {
|
||||
int *pos_robot = *pos;
|
||||
|
||||
pos_robot[0] += pos_robot[2];
|
||||
if (pos_robot[0] > w - 1)
|
||||
pos_robot[0] -= w;
|
||||
if (pos_robot[0] < 0)
|
||||
pos_robot[0] += w;
|
||||
pos_robot[1] += pos_robot[3];
|
||||
if (pos_robot[1] > h - 1)
|
||||
pos_robot[1] -= h;
|
||||
if (pos_robot[1] < 0)
|
||||
pos_robot[1] += h;
|
||||
}
|
||||
|
||||
void display(int **pos_robot) {
|
||||
for (int i = 0; i < h; i++) {
|
||||
for (int j = 0; j < w; j++) {
|
||||
int nbr = 0;
|
||||
for (int w = 0; pos_robot[w] != NULL; w++)
|
||||
if (pos_robot[w][0] == i && pos_robot[w][1] == j)
|
||||
nbr++;
|
||||
|
||||
if (nbr <= 0)
|
||||
printf(".");
|
||||
else
|
||||
printf("%d", nbr);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
if (argc != 2)
|
||||
return 1;
|
||||
|
||||
char *input = argv[1];
|
||||
char **inputs = str_split(strdup(input), '\n');
|
||||
|
||||
int **pos_robot = calloc(500 + 1, sizeof(int*));
|
||||
|
||||
for (int i = 0; inputs[i] != NULL; i++) {
|
||||
char **inputs_space = str_split(strdup(inputs[i]), ' ');
|
||||
|
||||
pos_robot[i] = calloc(4 + 1, sizeof(int));
|
||||
pos_robot[i][0] = atoi(inputs_space[0]);
|
||||
pos_robot[i][1] = atoi(inputs_space[1]);
|
||||
pos_robot[i][2] = atoi(inputs_space[2]);
|
||||
pos_robot[i][3] = atoi(inputs_space[3]);
|
||||
}
|
||||
|
||||
for (int j = 0; j < 10404; j++) {
|
||||
for (int i = 0; pos_robot[i] != NULL; i++) {
|
||||
move_robot(&(pos_robot[i]));
|
||||
}
|
||||
printf("j: %d\n", j);
|
||||
display(pos_robot);
|
||||
}
|
||||
|
||||
int count[4] = { [0 ... 3] = 0};
|
||||
for (int i = 0; pos_robot[i] != NULL; i++) {
|
||||
if (pos_robot[i][0] < w / 2 && pos_robot[i][1] < h / 2)
|
||||
count[0]++;
|
||||
if (pos_robot[i][0] > w / 2 && pos_robot[i][1] < h / 2)
|
||||
count[1]++;
|
||||
if (pos_robot[i][0] < w / 2 && pos_robot[i][1] > h / 2)
|
||||
count[2]++;
|
||||
if (pos_robot[i][0] > w / 2 && pos_robot[i][1] > h / 2)
|
||||
count[3]++;
|
||||
}
|
||||
|
||||
int result = 1;
|
||||
for (int i = 0; i < 4; i++)
|
||||
result *= count[i];
|
||||
|
||||
//display(pos_robot);
|
||||
|
||||
printf("result: %d\n", result);
|
||||
|
||||
return 0;
|
||||
}
|
1082017
14/result.txt
Normal file
1082017
14/result.txt
Normal file
File diff suppressed because it is too large
Load diff
12
14/test.txt
Normal file
12
14/test.txt
Normal file
|
@ -0,0 +1,12 @@
|
|||
0 4 3 -3
|
||||
6 3 -1 -3
|
||||
10 3 -1 2
|
||||
2 0 2 -1
|
||||
0 0 1 3
|
||||
3 0 -2 -2
|
||||
7 6 -1 -3
|
||||
3 0 -1 -2
|
||||
9 3 2 3
|
||||
7 3 -1 2
|
||||
2 4 2 -3
|
||||
9 5 -3 -3
|
Loading…
Reference in a new issue