summaryrefslogtreecommitdiff
path: root/src/simple.eprime
blob: 1a23810fa34b4e006deb878485a255d91686bb32 (plain) (blame)
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
language ESSENCE' 1.0

letting LEFT = 2
letting RIGHT = 3
letting UP = 4
letting DOWN = 5
letting END = 0
letting directions be domain int(LEFT, RIGHT, UP, DOWN, END)

letting SPACE = 0
letting BLOCK = 1
letting map be domain int(SPACE, BLOCK)
given initial_grid : matrix indexed by [int(1..n), int(1..n)] of map

letting range be domain int(1..n)

letting block_count = sum(flatten(initial_grid))
letting last = n*n - block_count
letting trange be domain int(0..last)

find grid : matrix indexed by [range, range] of (map union directions)
find time : matrix indexed by [range, range] of trange

such that true
    $ every square is reached at a different time
    , alldifferent_except(flatten(time), 0)

    $ the end square will be the last
    , forAll i, j: range .
        (grid[i, j] = END) <-> (time[i, j] = last)

    $ exactly one square is the end point of the path
    $ this is already certified by the previous two constraints
    , gcc(flatten(grid), [ END ], [ 1 ])

    $ all blocks from initial_grid are in the same place in grid
    , forAll i, j : range .
        (initial_grid[i, j] = BLOCK) <-> (grid[i, j] = BLOCK)

    $ all blocks have time 0
    , forAll i, j: range .
        (grid[i, j] = BLOCK) <-> (time[i, j] = 0)

    $ every move moves into a non-block square
    , forAll i, j : range .
        ((grid[i, j] = LEFT ) -> (i != 1 /\ grid[i - 1, j] != BLOCK)) /\
        ((grid[i, j] = RIGHT) -> (i != n /\ grid[i + 1, j] != BLOCK)) /\
        ((grid[i, j] = UP   ) -> (j != 1 /\ grid[i, j - 1] != BLOCK)) /\
        ((grid[i, j] = DOWN ) -> (j != n /\ grid[i, j + 1] != BLOCK))

    $ every move moves into a square after it
    $ this could use a bi-directional relation if blocks didn't have time 0
    , forAll i, j : range .
        ((grid[i, j] = LEFT ) -> (time[i - 1, j] = time[i, j] + 1)) /\
        ((grid[i, j] = RIGHT) -> (time[i + 1, j] = time[i, j] + 1)) /\
        ((grid[i, j] = UP   ) -> (time[i, j - 1] = time[i, j] + 1)) /\
        ((grid[i, j] = DOWN ) -> (time[i, j + 1] = time[i, j] + 1))

    $ the move after the current move is in the same direction, unless there is
    $ a block or an older move in the way, or it's at the edge of the grid
    , forAll i, j : range .
        ((grid[i, j] = LEFT ) -> (i - 1 = 1 \/
            grid[i - 1, j] = grid[i, j] \/
            $ grid[i - 2, j] = BLOCK \/
            time[i - 2, j] < time[i - 1, j])) /\
        ((grid[i, j] = RIGHT) -> (i + 1 = n \/
            grid[i + 1, j] = grid[i, j] \/
            $ grid[i + 2, j] = BLOCK \/
            time[i + 2, j] < time[i + 1, j])) /\
        ((grid[i, j] = UP   ) -> (j - 1 = 1 \/
            grid[i, j - 1] = grid[i, j] \/
            $ grid[i, j - 2] = BLOCK \/
            time[i, j - 2] < time[i, j - 1])) /\
        ((grid[i, j] = DOWN ) -> (j + 1 = n \/
            grid[i, j + 1] = grid[i, j] \/
            $ grid[i, j + 2] = BLOCK \/
            time[i, j + 2] < time[i, j + 1]))