InternBootcamp/internbootcamp/bootcamp/kakurasu/kakurasu.py
Yongkang Chen a8249acc18
update to tech report version (#10)
* feat(run_eval): add checkpoint resume functionality and update example documentation;
- update new bootcamp benchmark dataset

* refactor(data_pipeline): optimize data generation pipeline; add multiple preset configurations for data generation

* docs: update bootcamp list and add new scripts

- Update Fulllist_InternBootcamp.md with new bootcamps and categories
- Add new scripts to .gitignore:
  - examples/pipelines/filter_autogen_configs.py
  - examples/pipelines/quickgen_data_configs_from_eval_meta.py
- Update dependencies in setup.py:
  - Add scipy and scikit-learn

* refactor(internbootcamp): update bootcamp modules and improve error handling

- Update import statements in __init__.py files
- Add timestamp to target directory name in verl_data_preprocess.py
- Improve error handling and scoring logic in bootcamp_judger.py
- Remove unnecessary comments and update puzzle descriptions in multiple files
2025-08-28 12:39:47 +08:00

111 lines
4.4 KiB
Python
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""### 谜题描述
Kakurasu is a logic puzzle played on a rectangular grid (typically N×N). Each cell in the grid can be either shaded or unshaded. The puzzle provides target values for each row and column, and the goal is to shade cells such that:
1. **Row Constraints**: For every row, the sum of the *column indices* of the shaded cells in that row equals the target value specified for that row.
- Example: If a row's target is 7, you might shade cells in columns 3 and 4 (since 3 + 4 = 7).
2. **Column Constraints**: For every column, the sum of the *row indices* of the shaded cells in that column equals the target value specified for that column.
- Example: If a column's target is 5, you might shade cells in rows 2 and 3 (since 2 + 3 = 5).
3. **Unique Contributions**: A shaded cell at position (row *i*, column *j*) contributes its *column index* **j** to its row's sum and its *row index* **i** to its column's sum. These dual contributions must satisfy both the row and column targets simultaneously.
4. **No Overlapping Rules**: Unlike Sudoku, there are no region constraints—only row and column sums matter. However, cells cannot be \"partially\" shaded; they are either fully shaded or unshaded.
The puzzle is solved when all row and column targets are satisfied without contradiction.
请完成上述谜题的训练场环境类实现,包括所有必要的方法。
"""
from bootcamp import Basebootcamp
import random
import re
import ast
class Kakurasubootcamp(Basebootcamp):
def __init__(self, n=5):
self.n = n
def case_generator(self):
n = self.n
grid = [[random.choice([0, 1]) for _ in range(n)] for _ in range(n)]
row_targets = []
for i in range(n):
total = sum((j + 1) * cell for j, cell in enumerate(grid[i]))
row_targets.append(total)
col_targets = []
for j in range(n):
total = sum((i + 1) * grid[i][j] for i in range(n))
col_targets.append(total)
return {
'n': n,
'row_targets': row_targets,
'col_targets': col_targets
}
@staticmethod
def prompt_func(question_case) -> str:
n = question_case['n']
row_targets = question_case['row_targets']
col_targets = question_case['col_targets']
return f"""你正在解决一个Kakurasu谜题。这是一个{n}x{n}的网格谜题,目标是根据行和列的约束条件涂黑单元格。
规则:
1. 每行中被涂黑单元格的列索引从左到右为1到{n})之和等于该行的目标值。
2. 每列中被涂黑单元格的行索引从上到下为1到{n})之和等于该列的目标值。
3. 每个单元格必须明确涂黑1或未涂黑0
当前谜题的行目标值(从上到下):{row_targets}
当前谜题的列目标值(从左到右):{col_targets}
请将你的解答格式化为{n}x{n}的二维数组其中1表示涂黑0表示未涂黑并用[answer]和[/answer]标签包裹。例如:
[answer]
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
[/answer]
"""
@staticmethod
def extract_output(output):
matches = re.findall(r'\[answer\](.*?)\[/answer\]', output, re.DOTALL)
if not matches:
return None
last_match = matches[-1].strip()
try:
solution = ast.literal_eval(last_match)
return solution
except (SyntaxError, ValueError):
return None
@classmethod
def _verify_correction(cls, solution, identity):
n = identity['n']
row_targets = identity['row_targets']
col_targets = identity['col_targets']
# 验证答案结构
if not isinstance(solution, list) or len(solution) != n:
return False
for row in solution:
if not isinstance(row, list) or len(row) != n:
return False
for cell in row:
if cell not in (0, 1):
return False
# 验证行约束
for i in range(n):
row_sum = sum((j + 1) * cell for j, cell in enumerate(solution[i]))
if row_sum != row_targets[i]:
return False
# 验证列约束
for j in range(n):
col_sum = sum((i + 1) * solution[i][j] for i in range(n))
if col_sum != col_targets[j]:
return False
return True