Skip to content

Commit 270b8e0

Browse files
Merge pull request #6 from thenightmail/main
I updated various functions and restructured parts of the code.
2 parents 97f8c9d + 1bc919a commit 270b8e0

File tree

3 files changed

+1316
-960
lines changed

3 files changed

+1316
-960
lines changed

pathmode-queues.js

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// putting the environment variables or global variables up top
2+
// not sure what we call these ...
3+
let last_output = [
4+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
5+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
6+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
7+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
8+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
9+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
10+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
11+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
12+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
13+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
14+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
15+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
16+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
17+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
18+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
19+
["N", "N", "N", "N", "N", "N", "N", "N", "N", "N"],
20+
["G", "G", "G", "N", "N", "N", "G", "G", "G", "G"],
21+
["G", "G", "G", "N", "N", "N", "G", "G", "G", "G"],
22+
["G", "G", "G", "N", "N", "N", "G", "G", "G", "G"],
23+
["G", "G", "G", "N", "N", "N", "G", "G", "G", "G"],
24+
].reverse();
25+
26+
var weight_table = [
27+
[1, 0, 0],
28+
[0, 0, 0],
29+
[0, 0, 0],
30+
[0, 0, 0],
31+
[0, 0, 0],
32+
[0, 0, 0],
33+
[0, 0, 0],
34+
[0, 0, 0],
35+
[0, 0, 0],
36+
[0, 0, 0],
37+
[0, 0, 0],
38+
[0, 0, 0],
39+
[0, 0, 0],
40+
[0, 0, 0],
41+
[0, 0, 0],
42+
];
43+
// this is a subset of the queues that solve 3x4 box.
44+
queues = [
45+
["JILZ", "JIZL", "JZIL"], // Array with 48 elements
46+
["TIJO", "TIOJ", "TOIJ"], // Array with 48 elements
47+
["JILZ", "JLIZ", "LJIZ"], // Array with 48 elements
48+
["LITO", "ILTO", "OITL"], // Array with 48 elements
49+
["JLOZ", "JLZO", "LJSO"], // Array with 48 elements
50+
["JOZL", "OJZL", "OJLZ"], // Array with 32 elements
51+
["LSJT", "SLJT", "SLTJ"], // Array with 32 elements
52+
["JZLO", "JZIL", "JZOL"], // Array with 32 elements
53+
["TZLJ", "ZTLJ", "ZTJL"], // Array with 32 elements
54+
["SLTJ", "LSTJ", "TSLJ"], // Array with 32 elements
55+
["OJLZ", "JOLZ", "LOJS"], // Array with 32 elements
56+
["JLZO", "JLZI", "LJZI"], // Array with 32 elements
57+
["LJSO", "SJOL", "SJLO"], // Array with 32 elements
58+
["SLTJ", "LSTJ", "LTSJ"], // Array with 32 elements
59+
["TJZL", "TJLZ", "JTLZ"], // Array with 32 elements
60+
];
61+
62+
// the variable distributedQueues keeps track of how we order the queues.
63+
const distributedQueuesButton = document.getElementById(
64+
"toggleDistributedQueues",
65+
);
66+
67+
let distributedQueues = distributedQueuesButton.checked;
68+
69+
distributedQueuesButton.addEventListener("change", function () {
70+
distributedQueues = distributedQueuesButton.checked;
71+
});
72+
73+
document.addEventListener("DOMContentLoaded", () => {
74+
const fileInputMD = document.getElementById("mdFile");
75+
76+
fileInputMD.addEventListener("change", function (e) {
77+
const file = e.target.files[0];
78+
if (!file) return;
79+
80+
const reader = new FileReader();
81+
82+
reader.onload = function (e) {
83+
const text = e.target.result;
84+
const parsedData = parse_md_queues(text);
85+
queues = parsedData;
86+
weight_table = setup_weight_table(queues);
87+
};
88+
89+
reader.readAsText(file);
90+
});
91+
});
92+
93+
document.getElementById("txtFile").addEventListener("change", function (event) {
94+
const file = event.target.files[0];
95+
if (!file) return;
96+
97+
const reader = new FileReader();
98+
reader.onload = function (e) {
99+
const text = e.target.result;
100+
//document.getElementById('output').textContent = text;
101+
last_output = parse_last_output(text);
102+
};
103+
reader.readAsText(file);
104+
});
105+
106+
function parse_last_output(text) {
107+
const lines = text.split("\n");
108+
const field = [];
109+
110+
let insideField = false;
111+
112+
for (const line of lines) {
113+
const trimmed = line.trim();
114+
115+
if (trimmed.startsWith("# Setup Field")) {
116+
insideField = true;
117+
continue;
118+
}
119+
120+
if (insideField) {
121+
if (trimmed === "" || trimmed.startsWith("#")) break;
122+
123+
// Convert each line into a row of G/N
124+
const row = [...trimmed].map((char) => (char === "X" ? "G" : "N"));
125+
field.push(row);
126+
}
127+
}
128+
129+
// Pad with empty rows at the top to make it 20 rows tall
130+
const totalRows = 20;
131+
const numColumns = 10;
132+
const emptyRow = Array(numColumns).fill("N");
133+
134+
const paddedField = [];
135+
136+
const paddingCount = totalRows - field.length;
137+
for (let i = 0; i < paddingCount; i++) {
138+
paddedField.push([...emptyRow]);
139+
}
140+
141+
// Then add the original rows
142+
paddedField.push(...field);
143+
144+
return paddedField.reverse();
145+
}
146+
function setup_weight_table(queues) {
147+
let out = queues.map((subArr) => subArr.slice()); // copy the array queues to out
148+
for (let i = 0; i < out.length; i++) {
149+
for (let j = 0; j < out[i].length; j++) {
150+
out[i][j] = 0;
151+
}
152+
}
153+
return out;
154+
}
155+
156+
function parse_md_queues(markdown) {
157+
const regex = /```(?:\r?\n)?([\s\S]*?)```/g;
158+
const result = [];
159+
let match;
160+
161+
while ((match = regex.exec(markdown)) !== null) {
162+
const content = match[1].trim(); // get the content inside the code block
163+
const patterns = content.split(",").map((p) => p.trim());
164+
if (patterns.length > 0 && patterns[0]) {
165+
result.push(patterns);
166+
}
167+
}
168+
169+
return result;
170+
}
171+
172+
function print_log() {
173+
console.log("last_output = " + last_output);
174+
console.log("queues = " + queues);
175+
console.log("weight_table = " + weight_table);
176+
}
177+
178+
// function for parsing the path_minimal_strict_file.md and storing the
179+
// information in a meaningful way
180+
function parse_path_minimal_strict_file() {
181+
document.getElementById("mdFile").addEventListener("change", function (e) {});
182+
}
183+
184+
function get_next_queue() {
185+
if (distributedQueues) {
186+
//if distributedQueues is enabled, we want to cycle throught the queues in an
187+
//interesting way. Basically I want it to not repeat a 'minimal' if
188+
//there has been a puzzle in that row already.
189+
return get_next_queue_helper();
190+
} else {
191+
//else let's just iterate through them.
192+
for (let i = 0; i < weight_table.length; i++) {
193+
for (let j = 0; j < weight_table[i].length; j++) {
194+
if (weight_table[i][j] == 0) {
195+
weight_table[i][j] += 1;
196+
return queues[i][j].split("");
197+
}
198+
}
199+
}
200+
}
201+
}
202+
203+
function get_lowest_row(arr) {
204+
let minRowIndex = 0;
205+
let minRowSum = arr[0].reduce((acc, val) => acc + val, 0); // Sum of the first row
206+
207+
// Loop through each row to find the row with the smallest sum of its elements
208+
for (let i = 1; i < arr.length; i++) {
209+
let rowSum = arr[i].reduce((acc, val) => acc + val, 0); // Sum of current row
210+
if (rowSum < minRowSum) {
211+
minRowSum = rowSum;
212+
minRowIndex = i;
213+
}
214+
}
215+
216+
// Return the index of the row with the lowest sum of elements
217+
return minRowIndex;
218+
}
219+
220+
function get_next_queue_helper() {
221+
print_log();
222+
let row = get_lowest_row(weight_table); // Get the row with the smallest minimum value
223+
console.log("row " + row);
224+
let lowestInRow = Math.min(...weight_table[row]); // Get the smallest value in that row
225+
console.log("lowestInRow " + lowestInRow);
226+
// Find the column index of the lowest value in that row
227+
let column = weight_table[row].indexOf(lowestInRow);
228+
console.log("column " + column);
229+
// Increment the weight value for that queue
230+
weight_table[row][column] += 1;
231+
232+
// Return the corresponding queue as a string
233+
return queues[row][column].split("");
234+
}
235+
236+
function find_lowest() {
237+
let lowest = 100;
238+
for (let i = 0; i < weight_table.length; i++) {
239+
for (let j = 0; j < weight_table[i].length; j++) {
240+
if (weight_table[i][j] < lowest) {
241+
lowest = weight_table[i][j];
242+
}
243+
}
244+
}
245+
console.log("lowest = " + lowest);
246+
return lowest;
247+
}

0 commit comments

Comments
 (0)