-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_Scratchcards.bf
145 lines (117 loc) · 3.74 KB
/
04_Scratchcards.bf
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
138
139
140
141
142
143
144
145
/* Usual execution time: ~40s */
IntList<10> winning_numbers = [0,0,0,0,0,0,0,0,0,0]
Int winning_num_idx = 0
Int state = 0 /* 0 - card X, 1 - collect winning numbers, 2 - collect our numbers */
Int char = 1
Int inp_num = 0
Int has_num = 0
Int is_space_or_lf = 0
IntList<3> current_matches = [0,0,0]
Int is_current_matches_not_zero = 0
Int current_matches_tmp = 0
IntList<6> p1 = [0,0,0,0,0,0]
Int p1_tmp = 0
while(char) {
char = 0
input(char)
if((char == 58)) { /* : */
state = 1
inp_num = 0
has_num = 0
winning_num_idx = 0
while((winning_num_idx < 10)) {
winning_numbers[winning_num_idx] = 0
winning_num_idx = (winning_num_idx + 1)
}
winning_num_idx = 0
}
if((char == 124)) { /* | */
state = 2
inp_num = 0
has_num = 0
}
if((char >= 48)) { /* check if char is number */
if((char <= 57)) {
inp_num = ((inp_num * 10) + (char - 48))
has_num = 1
}
}
is_space_or_lf = 0
if((char == 10)) { /* new line */
is_space_or_lf = 1
}
if((char == 32)) { /* space */
if(has_num) {
if((state == 1)) {
winning_numbers[winning_num_idx] = inp_num
winning_num_idx = (winning_num_idx + 1)
inp_num = 0
has_num = 0
}
is_space_or_lf = 1
}
}
if(is_space_or_lf) {
if((state == 2)) {
winning_num_idx = 0
while((winning_num_idx < 10)) {
if((inp_num == winning_numbers[winning_num_idx])) { /* our current number was a winning number -> count it */
is_current_matches_not_zero = 1
if((current_matches[0] == 0)) {
if((current_matches[1] == 0)) {
if((current_matches[2] == 0)) {
current_matches[0] = 0
current_matches[1] = 0
current_matches[2] = 1
is_current_matches_not_zero = 0
}
}
}
if(is_current_matches_not_zero) {
current_matches_tmp = (current_matches[2] * 2)
if((current_matches_tmp > 9)) { current_matches[2] = (current_matches_tmp - 10) current_matches_tmp = 1 } else { current_matches[2] = current_matches_tmp current_matches_tmp = 0 }
current_matches_tmp = (current_matches_tmp + (current_matches[1] * 2))
if((current_matches_tmp > 9)) { current_matches[1] = (current_matches_tmp - 10) current_matches_tmp = 1 } else { current_matches[1] = current_matches_tmp current_matches_tmp = 0 }
current_matches[0] = (current_matches_tmp + (current_matches[0] * 2))
}
}
winning_num_idx = (winning_num_idx + 1)
}
inp_num = 0
has_num = 0
}
}
if((char == 10)) { /* new line - reset state for next card */
/* add current_matches to p1 result */
p1_tmp = (p1[5] + current_matches[2])
if((p1_tmp>9)) { p1[5] = (p1_tmp - 10) p1_tmp = 1 } else { p1[5] = p1_tmp p1_tmp = 0 }
p1_tmp = (p1_tmp + (p1[4] + current_matches[1]))
if((p1_tmp>9)) { p1[4] = (p1_tmp - 10) p1_tmp = 1 } else { p1[4] = p1_tmp p1_tmp = 0 }
p1_tmp = (p1_tmp + (p1[3] + current_matches[0]))
if((p1_tmp>9)) { p1[3] = (p1_tmp - 10) p1_tmp = 1 } else { p1[3] = p1_tmp p1_tmp = 0 }
p1_tmp = (p1_tmp + p1[2])
if((p1_tmp>9)) { p1[2] = (p1_tmp - 10) p1_tmp = 1 } else { p1[2] = p1_tmp p1_tmp = 0 }
p1_tmp = (p1_tmp + p1[1])
if((p1_tmp>9)) { p1[1] = (p1_tmp - 10) p1_tmp = 1 } else { p1[1] = p1_tmp p1_tmp = 0 }
p1[0] = (p1_tmp + p1[0])
/* reset current_matches */
current_matches[0] = 0
current_matches[1] = 0
current_matches[2] = 0
inp_num = 0
has_num = 0
state = 0
}
}
Int has_started = 0
Int num = 0
for (num, p1) {
if(num) {
has_started = 1
}
if(has_started) {
printN(num)
}
}
print(10)
print(45)