-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuckIt.js
98 lines (86 loc) · 2.54 KB
/
fuckIt.js
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
function fuckit(input) {
const data = [];
for (let i in input) {
data.push({ "char": input[i], "ascii": input.charCodeAt(i) });
}
const groups = [];
const skipIndex = [];
let maxCells = 0; // Track the maximum cell index used
// Check each letter for letters that are less than 10 bits away and separate them into groups
for (let i in data) {
if (skipIndex.indexOf(i) === -1) {
for (let j in data) {
if (Math.abs(data[i]["ascii"] - data[j]["ascii"]) < 10) {
if (!groups[i]) groups[i] = [];
groups[i].push(data[j]["ascii"]);
skipIndex.push(j);
}
}
}
}
// Make a copy of groups without any empty indexes
const condensedGroups = groups.filter(el => el);
for (let i in data) {
for (let j in condensedGroups) {
if (condensedGroups[j].indexOf(data[i]['ascii']) !== -1) {
data[i]['group'] = Number(j);
}
}
}
let output = '++++++++++';
const initLoop = [];
// Create an initial loop
for (let i in condensedGroups) {
let char;
for (let v of data) {
if (!char) {
if (v['group'] == i) char = v['ascii'];
}
}
let currentLine = ">";
for (let j = 0; j < Math.floor(char / 10); j++) {
currentLine += "+";
}
initLoop.push(currentLine);
}
output += "[" + initLoop.join('');
for (let i in groups) {
output += "<";
}
output += "-]";
// This loop writes the actual characters
let currentCell = -1;
let currentValue;
const savedCells = [];
for (let i of data) {
if (currentCell !== i['group']) {
// Update maxCells based on the new group index
maxCells = Math.max(maxCells, i['group'] + 1); // +1 because we are indexing from 0
// Move to the correct cell
while (currentCell !== i['group']) {
if (currentCell < i['group']) {
currentCell++;
output += '>';
} else {
currentCell--;
output += '<';
}
}
currentValue = (savedCells[currentCell]) ? savedCells[currentCell] : Math.trunc(i['ascii'] / 10) * 10;
}
while (currentValue !== i['ascii']) {
if (currentValue < i['ascii']) {
output += '+';
currentValue++;
} else {
output += '-';
currentValue--;
}
}
savedCells[currentCell] = currentValue;
output += '.';
}
// Prepare the tape length definition based on maxCells
const tapeLength = '%'.padEnd(maxCells + 2, '+') + '%'; // Length is maxCells
return tapeLength + output; // Return the length and the formed string
}