aboutsummaryrefslogtreecommitdiffstats
path: root/c/sudoku.c
blob: 040ba8d24837d43fb719e9830f81bb51f5ce2e62 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>


char* solve(char puzzle[])
{

  /* search for an unanswered square */
  int i = 0;
  while(puzzle[i] != '\0') {
    if(puzzle[i] - '0' != 0) {
      goto next; // this square is answered
    }

    // found an unanswered square
    //printf("%d", puzzle[i] - '0');

  next:
    i++;
  }

  return puzzle; // we solved it!
}


void display(char puzzle[])
{
  printf("\n  ");

  int i = 0;
  while(puzzle[i] != '\0') {
    printf(" %d", puzzle[i] - '0');

    if(i % 9 == 8)
      printf("\n  ");
    i++;
  }

  printf("\n");
}


int main(int argc, char *argv[])
{
  for(int i=1; i < argc; i++) {
    char* puzzle = solve(argv[i]);

    display(puzzle);
  }
  return 0;
}