File size: 3,932 Bytes
67acd34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# %%


# %%
import re
import sys
from pathlib import Path
import pandas as pd

# --- Config ---
ROOT = Path("/notebooks/multimodal_cot/FlowFree/generated/")  # folder containing your files
OUTPUT_CSV = ROOT / "problems_solutions.csv"

# Regex for any triple-quoted grid
TRIPLE_BLOCK_RE = re.compile(r'"""\s*([\s\S]*?)\s*"""', re.M)

# Regex for the specific "Human-readable solution" section
HR_SOLUTION_RE = re.compile(
    r'##\s*Human-readable solution:\s*"""\s*([\s\S]*?)\s*"""',
    re.M,
)

def extract_problem_and_solution(text: str):
    """
    Returns (problem_grid, solution_grid) as strings (without quotes),
    or (None, None) if not found.
    - Problem grid: first triple-quoted block in the file.
    - Solution grid: triple-quoted block following the 'Human-readable solution' header.
    """
    # Find the solution block (authoritative when present)
    sol_match = HR_SOLUTION_RE.search(text)
    solution = sol_match.group(1) if sol_match else None

    # Problem: take the first triple-quoted block in the file
    triples = TRIPLE_BLOCK_RE.findall(text)
    problem = triples[0] if triples else None

    # Safety: If we only found one block, try to infer which it is.
    # Prefer treating the explicitly labeled "Human-readable solution" as solution.
    if problem == solution and problem is not None:
        # If they’re the same (rare), keep as-is; otherwise leave as found.
        pass

    return problem, solution

def scan_folder(root: Path):
    rows = []
    for p in root.rglob("*"):
        if not p.is_file():
            continue
        try:
            text = p.read_text(encoding="utf-8")
        except Exception:
            # Skip unreadable files
            continue

        problem, solution = extract_problem_and_solution(text)
        if problem is None and solution is None:
            continue

        rows.append({
            "path": str(p.relative_to(root)),
            "problem": problem,
            "solution": solution,
        })
    return rows

# %%
rows = scan_folder(ROOT)
df = pd.DataFrame(rows)

df.head()

# %%
df.shape

# %%
df.to_csv('/notebooks/multimodal_cot/FlowFree/flowfree_problems_solutions.csv', index=False)

# %%
#path like 9x9_8c_5_2af3ded9.txt	
# find histogram of grid sizes and number of colors
df['grid_size'] = df['path'].apply(lambda x: x.split('_')[0])
df['num_colors'] = df['path'].apply(lambda x: int(x.split('_')[1][:-1]))
df['num_colors'].value_counts().sort_index().plot(kind='bar', title='Number of colors distribution')


# %%
df['grid_size'].value_counts().sort_index().plot(kind='bar', title='Grid size distribution')

# %%
df

# %%
# add a column with 5*5, 6*6 as easy, 7*7, 8*8 as medium, 9*9, 10*10 as hard
df['difficulty'] = df['grid_size'].apply(lambda x: 'easy' if x in ['5x5', '6x6'] else ('medium' if x in ['7x7', '8x8'] else 'hard'))
df['difficulty'].value_counts().sort_index().plot(kind='bar', title='Difficulty distribution')




# %%
# now sample a test set with 50 easy samples, 50 medium samples and 50 hard samples
# add column as test or train
seed = 42
df['set'] = 'train'
df_copy = df.copy()
df_copy = df_copy.sample(frac=1, random_state=seed).reset_index(drop=True)  # shuffle the dataframe
df_copy.shape

for difficulty in ['easy', 'medium', 'hard']:
    mask = (df_copy['difficulty'] == difficulty) & (df_copy['set'] == 'train')
    test_samples = df_copy[mask].head(50).index
    print(f"Selected {len(test_samples)} samples for difficulty {difficulty}")
    # asd
    df_copy.loc[test_samples, 'set'] = 'test'

# %%
len(test_samples)

# %%
df_copy.shape


# %%
df_copy['set'].value_counts().sort_index().plot(kind='bar', title='Train/Test distribution')

# %%
df_copy[df_copy['set'] == 'test']['difficulty'].value_counts().sort_index().plot(kind='bar', title='Test set difficulty distribution')

# %%


# %%
df_copy.to_csv('/notebooks/multimodal_cot/csvs/flowfree_problems_solutions.csv', index=False)