1use std::panic::RefUnwindSafe;
6use crate::examples::*;
7use crate::grid::*;
8use crate::cats::Colour;
10use crate::cats::Transformation;
11pub fn check_examples(ex: &Examples, func: &dyn Fn(&Example) -> bool) -> bool {
14 for e in &ex.examples {
15 if !func(e) {
16 return false;
17 }
18 }
19
20 true
21}
22
23pub fn check_input_grids(ex: &Examples, func: &dyn Fn(&Grid) -> bool) -> bool {
24 for e in &ex.examples {
25 if !func(&e.input.grid) {
26 return false;
27 }
28 }
29
30 true
31}
32
33pub fn check_output_grids(ex: &Examples, func: &dyn Fn(&Grid) -> bool) -> bool {
34 for e in &ex.examples {
35 if !func(&e.output.grid) {
36 return false;
37 }
38 }
39
40 true
41}
42
43pub fn experiment_grid(ex: &Examples, file: &str, experiment: usize, _trans: Transformation, func: &(dyn Fn(&Grid, &Grid, &mut usize) -> Grid + RefUnwindSafe)) -> Vec<Grid> {
69 let mut n = usize::MAX;
70
71 for (attempts, e) in ex.examples.iter().enumerate() {
72 let grid = &e.input.grid;
73 let target = &e.output.grid;
74
75 if target.size() == 0 {
76 return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
77 }
78
79 loop {
80 if n == 0 {
81 return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
82 } else {
83 let ans = func(grid, target, &mut n);
84 if ans.equals(target) != Colour::Same {
92 if n == usize::MAX {
93 if attempts > 0 && (experiment < 100000 || attempts > 1) {
94 println!("{file} {experiment:<4}: {attempts} worked out of {}", ex.examples.len());
95 }
96 return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
97 }
98
99 n -= 1;
100 } else {
101 break;
102 }
103 }
104 }
105 }
106
107 ex.tests.iter().map(|test| {
109 let ans = func(&test.input.grid, &ex.examples[0].output.grid, &mut n);
110
111 ans
119 })
120 .collect()
121}
122
123pub fn experiment_example(ex: &Examples, file: &str, experiment: usize, _trans: Transformation, func: &(dyn Fn(&Example) -> Grid + RefUnwindSafe)) -> Vec<Grid> {
124 for (attempts, e) in ex.examples.iter().enumerate() {
125 let target = &e.output.grid;
127 let ans = func(e);
128
129 if ans == Grid::trivial() || ans.equals(target) != Colour::Same {
139 if attempts > 0 && (experiment < 100000 || attempts > 1) {
140 println!("{file} {experiment:<4}: {attempts} worked out of {}", ex.examples.len());
141 }
142 return ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>();
143 }
144 }
145
146 ex.tests.iter().map(|test| {
148 let ans = func(test);
149
150 ans
158 })
159 .collect()
160}
161
162pub fn experiment_colours(ex: &Examples, func: &(dyn Fn(&Example, Colour) -> Grid + RefUnwindSafe)) -> Vec<Grid> {
163 const COLOURS: &[Colour] = Colour::base_colours();
164
165 'outer:
166 for colour in COLOURS {
167 for e in ex.examples.iter() {
168 let target = &e.output.grid;
169 let ans = func(e, *colour);
170
171 if ans == Grid::trivial() || ans.equals(target) != Colour::Same {
172 continue 'outer;
173 }
174 }
175
176 let ans = ex.tests.iter().map(|test| {
178 let ans = func(test, *colour);
179
180 ans
181 })
182 .collect();
183
184 return ans;
185 }
186
187 ex.tests.iter().map(|_| Grid::trivial()).collect::<Vec<_>>()
188}