Debian Codegraph @

src/activities/guess24/tools/build-datas.php

raw
1 <?php
2 // Convert Guess24-difficulty.csv and Guess24-solutions.csv into a json file with required information
3 // Usage from guess24 directory: $ php build-datas.php
4 // Output a report on stdout and a json file for the activity.
5
6 $outputFile = "../resource/guess24.json";
7 $difficultyFile = "Guess24-difficulty.csv";
8 $solutionFile = "Guess24-solutions.csv";
9 $limitRate = 80.0;
10 $maxRate = 40.0;
11 $verbose = false;
12 $noSolutions = false;
13
14 $short_options = "vschr:";
15 $long_options = ["verbose", "solutions", "help", "rate:"];
16 $options = getopt($short_options, $long_options);
17
18 if(isset($options["v"]) || isset($options["verbose"])) {
19 $verbose = true;
20 }
21 if(isset($options["s"]) || isset($options["solutions"])) {
22 $noSolutions = true;
23 }
24 if(isset($options["r"]) || isset($options["rate"])) {
25 $limitRate = isset($options["r"]) ? $options["r"] : $options["rate"];
26 if (is_numeric($limitRate))
27 $limitRate = (float) $limitRate;
28 else {
29 print("Rate parameter should be numeric.\n");
30 exit(1);
31 }
32 }
33
34 if(isset($options["h"]) || isset($options["help"])) {
35 print("Usage: php build-datas.php [OPTIONS]\n");
36 print("Create file `$outputFile` from `$difficultyFile` and `$solutionFile`.\nPrint a report on stdout.\n\n");
37 print("Options\n");
38 print(" -v, --verbose print information on rejected formulas.\n");
39 print(" -s, --solutions no solutions in output file.\n");
40 print(" -r, --rate [LIMITRATE] set minimum solved rate required (default 80).\n");
41 print(" -h, --help display this help and exit.\n");
42 exit(0);
43 }
44
45 // First line contains csv headers
46 function stripFirstLine($text) {
47 return substr($text, strpos($text, "\n") + 1);
48 }
49
50 $solutions = array();
51 $problems = array();
52 $hardProblems = array();
53 $rejected = 0;
54 // Load files, remove non-breaking spaces and split into an array
55 $ranked = preg_split("/\n/", stripFirstLine(trim(preg_replace("/\xc2\xa0/", '', file_get_contents($difficultyFile)))));
56 $solutext = preg_split("/\n/", stripFirstLine(trim(preg_replace("/\xc2\xa0/", '', file_get_contents($solutionFile)))));
57
58 foreach($solutext as $sol) {
59 $sols = preg_split("/,/", $sol);
60 array_shift($sols);
61 $puz = trim(array_shift($sols));
62 while (trim($sols[array_key_last($sols)]) === "") { // Remove last solutions if empty
63 array_pop($sols);
64 }
65 $translatedSols = Array();
66 foreach($sols as $idx => $val) {
67 $val = preg_replace(array("/×/", "/÷/"), array("*", "/"), trim($val));
68 // $val = trim($val);
69 $result = 0;
70 $sols[$idx] = $val;
71 if (preg_match_all("/\([^\)]+\)/", $val, $matches)) { // Check for rational intermediate result
72 foreach($matches[0] as $formula) {
73 $formula = substr($formula, 1, strlen($formula) - 2);
74 eval("\$result = $formula;");
75 if ($result != floor($result)) {
76 if ($verbose)
77 print("$puz solution $val rejected: ($formula) = $result\n");
78 array_splice($sols, $idx, 1);
79 $rejected++;
80 continue 2;
81 }
82 }
83 }
84 $translatedSols[] = $val;
85 }
86 $solutions[$puz] = $translatedSols;
87 }
88 foreach($ranked as $line) {
89 list($rank, $puzzle, $amt, $solvedRate, $sigmaMean, $sigmaSTD) = preg_split("/,/", $line);
90 $puzzle = trim($puzzle);
91 $solvedRate = (double) substr($solvedRate, 0, strlen($solvedRate) - 1);
92 if (($solvedRate > $limitRate) && count($solutions[$puzzle])) {
93 $problems[] = array(
94 'puzzle' => $puzzle,
95 'solutions' => $solutions[$puzzle]
96 );
97 } else if (($solvedRate > $maxRate) && count($solutions[$puzzle])) {
98 $hardProblems[] = array(
99 'puzzle' => $puzzle,
100 'solutions' => $solutions[$puzzle]
101 );
102 }
103 }
104
105 $difficount = array(0, 0, 0, 0);
106 foreach($problems as $num => $problem) {
107 $complexity = 3;
108 foreach($problem['solutions'] as $solution) {
109 if (preg_match("/\//", $solution)) { // if one solution has divide
110 $complexity = min($complexity, 3);
111 continue;
112 }
113 if (preg_match("/\*/", $solution)) { // if one solution has multiply
114 $complexity = min($complexity, 2);
115 continue;
116 }
117 $complexity = min($complexity, 1); // if one solution has no multiply nor divide
118 }
119 $problems[$num]['complexity'] = $complexity;
120 if ($noSolutions)
121 unset($problems[$num]['solutions']);
122 $difficount[$complexity - 1]++;
123 }
124
125 foreach($hardProblems as $num => $problem) {
126 $hardProblems[$num]['complexity'] = 4;
127 $difficount[3]++;
128 }
129
130 file_put_contents($outputFile, json_encode(array_merge($problems, $hardProblems), JSON_PRETTY_PRINT));
131
132 // Statistics on stdout
133 $total = $difficount[0] + $difficount[1] + $difficount[2] + $difficount[3];
134 print("Problems with a solved rate greater than $limitRate%\n");
135 print(str_repeat("-", 20)."\n");
136 print("Complexity 1: {$difficount[0]} problems\n");
137 print("Complexity 2: {$difficount[1]} problems\n");
138 print("Complexity 3: {$difficount[2]} problems\n");
139 print("Complexity 4: {$difficount[3]} problems\n");
140 print("Total: $total problems\n");
141 print(str_repeat("-", 20)."\n");
142 print("Output file: $outputFile\n");
143 print("Output file size: ".filesize($outputFile)." bytes\n");
144 print("$rejected solutions rejected for a rational intermediate result\n");
145
146 ?>