Assessment 2009 Hard Answers

From Progzoo
Jump to: navigation, search

Guardian Puzzles. For these problems you must read the puzzle definition from a file. In each case there are two files given, you should use the exact same routine for both examples.

Sudoko

Example of a sudoku
  8 
4   73  
  3 1 65 
   8   4 
 37   5 6
5    9 2
  1 957  
  51    
    46   

http://progzoo.net/sudoko1.txt

Drawing the grid lines

There are ten lines horizontally and vertically. Every third line should be thicker than the others. That is lines 0, 3, 6 and 9 should be thicker. The expression i%3 is 0 when i is divisible by three (that is 0, 3, 6 and 9). So the ternary operator (i%3!=0)?1:3 is either 1 (for a thin line) or 3 (for a thick line.

for (int i=0;i<10;i++){
  g.setStroke(new BasicStroke(i%3!=0)?1:3);
  g.drawLine(0,i*w/9,h,i*w/9);
  g.drawLine(i*w/9,0,i*w/9,h);
}

Drawing the numbers

There are nine lines of nine characters to be read.

for (int i=0;i<9;i++){
  String s = fh.readLine();
  for (int j=0;j<s.length();j++){
    g.drawString(s.substring(j,j+1),j*w/9+ox,(i+1)*w/9+oy);
  }
}

The outer loop (the i loop) reads one line at a time

The inner loop (the j loop) runs down each character in the string s


[Font] [Default] [Show] [Resize] [History] [Profile]

Killer

Each region has a number and a letter associated. The grid of letters shows the shape of each region. Region a has number 18, region b has 21 etc.

18 21 39 14 9 10 32 24 11 19 19 24 7 14 11 22 13 7 22 25 9 4 8 9 14
abbbccccc
abddeeecf
agdhhijjf
agdkhijjf
gggkhlmmf
nnokplqrs
nnokplqrs
tttuplvvs
wwtuxxxyy
Andrew's attempt at this
Example of a killer from the Guardian


[Font] [Default] [Show] [Resize] [History] [Profile]

Chess

Example of a chess problem

http://www.guardian.co.uk/sport/2009/jul/10/chess-ronan-bennett

r5k1/3P2pp/8/5N2/8/p4K2/5PP1/8 w

You can think of the string r5k1/3P2pp/8/5N2/8/p4K2/5PP1/8 as a list of instructions. Here is how to interpret the first few:

  • r draw a black rook and move right by one
  • 5 move right by 5
  • k draw a black king and move right by one
  • 1 move right one
  • / move to the start of the next row

You can use a simple state machine to process these. The state is represented simply by two variables,x and y the coordinates of the current square.

As you process each character you decide what to do next, you can use a case statement

You can create the array images. The fixed string pieces "rnbqkpRNBQKP" shows the position of each image.

So the index of b in the string pieces is 2 which means the image of a black bishop is in position 2 of the array images.



[Font] [Default] [Show] [Resize] [History] [Profile]

Crossword

Example of a crossword
00000000
01010101
00000000
01010101
00000000
01010101
11111000

The crossword always has rotational symmetry order 4. The input file indicates 1 for black squares in the upper left quarter of the crossword puzzle.

http://www.guardian.co.uk/crossword/print/0,,-24670,00.html?answers=156

[Font] [Default] [Show] [Resize] [History] [Profile]

Kakuro

Example of a kakuro
00010011001
00000010000
11000000100
10010001001
00001100000
00000100100
11000100011
00100100000
00000110000
10010001001
00100000011
00001000000
10011001000
6 3 3 29 30 17 39 
6 14 12 14 13 16
34 31 30
17 36 16 14
9 3 21 23 17 17 8
29 27 35
22 4 42 9
14 36 9 6 20 4
17 17 16 15
35 11 10 26
16 17 13 6 4 16
13 9 23 7 4
28 21
4 8 7


[Font] [Default] [Show] [Resize] [History] [Profile]