Skip to content

Instantly share code, notes, and snippets.

View Abhi-ctrl-cmd's full-sized avatar

Abhi-ctrl-cmd

View GitHub Profile
@Abhi-ctrl-cmd
Abhi-ctrl-cmd / sudoku.hs
Last active February 18, 2021 17:04
Haskell code to solve sudokus using list monad for brute force nondeterminism
type Row = [Int]
type Col = [Int]
type Board = [Row]
addDigit :: Row -> Int -> Int -> Board -> [Row] -- takes a row r, its index m, the index of a cell in it m, and the board; returns list of possble rows with that cell filled
addDigit r m n b = let pos = r !! n
c = getCol n b
s = concat (getSquare m n b)
fill = intersection (left c) (left r) (left s) -- list of numbers that could be in cell (m,n)
in if (pos > 0) then [r] -- cell already filled
In each iteration, the program checks if the current instruction is +/,/-/./</>/[/] (in that order). If it obtains a match, it executes the instruction, but does not restart the loop; eg if there is a "++" sequence, it executes the first one but compares the second one to a , then - etc, and cycles back to plus.
The tape is configured as follows:
1 = instruction counter (no of instructions executed in current iteration)*
2 & 3 = program counter PC
4 & 5 = tape counter TC
6 = bracket counter (for nested brackets) BC
7 = buffer (always 0 except for copying purposes)
8 = beginning of input program T1 (always 255 except for copying purposes)
9 etc = input program; each instruction takes 4 cells (x; x; 255; 255)