Fork me on GitHub
#off-topic
<
2021-10-09
>
kulminaator18:10:12

whilst solving a sudoku in the kitchen .... it crossed my mind - how would you define a sudoku dataset in clojure

kulminaator18:10:44

just a long vector ? vector of vectors (by rows or columns) or each cell as a small vector and the whole lot as 9 vectors ?

kulminaator18:10:56

or something completely different?

p-himik18:10:31

Vector of vectors.

p-himik18:10:46

Or array or arrays if you care about performance/memory.

dgb2318:10:06

vector of vectors is the straight forward general representation. But it depends on what you do with it!

p-himik18:10:12

Oh, hold on - Java supports 2D arrays, doesn't it? Keep forgetting. Then those ones, instead of an array of arrays.

dgb2318:10:43

I haven’t looked into sudoku, but depending on how you want to operate on that data structure you may want to use a different one

kulminaator18:10:54

well you probably want to check the current state for validity 😄

kulminaator18:10:09

all rows all columns all cells must be made of unique numbers

dgb2318:10:24

yeah but with those mathematical things there are often clever algorithms that you can exploit

dgb2318:10:39

just an instict! i actually have no clue, never looked into it

kulminaator18:10:11

well sudoku doesn't have much to do with maths, you don't compute anything really from the numbers .... they are just recognizable symbols

dgb2318:10:30

> all rows all columns all cells must be made of unique numbers

dgb2318:10:33

then its a set

dgb2318:10:04

ah I thought you might think of generating or solving them or something

kulminaator18:10:18

i was thinking of generating yes

kulminaator18:10:37

but the process of generation is pretty much (throw some numbers in there and see if it could be solved)

dgb2318:10:43

well at some point you represent them as vectors of vectors or vectors of sets

dgb2318:10:09

that sounds awful

kulminaator18:10:17

right now i did it as vectors of vectors .... and helper functions to check validity based on that

dgb2318:10:18

can’t you kind of backtrack from a solution?

kulminaator18:10:34

technically 1 function of validation is sufficient - distinct?

kulminaator18:10:46

it's just what i cherry pick as it's input

dgb2318:10:49

well you dont need distinct if you have sets

dgb2318:10:08

because that is an inherent property

kulminaator18:10:10

i need to check data from a row, data from a column or data from a cell

dgb2318:10:30

right, then its vectors 😄

kulminaator18:10:42

if i start to combine the input into a set i would lose the visibility where the invalidity came from 🙂

dgb2318:10:53

right right

kulminaator18:10:06

but yeah, it's interesting to see what people come up with ... 🙂

dgb2318:10:18

maybe the cells should be fat even

kulminaator18:10:53

a long time ago i had to write game engines ... and it turned out that chess may be complicated for humans but checkers was just about as annoying to code, if not more 🙂

dgb2318:10:56

you almost nerd sniped me there, got stuff to do 😄

kulminaator18:10:12

because of the multiple hops and the enforcement of "you have to take it if you can"

p-himik18:10:24

If that's helpful, Peter Norvig wrote a nice lengthy article on Sudoku solver, and other people wrote implementations in other languages, including Clojure: https://norvig.com/sudoku.html

dgb2318:10:31

dammit…

kulminaator18:10:52

now the sabotage if dgb day is complete

schmee19:10:06

my sudoku solver uses a map of cell to number for the board, i.e {[:a 1] 5 [:a 2] 0 ...}

☝️ 1
schmee19:10:40

I went through a bunch of different structures and this was the one that ended up matching the algorithm the best

djblue19:10:05

I used a sparse matrix representation {{:row x :column y} value}

Ben Sless05:10:08

Since you have logical programming in Clojure, using core.logic is a very interesting approach https://nextjournal.com/helios/solving-sudokus-using-core-logic

mauricio.szabo00:10:52

Or Spock: https://gitlab.com/mauricioszabo/Spock :rolling_on_the_floor_laughing: