VictorNicollet / 99-Problems-OCaml

Solving "99 List Problems" using Objective Caml
http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html
Other
84 stars 14 forks source link

add p94, the arithemetic_puzzle #2

Closed MassD closed 10 years ago

MassD commented 10 years ago

Solved problem 94:

An arithmetic puzzle. (hard)

Given a list of integer numbers, find a correct way of inserting arithmetic signs (operators) such that the result is a correct equation.

Example: With the list of numbers [2;3;5;7;11] we can form the equations 2-3+5+7 = 11 or 2 = (3*5+7)/11 (and ten others!).

MassD commented 10 years ago

Solved problem 97:

Sudoku. (medium)

Sudoku puzzles go like this:

Problem statement Solution

.  .  4 | 8  .  . | .  1  7      9  3  4 | 8  2  5 | 6  1  7
        |         |                      |         |
6  7  . | 9  .  . | .  .  .      6  7  2 | 9  1  4 | 8  5  3
        |         |                      |         |
5  .  8 | .  3  . | .  .  4      5  1  8 | 6  3  7 | 9  2  4
--------+---------+--------      --------+---------+--------
3  .  . | 7  4  . | 1  .  .      3  2  5 | 7  4  8 | 1  6  9
        |         |                      |         |
.  6  9 | .  .  . | 7  8  .      4  6  9 | 1  5  3 | 7  8  2
        |         |                      |         |
.  .  1 | .  6  9 | .  .  5      7  8  1 | 2  6  9 | 4  3  5
--------+---------+--------      --------+---------+--------
1  .  . | .  8  . | 3  .  6      1  9  7 | 5  8  2 | 3  4  6
        |         |                      |         |
.  .  . | .  .  6 | .  9  1      8  5  3 | 4  7  6 | 2  9  1
        |         |                      |         |
2  4  . | .  .  1 | 5  .  .      2  4  6 | 3  9  1 | 5  7  8

Every spot in the puzzle belongs to a (horizontal) row and a (vertical) column, as well as to one single 3x3 square (which we call "square" for short). At the beginning, some of the spots carry a single-digit number between 1 and 9. The problem is to fill the missing spots with digits in such a way that every number between 1 and 9 appears exactly once in each row, in each column, and in each square.

VictorNicollet commented 10 years ago

Thanks for the contribution ! The Sudoku version looks a bit long, but it's better than the one I don't have right now :smile:

MassD commented 10 years ago

Thanks. Yeah, I use array to solve it. It is not pure functional, but gives not bad efficiency. But I keep thinking of a pure functional way.

VictorNicollet commented 10 years ago

I have committed a shorter (and faster) version. The trick is to update the sudoku board in-place, and fill the cells with zero when backtracking.