summaryrefslogtreecommitdiff
path: root/src/simple.eprime
diff options
context:
space:
mode:
Diffstat (limited to 'src/simple.eprime')
-rw-r--r--src/simple.eprime77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/simple.eprime b/src/simple.eprime
new file mode 100644
index 0000000..1a23810
--- /dev/null
+++ b/src/simple.eprime
@@ -0,0 +1,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]))