diff options
| author | Tom Harley | 2020-01-23 13:03:25 +0000 |
|---|---|---|
| committer | Tom Harley | 2020-01-23 13:03:25 +0000 |
| commit | f3d375722130e448802c046af2d75f66a06de498 (patch) | |
| tree | 0c82bd620be0401c6b9ba69c3314dfe0d9522eae /src | |
| download | machinarium-master.tar.gz machinarium-master.zip | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/grid-time.eprime | 97 | ||||
| -rw-r--r-- | src/order-moves.eprime.m4 | 76 | ||||
| -rw-r--r-- | src/simple.eprime | 77 | ||||
| -rw-r--r-- | src/superfluous.eprime | 140 |
4 files changed, 390 insertions, 0 deletions
diff --git a/src/grid-time.eprime b/src/grid-time.eprime new file mode 100644 index 0000000..251ed32 --- /dev/null +++ b/src/grid-time.eprime @@ -0,0 +1,97 @@ +language ESSENCE' 1.0 + +given initial_grid : matrix indexed by [int(1..n), int(1..n)] of int(0, 1) + +letting range be domain int(1..n) + +letting LEFT = 1 +letting RIGHT = 2 +letting UP = 3 +letting DOWN = 4 +letting BLOCK = 5 +letting END = 6 +letting SPACE = 0 + +letting map be domain int(SPACE, BLOCK) +letting directions be domain int(LEFT, RIGHT, UP, DOWN, END) +letting map_directions be domain map union directions + +letting dir_move be [-1, 1, -n, n] + +letting block_count = sum(flatten(initial_grid)) + +letting time_length be (n*n - block_count) +letting time_domain be int(1..time_length) + +letting grid_length be (n*n) +letting grid_domain be int(1..grid_length) + +find grid : matrix indexed by [grid_domain] of map_directions +find time : matrix indexed by [time_domain] of grid_domain +find time_ : matrix indexed by [int(1..(time_length-1))] of grid_domain +find full_grid : matrix indexed by [range, range] of map_directions + +such that + +$ redraw grid as full_grid +forAll x, y: range . + full_grid[x, y] = grid[(x-1)*n + y], + +forAll x: int(1..(time_length-1)) . + time_[x] = (time[x] + dir_move[grid[time[x]]]), + +$ blocks are in the same place +forAll x, y: range . + (initial_grid[x, y] = 1) <-> (grid[(y-1)*n + x] = BLOCK), + +$ there are no spaces in the answer +$ there are the same number of blocks in both grids +$ there is one end square +gcc(grid, [ SPACE, BLOCK, END ], [ 0, block_count, 1 ]), + +$ no square is visited twice +allDiff(time), + +$ no square visited is a block +forAll i: time_domain . + grid[time[i]] != BLOCK, + +$ no left-side squares can move left +forAll y: range . + grid[(y-1)*n + 1] != LEFT, + +$ no right-side squares can move right +forAll y: range . + grid[y*n] != RIGHT, + +$ no top-side squares can move up +forAll x: range . + grid[x] != UP, + +$ no bottom-side squares can move down +forAll x: range . + grid[n*(n-1) + x] != DOWN, + +$ squares point to the next square in time +forAll x: int(1..(time_length-1)) . + time[x] + dir_move[grid[time[x]]] = time[x + 1], + +$ squares cannot point "backwards" +forAll x: int(1..(time_length-1)) . + time[x] + time[x+1] != 0, + +$ squares cannot point into blocks +forAll x: int(1..(time_length-1)) . + grid[time_[x]] != BLOCK, + +$ the big boy +forAll x: int(1..(time_length-2)) . ( + (grid[time[x]] = grid[time_[x]]) \/ + (grid[time[x] + grid[time[x]] + grid[time[x]]] = BLOCK) \/ + (exists y: int(1..(x-1)) . y = (time[x] + grid[time[x]] + grid[time[x]])) \/ + ((grid[time[x]] = LEFT) /\ ((time[x] % n) = 1)) \/ + ((grid[time[x]] = RIGHT) /\ ((time[x] % n) = (n - 1))) \/ + ((grid[time[x]] = UP) /\ ((time[x]) <= (n + n))) \/ + ((grid[time[x]] = DOWN) /\ ((time[x]) >= (n * n - n - n))) ), + +true diff --git a/src/order-moves.eprime.m4 b/src/order-moves.eprime.m4 new file mode 100644 index 0000000..3bdc65e --- /dev/null +++ b/src/order-moves.eprime.m4 @@ -0,0 +1,76 @@ +language ESSENCE' 1.0 + +given initial_grid : matrix indexed by [int(1..n),int(1..n)] of int(0,1) + +letting fig = flatten(initial_grid) +letting spaces = (n * n) - sum(fig) + +find order : matrix indexed by [int(1..spaces)] of int(1..n*n) +find moves : matrix indexed by [int(1..spaces-1)] of int(-1,1,-n,n) + +ifdef(`optimise_moves', `dnl +find chdir : matrix indexed by [int(2..spaces-1)] of int(0,1) + +minimising sum(chdir) +')dnl + +such that + +ifdef(`optimise_moves', `dnl +forAll i: int(2..spaces-1) . + chdir[i] = 1 <-> moves[i-1] != moves[i], +')dnl + +allDiff(order), + +ifdef(`fill_all', `dnl +forAll i: int(1..n*n) . + fig[i] = 0 <-> exists j: int(1..spaces) . order[j] = i, +', `dnl +forAll i: int(1..spaces) . + fig[order[i]] != 1, +')dnl + +ifdef(`no_block_moves', `dnl +forAll i: int(1..spaces-1) . + fig[order[i] + moves[i]] != 1, +')dnl + +ifdef(`no_repeat_moves', `dnl +forAll i: int(2..spaces-1) . + forAll j: int(1..i-1) . + order[i] + moves[i] != order[j], +')dnl + +forAll i: int(1..spaces-1) . + order[i] + moves[i] = order[i+1], + +forAll i: int(1..spaces-1) . +ifdef(`up_down_bounds', `dnl + ((order[i] <= n) -> (moves[i] != -n)) /\ + ((order[i] > n * n - n) -> (moves[i] != n)) /\ +')dnl + ((order[i] % n = 1) -> (moves[i] != -1)) /\ + ((order[i] % n = 0) -> (moves[i] != 1)) /\ +true, + +ifdef(`opposite_move_check', `dnl +forAll i: int(2..spaces-1) . + moves[i] != -moves[i-1], + +')dnl +forAll i: int(2..spaces-1) . + (moves[i] = moves[i-1]) \/ + (fig[order[i] + moves[i-1]] = 1) \/ + (exists j: int(1..i-1) . order[i] + moves[i-1] = order[j]) \/ + (moves[i-1] = -n /\ order[i] <= n) \/ +ifdef(`throw_exception', `dnl + (moves[i-1] = n /\ order[i] > n * n - 1) \/ +', `dnl + (moves[i-1] = n /\ order[i] > n * n - n) \/ +')dnl + (moves[i-1] = -1 /\ order[i] % n = 1) \/ + (moves[i-1] = 1 /\ order[i] % n = 0) \/ +false, + +true 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])) diff --git a/src/superfluous.eprime b/src/superfluous.eprime new file mode 100644 index 0000000..4f1ad2a --- /dev/null +++ b/src/superfluous.eprime @@ -0,0 +1,140 @@ +language ESSENCE' 1.0 + +letting SPACE = 0 +letting BLOCK = 1 +letting initial_grid_domain be int(SPACE, BLOCK) + +given initial_grid : + matrix indexed by [int(1..n), int(1..n)] of initial_grid_domain + +letting block_count = sum(flatten(initial_grid)) +letting total_moves = n * n - block_count +letting moves_domain be domain int(1..total_moves) +letting grid_side be domain int(1..n) + +letting LEFT = 2 +letting RIGHT = 3 +letting UP = 4 +letting DOWN = 5 +letting dirs be domain int(LEFT, RIGHT, UP, DOWN) + +letting dir_x = [ -1, 1, 0, 0 ; dirs] +letting dir_y = [ 0, 0, -1, 1 ; dirs] +letting opp_dir = [ RIGHT, LEFT, DOWN, UP ; dirs ] + +letting END = 6 +letting solution_domain be domain initial_grid_domain union dirs union int(END) + +find solution : + matrix indexed by [moves_domain, grid_side, grid_side] of solution_domain +find final_grid : matrix indexed by [grid_side, grid_side] of solution_domain +find moved_to : + matrix indexed by [int(2..total_moves), grid_side, grid_side] + of solution_domain + +such that + +forAll t: int(2..total_moves) . + forAll x, y: grid_side . + ( (solution[t, x, y] = LEFT) + \/ (solution[t, x, y] = RIGHT) + \/ (solution[t, x, y] = UP) + \/ (solution[t, x, y] = DOWN) ) -> + (moved_to[t, x, y] = solution[t, x + dir_x[solution[t, x, y]], y + dir_y[solution[t, x, y]]]), + +$ all steps have a move moving into the end (except at the start) +forAll t: int(2..total_moves) . + exists x, y: grid_side . + moved_to[t, x, y] = END, + +$ final_grid is just the last step in the solution +forAll x, y: grid_side . + solution[total_moves, x, y] = final_grid[x, y], + +$ initial_grid is the same as the first step in the solution, except for end +forAll x, y: grid_side . + (solution[1, x, y] = END) \/ (solution[1, x, y] = initial_grid[x, y]), + +$ blocks are in the same place for all iterations of the grid +forAll t: moves_domain . + forAll x, y: grid_side . + (initial_grid[x, y] = BLOCK) <-> (solution[t, x, y] = BLOCK), + +$ there is an end spot in every step +forAll t: moves_domain . + gcc(flatten(solution[t, .., ..]), [ END ], [ 1 ]), + +$ the final iteration of the solution has no spaces +forAll x, y: grid_side . + final_grid[x, y] != SPACE, + +$ moves are preserved across steps +forAll t: int(2..total_moves) . + forAll x, y: grid_side . + ( (solution[t-1, x, y] = LEFT) + \/ (solution[t-1, x, y] = RIGHT) + \/ (solution[t-1, x, y] = UP) + \/ (solution[t-1, x, y] = DOWN) ) -> + (solution[t, x, y] = solution[t-1, x, y]), + +$ you gotta move +forAll t: int(2..total_moves) . + forAll x, y: grid_side . + (solution[t-1, x, y] = END) -> + ( (solution[t, x, y] = LEFT) + \/ (solution[t, x, y] = RIGHT) + \/ (solution[t, x, y] = UP) + \/ (solution[t, x, y] = DOWN) + \/ (solution[t, x, y] = END) ), + +$ all squares that change change to the same thing (except the end) +forAll t: int(2..total_moves) . + forAll x, y: grid_side . + (solution[t-1, x, y] != solution[t, x, y]) -> + (forAll x_, y_: grid_side . + (solution[t, x, y] = END) \/ + (solution[t, x_, y_] = END) \/ + (solution[t, x_, y_] = solution[t, x, y]) \/ + (solution[t, x_, y_] = solution[t-1, x_, y_]) ), + +$ spaces are preserved +forAll t: int(2..total_moves) . + forAll x, y: grid_side . + (solution[t-1, x, y] = SPACE) -> + ((solution[t, x, y] = SPACE) \/ (solution[t, x, y] = END)), + +$ moves cannot go "off the edge" +forAll t: moves_domain . + forAll x, y: grid_side . + ( (solution[t, x, y] = LEFT) + \/ (solution[t, x, y] = RIGHT) + \/ (solution[t, x, y] = UP) + \/ (solution[t, x, y] = DOWN) ) -> + ( ((x + dir_x[solution[t, x, y]]) >= 1) + /\ ((x + dir_x[solution[t, x, y]]) <= n) + /\ ((y + dir_y[solution[t, x, y]]) >= 1) + /\ ((y + dir_y[solution[t, x, y]]) <= n) ), + +$ moves cannot move into space +$ moves cannot move into blocks +forAll t: int(2..total_moves) . + forAll x, y: grid_side . + ( (solution[t, x, y] = LEFT) + \/ (solution[t, x, y] = RIGHT) + \/ (solution[t, x, y] = UP) + \/ (solution[t, x, y] = DOWN) ) -> + ( (solution[t, x + dir_x[solution[t, x, y]], + y + dir_y[solution[t, x, y]]] != SPACE) + /\ (solution[t, x + dir_x[solution[t, x, y]], + y + dir_y[solution[t, x, y]]] != BLOCK) + /\ (solution[t, x + dir_x[solution[t, x, y]], + y + dir_y[solution[t, x, y]]] != opp_dir[solution[t, x, y]])), + +$ either the x or y coordinate of the end block stays the same +forAll t: int(2..total_moves) . + exists x, y: grid_side . + (solution[t-1, x, y] = END) /\ + ((exists x_: grid_side . solution[t, x_, y] = END) \/ + (exists y_: grid_side . solution[t, x, y_] = END)), + +true |
