initial
This commit is contained in:
177
src/arcRender.js
Normal file
177
src/arcRender.js
Normal file
@ -0,0 +1,177 @@
|
||||
/**
|
||||
* ARC Puzzle Renderer for Retool
|
||||
* Renders ARC puzzle training pairs and test input as HTML
|
||||
*/
|
||||
|
||||
// Color mapping for ARC puzzles (0-9)
|
||||
const ARC_COLORS = {
|
||||
0: '#000000', // black
|
||||
1: '#0074D9', // blue
|
||||
2: '#FF4136', // red
|
||||
3: '#2ECC40', // green
|
||||
4: '#FFDC00', // yellow
|
||||
5: '#AAAAAA', // grey
|
||||
6: '#F012BE', // magenta
|
||||
7: '#FF851B', // orange
|
||||
8: '#7FDBFF', // light blue
|
||||
9: '#85144b' // dark red
|
||||
};
|
||||
|
||||
/**
|
||||
* Renders a single grid as HTML
|
||||
* @param {Array<Array<number>>} grid - 2D array representing the grid
|
||||
* @param {string} label - Optional label for the grid (e.g., dimensions)
|
||||
* @returns {string} HTML string for the grid
|
||||
*/
|
||||
function renderGrid(grid, label) {
|
||||
if (!grid || grid.length === 0) {
|
||||
return '<div style="color: red;">Invalid grid data</div>';
|
||||
}
|
||||
|
||||
const rows = grid.length;
|
||||
const cols = grid[0].length;
|
||||
const cellSize = 20; // Fixed 20px per cell
|
||||
|
||||
let html = '<div style="display: inline-block; margin: 10px;">';
|
||||
|
||||
// Add dimension label
|
||||
if (label) {
|
||||
html += `<div style="text-align: center; margin-bottom: 5px; font-size: 12px; color: #333;">${label}</div>`;
|
||||
}
|
||||
|
||||
// Grid container with black border
|
||||
html += `<div style="display: inline-block; border: 1px solid #000000;">`;
|
||||
|
||||
// Render each row
|
||||
for (let r = 0; r < rows; r++) {
|
||||
html += '<div style="display: flex; line-height: 0;">';
|
||||
|
||||
// Render each cell in the row
|
||||
for (let c = 0; c < cols; c++) {
|
||||
const cellValue = grid[r][c];
|
||||
const color = ARC_COLORS[cellValue] || '#FFFFFF'; // Default to white if value unknown
|
||||
|
||||
html += `<div style="width: ${cellSize}px; height: ${cellSize}px; background-color: ${color}; outline: 0.5px solid rgba(200, 200, 200, 0.3); outline-offset: -0.5px;"></div>`;
|
||||
}
|
||||
|
||||
html += '</div>'; // Close row
|
||||
}
|
||||
|
||||
html += '</div>'; // Close grid container
|
||||
html += '</div>'; // Close inline-block wrapper
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a training or test pair (input and output side-by-side)
|
||||
* @param {Array<Array<number>>} inputGrid - Input grid
|
||||
* @param {Array<Array<number>>} outputGrid - Output grid (can be null for test)
|
||||
* @param {number} exampleIndex - Index of the example (for spacing)
|
||||
* @returns {string} HTML string for the pair
|
||||
*/
|
||||
function renderPair(inputGrid, outputGrid, exampleIndex) {
|
||||
let html = '';
|
||||
|
||||
// Add spacing between examples (except for the first one)
|
||||
if (exampleIndex > 0) {
|
||||
html += '<div style="height: 40px;"></div>'; // 2 lines spacing
|
||||
}
|
||||
|
||||
html += '<div style="display: flex; align-items: flex-start; justify-content: flex-start;">';
|
||||
|
||||
// Render input grid
|
||||
const inputRows = inputGrid.length;
|
||||
const inputCols = inputGrid[0].length;
|
||||
html += renderGrid(inputGrid, `${inputCols}×${inputRows}`);
|
||||
|
||||
// Add spacing between input and output (2 cells worth = 40px)
|
||||
html += '<div style="width: 40px;"></div>';
|
||||
|
||||
// Render output grid if provided
|
||||
if (outputGrid) {
|
||||
const outputRows = outputGrid.length;
|
||||
const outputCols = outputGrid[0].length;
|
||||
html += renderGrid(outputGrid, `${outputCols}×${outputRows}`);
|
||||
}
|
||||
|
||||
html += '</div>'; // Close pair container
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function: Renders complete ARC puzzle with training pairs and test input
|
||||
* @param {Object|string} jsonData - ARC puzzle JSON object or JSON string
|
||||
* @returns {string} Complete HTML string for rendering in Retool
|
||||
*/
|
||||
function renderArcPuzzle(jsonData) {
|
||||
// Parse JSON if string is provided
|
||||
let data = jsonData;
|
||||
if (typeof jsonData === 'string') {
|
||||
try {
|
||||
data = JSON.parse(jsonData);
|
||||
} catch (e) {
|
||||
return '<div style="color: red;">Error: Invalid JSON data</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// Validate data structure
|
||||
if (!data || !data.train || !data.test) {
|
||||
return '<div style="color: red;">Error: JSON must contain "train" and "test" properties</div>';
|
||||
}
|
||||
|
||||
let html = '<div style="font-family: Arial, sans-serif;">';
|
||||
|
||||
// === TRAINING SECTION ===
|
||||
html += '<div style="margin-bottom: 60px;">';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Training Examples</h3>';
|
||||
|
||||
data.train.forEach((example, index) => {
|
||||
html += renderPair(example.input, example.output, index);
|
||||
});
|
||||
|
||||
html += '</div>'; // Close training section
|
||||
|
||||
// === TEST SECTION ===
|
||||
html += '<div>';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Test</h3>';
|
||||
|
||||
data.test.forEach((example, index) => {
|
||||
// Only render test INPUT (no output)
|
||||
html += renderPair(example.input, null, index);
|
||||
});
|
||||
|
||||
html += '</div>'; // Close test section
|
||||
|
||||
html += '</div>'; // Close main container
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate function: Renders only the test output grid (for answer display)
|
||||
* @param {Array<Array<number>>} testOutputGrid - 2D array representing test output
|
||||
* @returns {string} HTML string for the test output grid
|
||||
*/
|
||||
function renderTestOutput(testOutputGrid) {
|
||||
if (!testOutputGrid || testOutputGrid.length === 0) {
|
||||
return '<div style="color: red;">No test output data provided</div>';
|
||||
}
|
||||
|
||||
const rows = testOutputGrid.length;
|
||||
const cols = testOutputGrid[0].length;
|
||||
|
||||
let html = '<div style="font-family: Arial, sans-serif;">';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Test Output (Answer)</h3>';
|
||||
html += renderGrid(testOutputGrid, `${cols}×${rows}`);
|
||||
html += '</div>';
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
// Export for use in Retool (if module system is available)
|
||||
// Otherwise, functions are available globally
|
||||
if (typeof module !== 'undefined' && module.exports) {
|
||||
module.exports = { renderArcPuzzle, renderTestOutput };
|
||||
}
|
||||
176
src/arcRender_retool.js
Normal file
176
src/arcRender_retool.js
Normal file
@ -0,0 +1,176 @@
|
||||
/**
|
||||
* ARC Puzzle Renderer for Retool
|
||||
* Renders ARC puzzle training pairs and test input as HTML
|
||||
*
|
||||
* USAGE IN RETOOL:
|
||||
* 1. Copy this entire code into a JavaScript Query or Code Block
|
||||
* 2. In an HTML component, use: {{ renderArcPuzzle(your_data_source) }}
|
||||
*/
|
||||
|
||||
// Color mapping for ARC puzzles (0-9)
|
||||
const ARC_COLORS = {
|
||||
0: '#000000', // black
|
||||
1: '#0074D9', // blue
|
||||
2: '#FF4136', // red
|
||||
3: '#2ECC40', // green
|
||||
4: '#FFDC00', // yellow
|
||||
5: '#AAAAAA', // grey
|
||||
6: '#F012BE', // magenta
|
||||
7: '#FF851B', // orange
|
||||
8: '#7FDBFF', // light blue
|
||||
9: '#85144b' // dark red
|
||||
};
|
||||
|
||||
/**
|
||||
* Renders a single grid as HTML
|
||||
* @param {Array<Array<number>>} grid - 2D array representing the grid
|
||||
* @param {string} label - Optional label for the grid (e.g., dimensions)
|
||||
* @returns {string} HTML string for the grid
|
||||
*/
|
||||
function renderGrid(grid, label) {
|
||||
if (!grid || grid.length === 0) {
|
||||
return '<div style="color: red;">Invalid grid data</div>';
|
||||
}
|
||||
|
||||
const rows = grid.length;
|
||||
const cols = grid[0].length;
|
||||
const cellSize = 20; // Fixed 20px per cell
|
||||
|
||||
let html = '<div style="display: inline-block; margin: 10px;">';
|
||||
|
||||
// Add dimension label
|
||||
if (label) {
|
||||
html += `<div style="text-align: center; margin-bottom: 5px; font-size: 12px; color: #333;">${label}</div>`;
|
||||
}
|
||||
|
||||
// Grid container with black border
|
||||
html += `<div style="display: inline-block; border: 1px solid #000000;">`;
|
||||
|
||||
// Render each row
|
||||
for (let r = 0; r < rows; r++) {
|
||||
html += '<div style="display: flex; line-height: 0;">';
|
||||
|
||||
// Render each cell in the row
|
||||
for (let c = 0; c < cols; c++) {
|
||||
const cellValue = grid[r][c];
|
||||
const color = ARC_COLORS[cellValue] || '#FFFFFF'; // Default to white if value unknown
|
||||
|
||||
html += `<div style="width: ${cellSize}px; height: ${cellSize}px; background-color: ${color}; outline: 0.5px solid rgba(200, 200, 200, 0.3); outline-offset: -0.5px;"></div>`;
|
||||
}
|
||||
|
||||
html += '</div>'; // Close row
|
||||
}
|
||||
|
||||
html += '</div>'; // Close grid container
|
||||
html += '</div>'; // Close inline-block wrapper
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a training or test pair (input and output side-by-side)
|
||||
* @param {Array<Array<number>>} inputGrid - Input grid
|
||||
* @param {Array<Array<number>>} outputGrid - Output grid (can be null for test)
|
||||
* @param {number} exampleIndex - Index of the example (for spacing)
|
||||
* @returns {string} HTML string for the pair
|
||||
*/
|
||||
function renderPair(inputGrid, outputGrid, exampleIndex) {
|
||||
let html = '';
|
||||
|
||||
// Add spacing between examples (except for the first one)
|
||||
if (exampleIndex > 0) {
|
||||
html += '<div style="height: 40px;"></div>'; // 2 lines spacing
|
||||
}
|
||||
|
||||
html += '<div style="display: flex; align-items: flex-start; justify-content: flex-start;">';
|
||||
|
||||
// Render input grid
|
||||
const inputRows = inputGrid.length;
|
||||
const inputCols = inputGrid[0].length;
|
||||
html += renderGrid(inputGrid, `${inputCols}×${inputRows}`);
|
||||
|
||||
// Add spacing between input and output (2 cells worth = 40px)
|
||||
html += '<div style="width: 40px;"></div>';
|
||||
|
||||
// Render output grid if provided
|
||||
if (outputGrid) {
|
||||
const outputRows = outputGrid.length;
|
||||
const outputCols = outputGrid[0].length;
|
||||
html += renderGrid(outputGrid, `${outputCols}×${outputRows}`);
|
||||
}
|
||||
|
||||
html += '</div>'; // Close pair container
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main function: Renders complete ARC puzzle with training pairs and test input
|
||||
* @param {Object|string} jsonData - ARC puzzle JSON object or JSON string
|
||||
* @returns {string} Complete HTML string for rendering in Retool
|
||||
*/
|
||||
function renderArcPuzzle(jsonData) {
|
||||
// Parse JSON if string is provided
|
||||
let data = jsonData;
|
||||
if (typeof jsonData === 'string') {
|
||||
try {
|
||||
data = JSON.parse(jsonData);
|
||||
} catch (e) {
|
||||
return '<div style="color: red;">Error: Invalid JSON data</div>';
|
||||
}
|
||||
}
|
||||
|
||||
// Validate data structure
|
||||
if (!data || !data.train || !data.test) {
|
||||
return '<div style="color: red;">Error: JSON must contain "train" and "test" properties</div>';
|
||||
}
|
||||
|
||||
let html = '<div style="font-family: Arial, sans-serif;">';
|
||||
|
||||
// === TRAINING SECTION ===
|
||||
html += '<div style="margin-bottom: 60px;">';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Training Examples</h3>';
|
||||
|
||||
data.train.forEach((example, index) => {
|
||||
html += renderPair(example.input, example.output, index);
|
||||
});
|
||||
|
||||
html += '</div>'; // Close training section
|
||||
|
||||
// === TEST SECTION ===
|
||||
html += '<div>';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Test</h3>';
|
||||
|
||||
data.test.forEach((example, index) => {
|
||||
// Only render test INPUT (no output)
|
||||
html += renderPair(example.input, null, index);
|
||||
});
|
||||
|
||||
html += '</div>'; // Close test section
|
||||
|
||||
html += '</div>'; // Close main container
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Separate function: Renders only the test output grid (for answer display)
|
||||
* Use this in a separate HTML component with a toggle to show/hide answers
|
||||
* @param {Array<Array<number>>} testOutputGrid - 2D array representing test output
|
||||
* @returns {string} HTML string for the test output grid
|
||||
*/
|
||||
function renderTestOutput(testOutputGrid) {
|
||||
if (!testOutputGrid || testOutputGrid.length === 0) {
|
||||
return '<div style="color: red;">No test output data provided</div>';
|
||||
}
|
||||
|
||||
const rows = testOutputGrid.length;
|
||||
const cols = testOutputGrid[0].length;
|
||||
|
||||
let html = '<div style="font-family: Arial, sans-serif;">';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Test Output (Answer)</h3>';
|
||||
html += renderGrid(testOutputGrid, `${cols}×${rows}`);
|
||||
html += '</div>';
|
||||
|
||||
return html;
|
||||
}
|
||||
178
src/arcRender_retool_inline.js
Normal file
178
src/arcRender_retool_inline.js
Normal file
@ -0,0 +1,178 @@
|
||||
/**
|
||||
* ARC Puzzle Renderer for Retool - INLINE VERSION
|
||||
*
|
||||
* USAGE IN RETOOL HTML COMPONENT:
|
||||
* Paste this entire code block wrapped in {{ ... }} in your HTML component
|
||||
*
|
||||
* Example for database query:
|
||||
* {{
|
||||
* (function() {
|
||||
* // ... paste this entire file content here ...
|
||||
* })()
|
||||
* }}
|
||||
*/
|
||||
|
||||
(function() {
|
||||
// =============================================
|
||||
// GET DATA FROM RETOOL QUERY
|
||||
// =============================================
|
||||
|
||||
// ADJUST THESE LINES based on your Retool setup:
|
||||
const selectedPuzzle = problem_select.value; // Your selector name
|
||||
|
||||
// Handle empty selection
|
||||
if (!selectedPuzzle) {
|
||||
return '<div style="color: orange; padding: 20px; font-family: Arial;">No puzzle data available. Please select a problem.</div>';
|
||||
}
|
||||
|
||||
// Use the selected row directly
|
||||
const row = selectedPuzzle;
|
||||
|
||||
// Parse JSON if it's stored as text, otherwise use as-is
|
||||
let puzzleData;
|
||||
try {
|
||||
puzzleData = typeof row.json === 'string'
|
||||
? JSON.parse(row.json)
|
||||
: row.json;
|
||||
} catch (e) {
|
||||
return '<div style="color: red; padding: 20px; font-family: Arial;">Error parsing JSON: ' + e.message + '</div>';
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// ARC RENDERER CODE
|
||||
// =============================================
|
||||
|
||||
// Color mapping for ARC puzzles (0-9)
|
||||
const ARC_COLORS = {
|
||||
0: '#000000', // black
|
||||
1: '#0074D9', // blue
|
||||
2: '#FF4136', // red
|
||||
3: '#2ECC40', // green
|
||||
4: '#FFDC00', // yellow
|
||||
5: '#AAAAAA', // grey
|
||||
6: '#F012BE', // magenta
|
||||
7: '#FF851B', // orange
|
||||
8: '#7FDBFF', // light blue
|
||||
9: '#85144b' // dark red
|
||||
};
|
||||
|
||||
/**
|
||||
* Renders a single grid as HTML
|
||||
*/
|
||||
function renderGrid(grid, label) {
|
||||
if (!grid || grid.length === 0) {
|
||||
return '<div style="color: red;">Invalid grid data</div>';
|
||||
}
|
||||
|
||||
const rows = grid.length;
|
||||
const cols = grid[0].length;
|
||||
const cellSize = 20; // Fixed 20px per cell
|
||||
|
||||
let html = '<div style="display: inline-block; margin: 10px;">';
|
||||
|
||||
// Add dimension label
|
||||
if (label) {
|
||||
html += `<div style="text-align: center; margin-bottom: 5px; font-size: 12px; color: #333;">${label}</div>`;
|
||||
}
|
||||
|
||||
// Grid container with black border
|
||||
html += `<div style="display: inline-block; border: 1px solid #000000;">`;
|
||||
|
||||
// Render each row
|
||||
for (let r = 0; r < rows; r++) {
|
||||
html += '<div style="display: flex; line-height: 0;">';
|
||||
|
||||
// Render each cell in the row
|
||||
for (let c = 0; c < cols; c++) {
|
||||
const cellValue = grid[r][c];
|
||||
const color = ARC_COLORS[cellValue] || '#FFFFFF';
|
||||
|
||||
html += `<div style="width: ${cellSize}px; height: ${cellSize}px; background-color: ${color}; outline: 0.5px solid rgba(200, 200, 200, 0.3); outline-offset: -0.5px;"></div>`;
|
||||
}
|
||||
|
||||
html += '</div>'; // Close row
|
||||
}
|
||||
|
||||
html += '</div>'; // Close grid container
|
||||
html += '</div>'; // Close inline-block wrapper
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a training or test pair (input and output side-by-side)
|
||||
*/
|
||||
function renderPair(inputGrid, outputGrid, exampleIndex) {
|
||||
let html = '';
|
||||
|
||||
// Add spacing between examples (except for the first one)
|
||||
if (exampleIndex > 0) {
|
||||
html += '<div style="height: 40px;"></div>';
|
||||
}
|
||||
|
||||
html += '<div style="display: flex; align-items: flex-start; justify-content: flex-start;">';
|
||||
|
||||
// Render input grid
|
||||
const inputRows = inputGrid.length;
|
||||
const inputCols = inputGrid[0].length;
|
||||
html += renderGrid(inputGrid, `${inputCols}×${inputRows}`);
|
||||
|
||||
// Add spacing between input and output (2 cells worth = 40px)
|
||||
html += '<div style="width: 40px;"></div>';
|
||||
|
||||
// Render output grid if provided
|
||||
if (outputGrid) {
|
||||
const outputRows = outputGrid.length;
|
||||
const outputCols = outputGrid[0].length;
|
||||
html += renderGrid(outputGrid, `${outputCols}×${outputRows}`);
|
||||
}
|
||||
|
||||
html += '</div>'; // Close pair container
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main rendering function
|
||||
*/
|
||||
function renderArcPuzzle(data) {
|
||||
// Validate data structure
|
||||
if (!data || !data.train || !data.test) {
|
||||
return '<div style="color: red;">Error: JSON must contain "train" and "test" properties</div>';
|
||||
}
|
||||
|
||||
let html = '<div style="font-family: Arial, sans-serif;">';
|
||||
|
||||
// === TRAINING SECTION ===
|
||||
html += '<div style="margin-bottom: 60px;">';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Training Examples</h3>';
|
||||
|
||||
data.train.forEach((example, index) => {
|
||||
html += renderPair(example.input, example.output, index);
|
||||
});
|
||||
|
||||
html += '</div>'; // Close training section
|
||||
|
||||
// === TEST SECTION ===
|
||||
html += '<div>';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Test</h3>';
|
||||
|
||||
data.test.forEach((example, index) => {
|
||||
// Render both test input and output
|
||||
html += renderPair(example.input, example.output, index);
|
||||
});
|
||||
|
||||
html += '</div>'; // Close test section
|
||||
|
||||
html += '</div>'; // Close main container
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// RENDER AND RETURN
|
||||
// =============================================
|
||||
|
||||
return renderArcPuzzle(puzzleData);
|
||||
|
||||
})()
|
||||
133
src/arcRender_testOutput_inline.js
Normal file
133
src/arcRender_testOutput_inline.js
Normal file
@ -0,0 +1,133 @@
|
||||
/**
|
||||
* ARC Test Output Renderer for Retool - INLINE VERSION
|
||||
*
|
||||
* This displays ONLY the test output (the answer to the puzzle)
|
||||
* Use this in a separate HTML component with a toggle control
|
||||
*
|
||||
* USAGE IN RETOOL HTML COMPONENT:
|
||||
* {{
|
||||
* (function() {
|
||||
* // ... paste this entire file content here ...
|
||||
* })()
|
||||
* }}
|
||||
*/
|
||||
|
||||
(function() {
|
||||
// =============================================
|
||||
// CHECK TOGGLE STATE
|
||||
// =============================================
|
||||
|
||||
// ADJUST THIS LINE: Your toggle/button component name
|
||||
const shouldShow = toggleShowAnswer.value; // Change to your toggle component name
|
||||
|
||||
if (!shouldShow) {
|
||||
return ''; // Return empty if toggle is off
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// GET DATA FROM RETOOL QUERY
|
||||
// =============================================
|
||||
|
||||
// ADJUST THIS LINE: Your query name
|
||||
const queryData = arcPuzzleQuery.data;
|
||||
|
||||
// Handle empty query
|
||||
if (!queryData || queryData.length === 0) {
|
||||
return '<div style="color: orange; padding: 20px; font-family: Arial;">No puzzle data available.</div>';
|
||||
}
|
||||
|
||||
// Get the first row from query result
|
||||
const row = queryData[0];
|
||||
|
||||
// Parse JSON if it's stored as text, otherwise use as-is
|
||||
let puzzleData;
|
||||
try {
|
||||
puzzleData = typeof row.json === 'string'
|
||||
? JSON.parse(row.json)
|
||||
: row.json;
|
||||
} catch (e) {
|
||||
return '<div style="color: red; padding: 20px; font-family: Arial;">Error parsing JSON: ' + e.message + '</div>';
|
||||
}
|
||||
|
||||
// Check if test output exists
|
||||
if (!puzzleData.test || !puzzleData.test[0] || !puzzleData.test[0].output) {
|
||||
return '<div style="color: orange; padding: 20px; font-family: Arial;">No test output available for this puzzle.</div>';
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// RENDERER CODE
|
||||
// =============================================
|
||||
|
||||
// Color mapping for ARC puzzles (0-9)
|
||||
const ARC_COLORS = {
|
||||
0: '#000000', // black
|
||||
1: '#0074D9', // blue
|
||||
2: '#FF4136', // red
|
||||
3: '#2ECC40', // green
|
||||
4: '#FFDC00', // yellow
|
||||
5: '#AAAAAA', // grey
|
||||
6: '#F012BE', // magenta
|
||||
7: '#FF851B', // orange
|
||||
8: '#7FDBFF', // light blue
|
||||
9: '#85144b' // dark red
|
||||
};
|
||||
|
||||
/**
|
||||
* Renders a single grid as HTML
|
||||
*/
|
||||
function renderGrid(grid, label) {
|
||||
if (!grid || grid.length === 0) {
|
||||
return '<div style="color: red;">Invalid grid data</div>';
|
||||
}
|
||||
|
||||
const rows = grid.length;
|
||||
const cols = grid[0].length;
|
||||
const cellSize = 20; // Fixed 20px per cell
|
||||
|
||||
let html = '<div style="display: inline-block; margin: 10px;">';
|
||||
|
||||
// Add dimension label
|
||||
if (label) {
|
||||
html += `<div style="text-align: center; margin-bottom: 5px; font-size: 12px; color: #333;">${label}</div>`;
|
||||
}
|
||||
|
||||
// Grid container with black border
|
||||
html += `<div style="display: inline-block; border: 1px solid #000000;">`;
|
||||
|
||||
// Render each row
|
||||
for (let r = 0; r < rows; r++) {
|
||||
html += '<div style="display: flex; line-height: 0;">';
|
||||
|
||||
// Render each cell in the row
|
||||
for (let c = 0; c < cols; c++) {
|
||||
const cellValue = grid[r][c];
|
||||
const color = ARC_COLORS[cellValue] || '#FFFFFF';
|
||||
|
||||
html += `<div style="width: ${cellSize}px; height: ${cellSize}px; background-color: ${color}; outline: 0.5px solid rgba(200, 200, 200, 0.3); outline-offset: -0.5px;"></div>`;
|
||||
}
|
||||
|
||||
html += '</div>'; // Close row
|
||||
}
|
||||
|
||||
html += '</div>'; // Close grid container
|
||||
html += '</div>'; // Close inline-block wrapper
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
// =============================================
|
||||
// RENDER TEST OUTPUT
|
||||
// =============================================
|
||||
|
||||
const testOutputGrid = puzzleData.test[0].output;
|
||||
const rows = testOutputGrid.length;
|
||||
const cols = testOutputGrid[0].length;
|
||||
|
||||
let html = '<div style="font-family: Arial, sans-serif;">';
|
||||
html += '<h3 style="margin: 0 0 20px 0; color: #333;">Test Output (Answer)</h3>';
|
||||
html += renderGrid(testOutputGrid, `${cols}×${rows}`);
|
||||
html += '</div>';
|
||||
|
||||
return html;
|
||||
|
||||
})()
|
||||
Reference in New Issue
Block a user