Skip to content

Instantly share code, notes, and snippets.

@chamik
Created May 1, 2023 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chamik/7f851efb5e740ca7793c0d3eeaf0d6b0 to your computer and use it in GitHub Desktop.
Save chamik/7f851efb5e740ca7793c0d3eeaf0d6b0 to your computer and use it in GitHub Desktop.
# Written by @mvolfik (helped by @chamik, gotta take some cred) for
# solving the 9th puzzle of Civilizace 2023
n = [[15, 8, 12, 7, 10, 11, 10, 11],
[ 4, 2, 6, 5, 9, 8, 17, 24],
[ 13, 11, 11, 7, 1, 0, 8, 22],
[ 23, 18, 15, 14, 7, 4, 0, 9],
[ 19, 13, 20, 18, 7, 4, 0, 0],
[ 16, 19, 18, 11, 11, 6, 2, 6],
[ 8, 24, 17, 9, 15, 12, 8, 6],
[ 6, 9, 9, 4, 4, 6, 13, 7]]
from pulp import *
p = LpProblem()
v = [[LpVariable(name=f"{i}_{j}", cat=LpInteger, lowBound=0, upBound=9) for j in range(9)] for i in range(9)]
for i in range(8):
for j in range(8):
p += v[i][j] + v[i][j+1] + v[i+1][j] + v[i+1][j+1] == n[i][j]
# Run with -i to view the result interactively
p.solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment