-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiching.R
268 lines (202 loc) · 6.99 KB
/
iching.R
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# Introduction....
# print("Hello! Press anything to throw... ")
# readline(prompt = "")
print("Throwing. This will take a minute ")
setwd("/home/bill/Projects/iching")
# Creates a directory to hold all the files in
if (dir.exists("temp")) {
unlink("temp", recursive=TRUE)
}
dir.create("temp")
# A homemade function for creating csvs (so that it saves lines)
create_csv <- function(csv_current) {
# File name that can be reused
file_name <- paste("./temp/","csv_", toString(csv_current), ".csv", sep="")
file.create(file_name)
# Generates 10000 (A myriad) of numbers that can be written to the text
numbers <- floor(runif(10000, min=0, max=2))
write.csv(numbers, file = file_name)
}
# A homemade function for creating txts (so that it saves lines)
create_txt <- function(txt_current) {
# File name that can be reused
file_name <- paste("./temp/","txt_", toString(txt_current), ".txt", sep="")
file.create(file_name)
# Generates A myriad (10000) of numbers that can be written to the text
numbers <- floor(runif(10000, min=0, max=2))
write(numbers, file = file_name)
}
# A homemade function for deleting a random csv (To save lines)
# While it is possible to do this in 2 - 3 lines with a list.files(pattern...)
# I kept it this way to preserve a bit more of randomness
delete_csv <- function() {
all_files <- list.files("temp")
# Repeats cycling through until a csv is found
repeat {
doomed_file <- all_files[runif(1, min= 1, max = length(all_files) + 1)]
if (grepl(".csv", doomed_file, fixed=TRUE)) {
unlink(paste("./temp/", doomed_file, sep = ""))
break
}
}
}
# A homemade function for deleting a random txt (To save lines)
# While it is possible to do this in 2 - 3 lines with a list.files(pattern...)
# I kept it this way to preserve a bit more of randomness
delete_txt <- function() {
all_files <- list.files("temp")
# Repeats cycling through until a csv is found
repeat {
doomed_file <- all_files[runif(1, min= 1, max = length(all_files) + 1)]
if (grepl(".txt", doomed_file, fixed=TRUE)) {
unlink(paste("./temp/", doomed_file, sep = ""))
break
}
}
}
reading <- rep("x", 6) # This will be your reading once done
for (slot in 1:length(reading)) {
# Counts of how many we he have. Also nice for numbering purposes
csv_count <- 0
txt_count <- 0
# Placeholder for stroke. Broken is csv, Solid is txt
stroke <- ""
# For sake of complete randomness and not anarchy, making 150 files
for (x in 1:150) {
# Generating a random number
random_number <- runif(1, min=0, max=10)
# TXT case (greater than 5. Generates a text file with 10000 ones and zeros)
# (Function defined above)
if (random_number > 5) {
txt_count = txt_count + 1
create_txt(txt_count)
}
# The CSV case (less than 5). Generates a csv with 10000 1 & zeroes in a
# Double column (Function defined above)
else {
csv_count = csv_count + 1
create_csv(csv_count)
}
}
# The balancing act
# Runs until one of two conditions are met
# 1) One is double the other
# 2) The totals are equal
repeat {
# Next chunk is resetting counters of whats in the folder
all_files <- list.files("./temp")
# Completion conditions check.... If not... Time for the next round!
if (csv_count == txt_count
|| abs(csv_count - txt_count) >= min(csv_count, txt_count)) {
# Prints out which one was victorious
print(c("csv_count", "txt_count")[which.max(c(csv_count, txt_count))])
# If they're equal, then pick a random one
if (csv_count == txt_count) {
# Mimics case earlier. CSV is broken and txt is solid
if (floor(runif(1, min=0, max=10)) > 5) {
stroke = "solid"
}
else {
stroke = "broken"
}
} else {
# Otherwise pick the larger one
stroke <- ifelse(c("csv_count", "txt_count")[which.max(c(csv_count, txt_count))]
== "txt_count", "solid", "broken")
}
break
}
# Cap this whole thing at 5000
if (length(all_files) >= 500) {
print(c("csv_count", "txt_count")[which.min(c(csv_count, txt_count))])
break
}
# Randomly select one of the files
# Case for if a csv was chosen
if (grepl(".csv",
all_files[floor(runif(1, min= 1, max = length(all_files)) + 1)],
fixed=TRUE)) {
# If csv is in the lead by 50%
if ((csv_count - txt_count) >= (min(csv_count, txt_count) * 0.50)) {
# Make 2 csv, make 1 txt
create_csv(csv_count)
csv_count = csv_count + 1
create_csv(csv_count)
csv_count = csv_count + 1
create_txt(txt_count)
txt_count = txt_count + 1
}
# Not in the lead
else {
# Make 1 csv, make 2 txt
create_csv(csv_count)
csv_count = csv_count + 1
create_txt(txt_count)
txt_count = txt_count + 1
create_txt(txt_count)
txt_count = txt_count + 1
}
}
# Case for if a txt was chosen
else {
# If txt is in the lead by 50%
if ((txt_count - csv_count) >= (min(csv_count, txt_count) * 0.50)) {
# Make 1 txt, make 1 csv
create_csv(csv_count)
csv_count = csv_count + 1
create_txt(txt_count)
txt_count = txt_count + 1
}
# Not in the lead
else {
# Make 1 txt, delete 1 csv
create_txt(txt_count)
txt_count = txt_count + 1
delete_csv()
csv_count = csv_count - 1
}
}
}
# Once to this point. There is a winning file format
# Pick a random of the file type
if (stroke == "broken") {
# Case for csv
winner_all <- list.files("./temp", pattern = ".csv")
winner_file <- paste("./temp/",
winner_all[floor(runif(1, min= 1, max = length(winner_all)) + 1)],
sep = "")
winner_data <- read.csv(winner_file)
# Picking the 81st number, stored in the second column
if (winner_data[81, 2] == 0) {
reading[slot] = "0"
}
else {
reading[slot] = "1"
}
}
else {
# List of all text files
winner_all <- list.files("./temp", pattern = ".txt")
# random txt file
winner_file <- paste("./temp/",
winner_all[floor(runif(1, min= 1, max = length(winner_all)) + 1)],
sep = "")
# Reads the document as a table (Its a column of 5 letters)
winner_data <- read.table(winner_file)
# Picks the 81st number stored in the second column (to match the csv winner)
if (winner_data[81, 2] == 0) {
reading[slot] = "0"
}
else {
reading[slot] = "1"
}
}
print(reading)
# Deletes temp
if (dir.exists("temp")) {
unlink("temp", recursive=TRUE)
}
if (slot != 6) {
dir.create("temp")
}
}