Solving Sudoku in Python Packaging

(github.com)

102 points | by Yenrabbit 2 days ago ago

19 comments

  • simonw 5 hours ago

    I love this so much. I dug around a bit and figured out how it works - I have an explanation (with an illustrative diagram) here: https://simonwillison.net/2024/Oct/21/sudoku-in-python-packa...

    Figuring out how it works is a great way to learn a bit more about how Python packaging works under the hood. I learned that .whl files contain a METADATA file listing dependency constraints as "Requires-Dist" rules.

    I ran a speed comparison too. Using the uv pip resolver it took 0.24s - with the older pip-compile tool it took 17s.

    • zahlman an hour ago

      People keep trying to sell the speed of such solutions as a killer feature for uv, but I think I must not be anywhere near the target audience. The constraint-solving required for the sorts of projects I would typically work on is not even remotely as complex, while I'm bottlenecked by a slow, unreliable Internet connection (and the lack of a good way to tell Pip not to check PyPI for new versions and only consider what's currently in the wheel cache).

    • seanw444 3 hours ago

      Wow, uv really is fast.

      • jebebeebehhe an hour ago

        As is simonw writing that post in under 60m assuming he first saw the concept here on HN.

  • visarga 3 hours ago

    That's why it feels like installing a ML repo is like sudoku. You install everything and at the last step you realize your neural net uses FlashAttention2 which only works on NVIDIA compute version that is not deployed in your cloud VM and you need to start over from scratch.

    • hskalin 22 minutes ago

      Sometimes I just change the version of the package in requirements to fit with others and pray that it works out (a few times it does)

    • nicman23 13 minutes ago

      honestly if the ml does not have a docker image - not compose no build an image- i do not even bother any more

  • echoangle 3 hours ago

    > Solving the versions of python package from your requirements is NP-complete, in the worst case it runs exponentially slow. Sudokus are also NP-complete, which means we can solve sudokus with python packaging.

    Is that actually sufficient? Can every system that’s solving something that’s NP-complete solve every other NP-complete problem?

  • chatmasta 3 hours ago

    Here’s the same thing in Poetry (2022): https://www.splitgraph.com/blog/poetry-dependency-resolver-s...

    • teschmitt an hour ago

      Was just about to say: I've seen this before but building it with a universally usable requirements.txt is even cooler.

  • yochem 2 days ago

    No way pip actually is a really inefficient SAT solver!

  • niyonx 2 days ago

    How did you even think of that? Nice!

  • ziofill 6 hours ago

    but how does it know the constraints?

    • thangngoc89 5 hours ago

      This is the content of sudoku_0_0-1-py3-none-any.whl. So when the (0,0) cell is 1, none of the cells in the same row, column and subgrid should be 1.

          Requires-Dist: sudoku_0_1 != 1
          Requires-Dist: sudoku_0_2 != 1
          Requires-Dist: sudoku_0_3 != 1
          Requires-Dist: sudoku_0_4 != 1
          Requires-Dist: sudoku_0_5 != 1
          Requires-Dist: sudoku_0_6 != 1
          Requires-Dist: sudoku_0_7 != 1
          Requires-Dist: sudoku_0_8 != 1
          Requires-Dist: sudoku_1_0 != 1
          Requires-Dist: sudoku_2_0 != 1
          Requires-Dist: sudoku_3_0 != 1
          Requires-Dist: sudoku_4_0 != 1
          Requires-Dist: sudoku_5_0 != 1
          Requires-Dist: sudoku_6_0 != 1
          Requires-Dist: sudoku_7_0 != 1
          Requires-Dist: sudoku_8_0 != 1
          Requires-Dist: sudoku_0_1 != 1
          Requires-Dist: sudoku_0_2 != 1
          Requires-Dist: sudoku_1_0 != 1
          Requires-Dist: sudoku_1_1 != 1
          Requires-Dist: sudoku_1_2 != 1
          Requires-Dist: sudoku_2_0 != 1
          Requires-Dist: sudoku_2_1 != 1
          Requires-Dist: sudoku_2_2 != 1
    • jsnell 6 hours ago

      The constraints are going to be static and independent of the puzzle. So I expect they're encoded in the package dependencies. So for example version 1 of the package sudoku_0_0 will conflict with all of: version 1 of sudoku_[0-8]_0; version 1 of sudoku_0_[0-8]; version 1 of [012]_ [012].

    • IshKebab 3 hours ago

      Yeah they missed out the actual interesting bit from the readme...