arc_agi/
rules.rs

1//use std::collections::BTreeMap;
2//use pathfinding::prelude::Matrix;
3use itertools::Itertools;
4use crate::cats::*;
5//use crate::cell::*;
6use crate::examples::*;
7use crate::grid::*;
8use crate::shape::*;
9//use crate::rules::Colour::Mixed;
10//use crate::experiments::*;
11
12pub fn move_only(grid: &Grid, n: &mut usize) -> Grid {
13    let func = [Grid::move_down, Grid::move_up, Grid::move_right, Grid::move_left];
14    if *n == usize::MAX {
15        *n = func.len();
16    }
17    if *n == 0 {
18        Grid::trivial()
19    } else {
20        func[func.len() - *n](grid)
21    }
22}
23
24// Experiments on gravity
25pub fn gravity_only(grid: &Grid, n: &mut usize) -> Grid {
26    let func = [Grid::stretch_down, Grid::gravity_down, Grid::gravity_up];
27    if *n == usize::MAX {
28        *n = func.len();
29    }
30    if *n == 0 {
31        Grid::trivial()
32    } else {
33        func[func.len() - *n](grid)
34    }
35}
36
37// Experiments on mirroring and duplicating
38pub fn mirror_only(grid: &Grid, n: &mut usize) -> Grid {
39    let func = [Grid::mirror_right, Grid::mirror_left, Grid::mirror_down, Grid::mirror_up, Grid::dup_right, Grid::dup_left, Grid::dup_down, Grid::dup_up];
40    if *n == usize::MAX {
41        *n = func.len();
42    }
43    if *n == 0 {
44        Grid::trivial()
45    } else {
46        func[func.len() - *n](grid)
47    }
48}
49
50/*
51pub fn gravity_only(grid: &Grid, n: &mut usize) -> Grid {
52    let func = [&Grid::stretch_down, &Grid::gravity_down, &Grid::gravity_up];
53    apply(grid, n, &func)
54}
55
56pub fn apply(grid: &Grid, n: &mut usize, func: &[&dyn Fn(&Grid) -> Grid]) -> Grid {
57    if *n == usize::MAX {
58        *n = func.len();
59    }
60    if *n == 0 {
61        Grid::trivial()
62    } else {
63        func[func.len() - *n](grid)
64    }
65}
66*/
67
68pub fn diff_only(grid: &Grid, colour: Colour, n: &mut usize) -> Grid {
69    let func = [Grid::diff_only_and, Grid::diff_only_or, Grid::diff_only_xor, Grid::diff_black_same, Grid::diff_other_same];
70    if *n == usize::MAX {
71        *n = func.len();
72    }
73    if *n == 0 {
74        Grid::trivial()
75    } else {
76        func[func.len() - *n](grid, colour)
77    }
78}
79
80pub fn transform_only(grid: &Grid, n: &mut usize) -> Grid {
81    let func = [Grid::rot_00,  Grid::rot_90, Grid::rot_180, Grid::rot_270, Grid::transposed, Grid::mirrored_rows, Grid::mirrored_cols];
82
83    if *n == usize::MAX {
84        *n = func.len();
85    }
86    if *n == 0 {
87        Grid::trivial()
88    } else {
89        func[func.len() - *n](grid)
90    }
91}
92
93/*
94pub fn try_colours(grid: &Grid, n: &mut usize, func: &(dyn Fn(&Grid, Colour) -> Grid)) -> Grid {
95    //let colours = Colour::colours();
96    const COLOURS: &[Colour]  = Colour::base_colours();
97
98    if *n == usize::MAX {
99        *n = COLOURS.len();
100    }
101    if *n == 0 {
102        Grid::trivial()
103    } else {
104        func(grid, COLOURS[*n])
105    }
106}
107*/
108
109pub fn simple_diffs(exs: &Examples) -> Option<Vec<Grid>> {
110    let mut diffs: Vec<Grid> = Vec::new();
111
112    for e in exs.examples.iter() {
113        if let Some(diff) = e.input.grid.diff(&e.output.grid) {
114            diffs.push(diff);
115        } else {
116            return None;
117        }
118    }
119
120    Some(diffs)
121}
122
123pub fn difference(exs: &Examples) {
124//let mut done = false;
125    for e in exs.examples.iter() {
126        if let Some(diff) = e.input.grid.diff(&e.output.grid) {
127            diff.show_full();
128//done = true;
129        }
130        /*
131        let (count, total) = diff.count_diff();
132        println!("{file} counts: {count} of {total} {}", 
133                 if count < 9 { "easy" } else { "hard" });
134        */
135    }
136
137    exs.tests.iter().for_each(|tests| tests.input.grid.show());
138//if done { println!("++++"); }
139}
140
141pub fn permutations(v: &[usize]) -> Vec<Vec<&usize>> {
142    let mut perms: Vec<Vec<&usize>> = Vec::new();
143
144    for perm in v.iter().permutations(v.len()).unique() {
145        perms.push(perm);
146    }
147
148    perms
149}
150
151pub fn difference_shapes(exs: &Examples, coloured: bool) {
152    for shapes in &exs.examples {
153        if let Some(diff) = shapes.input.grid.diff(&shapes.output.grid) {
154            diff.show_full();
155        }
156        let mut used: Vec<Shape> = Vec::new();
157        let inp = if coloured { &shapes.input.coloured_shapes } else { &shapes.input.shapes };
158        let out = if coloured { &shapes.output.coloured_shapes } else { &shapes.output.shapes };
159
160        for si in inp.shapes.iter() {
161            for so in out.shapes.iter() {
162                if si.orow == so.orow && si.ocol == so.ocol {
163                    if let Some(diff) = si.diff(so) {
164                        println!("Same size");
165                        diff.show_full();
166                    } else {
167                        println!("Different sizes");
168                        si.show();
169                        so.show();
170                    }
171                } else if !used.contains(so) {
172                    used.push(so.clone());
173                    println!("Other shapes");
174                    so.show();
175                }
176            }
177        }
178    }
179
180    println!("Test shapes");
181    for test in exs.tests.iter() {
182        for ex in test.input.shapes.shapes.iter() {
183            ex.show();
184        }
185    }
186}
187
188/*
189pub fn repeat_pattern(ex: &Example, colour: Colour) -> Grid {
190    let shape = &ex.input.grid.as_shape();
191    let posns = shape.colour_position(colour);
192    let rows = shape.cells.rows;
193    let cols = shape.cells.columns;
194println!("{rows}/{cols} {colour:?}");
195shape.show();
196    let mut shapes = Shapes::new_sized(rows.pow(2), cols.pow(2));
197
198    for (r, c) in posns.iter() {
199        let or = r * 3;
200        let oc = c * 3;
201
202        shapes.shapes.push(shape.translate_absolute(or, oc));
203    }
204ex.output.grid.show();
205shapes.to_grid().show();
206    shapes.to_grid()
207}
208*/
209pub fn repeat_pattern(ex: &Example, colour: Colour) -> Grid {
210    let shape = ex.input.grid.as_shape();
211    let (rs, cs) = ex.input.grid.dimensions();
212
213    if colour == Colour::Black {
214        shape.chequer(rs, cs, &|r,c| ex.input.grid.cells[(r / rs,c / cs)].colour != colour, &|s| s.clone(), true).to_grid()
215    } else {
216        shape.chequer(rs, cs, &|r,c| ex.input.grid.cells[(r / rs,c / cs)].colour == colour, &|s| s.clone(), true).to_grid()
217    }
218}
219
220/* Diff
221 *
222 * 1* and 3*    1* is add
223 *
224 * fill same between x and y    22168020.json
225 * fill same between x and y    22eb0ac0.json
226 * line to limits x and y       178fcbfb.json
227 * line to limits x and y       23581191.json
228 * diag to limits or other      508bd3b6.json
229 *
230 * 2* and 3*    2* is delete
231 *
232 * take middle x and y          d23f8c26.json
233 */
234
235/*
236   Priors
237   ------
238
239   1. Object permanence, they can move, be occluded, rotate etc. but remain
240      the same object.
241   2. Agentness, object with 'purpose'. It has 'goals' and can influence other       object and/or agents.
242   3. Simple physics, distance, movement, 
243   4. Number, basic counting
244   5. Rotation, symmetry etc.
245*/