-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMirror.ts
64 lines (56 loc) · 2.06 KB
/
Mirror.ts
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
import { Bool } from 'o1js';
import { BoxFields } from '../Box';
import { DIR_ALL } from '../utils/direction';
/**
* A mirror box bounces the input laser.
*
* If the laser is coming diagonally w.r.t the mirror direction,
* it should bounce off to the other direction (d+2); or,
* a laser can face the mirror directly and it should bounce from the same
* direction.
*
* There shouldn't be any other out signals.
*
* The item direction is the direction at which the mirror
* directly reflects back the laser.
*/
export function isValidMirror(fields: BoxFields): Bool {
// check for each direction
return (
DIR_ALL.map((d) => {
// is this item looking at this direction?
const isItemDir = fields.itemDir.equals(d);
const d_l = (d - 1 + 8) % 8;
const d_r = (d + 1) % 8;
// is the item valid for this direction?
// there are multiple steps to this, shown below.
// - the `in` and `out` at `d` should be equal
const isValidDirect = fields.ins[d].equals(fields.outs[d]);
// - the `in` at d-1 should equal `out` d+1
const isValidRightBounce = fields.ins[d_l].equals(fields.outs[d_r]);
// - the `in` at d+1 should equal `out` d-1
const isValidLeftBounce = fields.ins[d_r].equals(fields.outs[d_l]);
// - the other output signals should be 0
// const isValidOut = Array.from(
// { length: 5 }, // ignore other 3 directions: d-1, d, d+1
// (_, i) => fields.outs[(d + 2 + i) % 8].equals(false)
// )
// .reduce((acc, cur) => acc.or(cur))
// .equals(false);
const isValidOut = fields.outs[(d + 2) % 8]
.or(fields.outs[(d + 3) % 8])
.or(fields.outs[(d - 2 + 8) % 8])
.or(fields.outs[(d - 3 + 8) % 8])
.or(fields.outs[(d + 4) % 8])
.not();
// finally `and` them all
const isValid = isValidDirect
.and(isValidRightBounce)
.and(isValidLeftBounce)
.and(isValidOut);
return isItemDir.and(isValid);
})
// or-reduce to get at least 1 valid
.reduce((acc, cur) => acc.or(cur))
);
}