Format and link fix 2011/hamaji/README.md
This commit is contained in:
1 parent
b2602712a6
commit
0497f58748
1 file changed
+69
-52
+69
-52
@@ -1,7 +1,7 @@
|
|||||||
# Best solved puzzle
|
# Best solved puzzle
|
||||||
|
|
||||||
Shinichiro Hamaji
|
Shinichiro Hamaji
|
||||||
<shinichiro.hamaji@gmail.com>
|
<shinichiro.hamaji@gmail.com>
|
||||||
|
|
||||||
## To build:
|
## To build:
|
||||||
|
|
||||||
@@ -30,29 +30,29 @@ make
|
|||||||
## Judges' remarks:
|
## Judges' remarks:
|
||||||
|
|
||||||
This program solves "Nonogram", which also known as "Paint by Numbers":
|
This program solves "Nonogram", which also known as "Paint by Numbers":
|
||||||
|
<http://en.wikipedia.org/wiki/Nonogram>.
|
||||||
<http://en.wikipedia.org/wiki/Nonogram>
|
|
||||||
|
|
||||||
This entry roar as the Year of the Dragon: A year considered
|
This entry roar as the Year of the Dragon: A year considered
|
||||||
some to be good luck. Well it was certainly true that the
|
some to be good luck. Well it was certainly true that the
|
||||||
IOCCC was lucky to get such a fine submission!
|
IOCCC was lucky to get such a fine submission!
|
||||||
|
|
||||||
The source code is a solution to a Paint by Numbers puzzle when you
|
The source code is a solution to a Paint by Numbers puzzle when you
|
||||||
paint by C. Try your hand at solving a few of the other .nono
|
paint by C. Try your hand at solving a few of the other `.nono`
|
||||||
files and then compare your solution with this program!
|
files and then compare your solution with this program!
|
||||||
|
|
||||||
NOTE: NONOGRAM (r) is a registered Trademark and is used with permission.
|
NOTE: NONOGRAM (r) is a registered Trademark and is used with permission. See
|
||||||
See http://nonogram.com/ for more information.
|
<https://web.archive.org/web/20110128130704/http://nonogram.com/> for more
|
||||||
|
information.
|
||||||
|
|
||||||
## Author's remarks:
|
## Author's remarks:
|
||||||
|
|
||||||
The format of the input files are as same as this site:
|
The format of the input files are as same as this site:
|
||||||
|
<https://web.archive.org/web/20130218055139/http://codegolf.com/paint-by-numbers>.
|
||||||
|
|
||||||
<http://codegolf.com/paint-by-numbers>
|
For example, for a simple problem which shows a glider of [Conway's
|
||||||
|
Game of Life](http://en.wikipedia.org/wiki/Conway's_Game_of_Life):
|
||||||
For example, for a simple problem which shows a glider of [conway's
|
|
||||||
lifegame](http://en.wikipedia.org/wiki/Conway's_Game_of_Life).
|
|
||||||
|
|
||||||
|
```
|
||||||
1
|
1
|
||||||
.
|
.
|
||||||
112
|
112
|
||||||
@@ -61,8 +61,11 @@ lifegame](http://en.wikipedia.org/wiki/Conway's_Game_of_Life).
|
|||||||
1 X
|
1 X
|
||||||
3 XXX
|
3 XXX
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
the input should be
|
the input should be
|
||||||
|
|
||||||
|
```
|
||||||
1
|
1
|
||||||
1
|
1
|
||||||
3
|
3
|
||||||
@@ -70,63 +73,76 @@ the input should be
|
|||||||
1
|
1
|
||||||
1.1
|
1.1
|
||||||
2
|
2
|
||||||
|
```
|
||||||
|
|
||||||
The width and the height of an input puzzle should be less than or
|
The width and the height of an input puzzle should be less than or
|
||||||
equal to 60.
|
equal to 60.
|
||||||
|
|
||||||
When this solver finds the problem has no solution, it prints "invalid".
|
When this solver finds the problem has no solution, it prints "`invalid`".
|
||||||
This means the problem is indeed broken. For example, this happens for
|
This means the problem is indeed broken. For example, this happens for
|
||||||
an input like
|
an input like
|
||||||
|
|
||||||
|
```
|
||||||
2
|
2
|
||||||
-
|
-
|
||||||
2
|
2
|
||||||
|
```
|
||||||
|
|
||||||
When this solver cannot find a solution, it prints "failed". This
|
When this solver cannot find a solution, it prints "`failed`". This
|
||||||
failure can happen when the problem has more than one solution or the
|
failure can happen when the problem has more than one solution or the
|
||||||
solver cannot solve the input problem. Here is an example input of the
|
solver cannot solve the input problem. Here is an example input of the
|
||||||
former:
|
former:
|
||||||
|
|
||||||
|
```
|
||||||
1
|
1
|
||||||
1
|
1
|
||||||
-
|
-
|
||||||
1
|
1
|
||||||
1
|
1
|
||||||
|
```
|
||||||
|
|
||||||
There are two solutions:
|
There are two solutions:
|
||||||
|
|
||||||
|
```
|
||||||
11
|
11
|
||||||
|
|
||||||
1 X
|
1 X
|
||||||
1 X
|
1 X
|
||||||
|
```
|
||||||
|
|
||||||
and
|
and
|
||||||
|
|
||||||
|
```
|
||||||
11
|
11
|
||||||
|
|
||||||
1 X
|
1 X
|
||||||
1 X
|
1 X
|
||||||
|
```
|
||||||
|
|
||||||
### Detail
|
### Detail
|
||||||
|
|
||||||
Solving Nonogram is known NP-complete, so we cannot make a solver
|
Solving Nonogram is
|
||||||
which solves all puzzles. However, we can write a solver which solves
|
[NP-complete](https://en.wikipedia.org/wiki/NP-completeness), so we cannot make
|
||||||
most problems solvable by humans. There are a bunch of ways to write
|
a solver which solves all puzzles. However, we can write a solver which solves
|
||||||
such programs. The two most obvious approaches are:
|
most problems solvable by humans. There are a bunch of ways to write such
|
||||||
|
programs. The two most obvious approaches are:
|
||||||
|
|
||||||
1. Check all possible solutions with backtracking.
|
1. Check all possible solutions with backtracking.
|
||||||
2. Implement all techniques which humans use.
|
2. Implement all techniques which humans use.
|
||||||
|
|
||||||
The former, brute-force approach is easy to implement. However, it
|
The former, brute-force approach is easy to implement. However, it
|
||||||
works only for smaller problems (width and height should be at most 30
|
works only for smaller problems (width and height should be at most 30
|
||||||
or so). Similarly, using generic probabilistic metaheuristics such as
|
or so). Similarly, using generic probabilistic
|
||||||
simulated annealing doesn't work for big problems.
|
[metaheuristics](https://en.wikipedia.org/wiki/Metaheuristic) such as
|
||||||
|
[simulated annealing](https://en.wikipedia.org/wiki/Simulated_annealing) doesn't
|
||||||
|
work for big problems.
|
||||||
|
|
||||||
<http://ccl.northwestern.edu/netlogo/models/community/Nonogram>
|
See: <http://ccl.northwestern.edu/netlogo/models/community/Nonogram>.
|
||||||
|
|
||||||
By its definition, the latter works for all human-solvable
|
By its definition, the latter works for all human-solvable
|
||||||
problems. The issue of the latter approach is this kind of programs
|
problems. The issue of the latter approach is this kind of programs
|
||||||
tends to be lengthy. This approach still requires some recursions. The
|
tends to be lengthy. This approach still requires some
|
||||||
|
[recursions](https://en.wikipedia.org/wiki/Recursion). The
|
||||||
most difficult problems which are considered human-solvable may
|
most difficult problems which are considered human-solvable may
|
||||||
require some "guesses". For this kind of problems, we need to assume a
|
require some "guesses". For this kind of problems, we need to assume a
|
||||||
cell is a space or a box, go ahead some steps with simple methods,
|
cell is a space or a box, go ahead some steps with simple methods,
|
||||||
@@ -134,19 +150,19 @@ mark the tried cell as a box (or a space if we assumed the tried cell
|
|||||||
is a box) if a contradiction is found.
|
is a box) if a contradiction is found.
|
||||||
|
|
||||||
My program uses an approach like "Set based solver" introduced in
|
My program uses an approach like "Set based solver" introduced in
|
||||||
[http://www.haskell.org/haskellwiki/Nonogram](http://www.haskell.org/haskellwiki/Nonogram) .
|
<https://wiki.haskell.org/Nonogram#Set_based_solver>. This should be equivalent
|
||||||
This should be equivalent to the combinations of simple methods (no
|
to the combinations of simple methods (no advanced reasoning with guess). My
|
||||||
advanced reasoning with guess). My solver also runs non-recursive
|
solver also runs a non-recursive guess for each undetermined cells. So, my
|
||||||
guess for each undetermined cells. So, my program should be able to
|
program should be able to solve most human-solvable problems (assuming humans
|
||||||
solve most human-solvable problems (assuming humans cannot execute
|
cannot execute deeper recursions).
|
||||||
deeper recursions).
|
|
||||||
|
|
||||||
### Obfuscation
|
### Obfuscation
|
||||||
|
|
||||||
First of all, as you see, my program looks like a nonogram puzzle
|
First of all, as you see, my program looks like a nonogram puzzle which results
|
||||||
which results a dragon. This puzzle is "dragon.nono" in this
|
in a [dragon](https://www.draconika.com/what). This puzzle is
|
||||||
directory. Note that whitespaces in character literals, a string
|
[dragon.nono](dragon.nono) in this directory. Note that whitespaces in character
|
||||||
literal, and around the last comment are considered as a boxed cell.
|
literals, a string literal, and around the last comment are considered as a
|
||||||
|
boxed cell.
|
||||||
|
|
||||||
My program is based on bit operations for speed. Many values are
|
My program is based on bit operations for speed. Many values are
|
||||||
stored using negative logic (0 means on and 1 means off) to utilize
|
stored using negative logic (0 means on and 1 means off) to utilize
|
||||||
@@ -162,22 +178,22 @@ with the well obfuscated code.
|
|||||||
|
|
||||||
### Portability
|
### Portability
|
||||||
|
|
||||||
This program uses two C99 features. One line comments and "long long".
|
This program uses two C99 features. One line comments and `long long`.
|
||||||
GCC's "-ansi -pedantic" check should pass:
|
GCC's `-ansi -pedantic` check should pass:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cc -ansi -pedantic -std=c99 hamaji.c
|
cc -ansi -pedantic -std=c99 hamaji.c
|
||||||
```
|
```
|
||||||
|
|
||||||
I checked my program with gcc-4.6.2 on linux, llvm-gcc-4.2 on mac, and
|
I checked my program with gcc-4.6.2 on linux, llvm-gcc-4.2 on Mac, and
|
||||||
clang-3.0 on mac.
|
clang-3.0 on Mac.
|
||||||
|
|
||||||
Note that the behavior of scanf differs on linux and mac, but this
|
Note that the behavior of `scanf()` differs on linux and mac, but this
|
||||||
program supports both semantics.
|
program supports both semantics.
|
||||||
|
|
||||||
This program should not depend on sizeof(int), sizeof(void*), ASCII,
|
This program should not depend on `sizeof(int)`, `sizeof(void*)`, ASCII,
|
||||||
memory layout, undefined evaluation order (e.g., a[i++]=i), etc.
|
memory layout, undefined evaluation order (e.g., `a[i++]=i`), etc.
|
||||||
This program won't work if the size of long long literal is less than 8.
|
This program won't work if the size of `long long` literal is less than 8.
|
||||||
By grepping the source code of gcc
|
By grepping the source code of gcc
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -186,25 +202,26 @@ grep LONG_LONG_TYPE_SIZE gcc/config/*/*
|
|||||||
|
|
||||||
I found long long may be 32bit integers only on AVR.
|
I found long long may be 32bit integers only on AVR.
|
||||||
|
|
||||||
### A few more
|
### A few more things
|
||||||
|
|
||||||
This code is considered as a new year's postcard in Japan. We chose
|
This code is considered as a [new year's postcard in
|
||||||
the dragon because 2012 is the year of dragon.
|
Japan](http://en.wikipedia.org/wiki/Japanese_New_Year#Postcards). We chose
|
||||||
|
the dragon because 2012 is the [year of
|
||||||
<http://en.wikipedia.org/wiki/Japanese_New_Year#Postcards>
|
dragon](https://japanesezodiac.com/japanese-year-of-the-dragon).
|
||||||
|
|
||||||
The second author wrote the picture, and the first author did
|
The second author wrote the picture, and the first author did
|
||||||
everything else.
|
everything else.
|
||||||
|
|
||||||
The dragon.nono, samurai.nono, and penguin.nono was written by the
|
The [dragon.nono](dragon.nono), [samurai.nono](samurai.nono), and
|
||||||
second author. The soccer.nono was from a wikipedia entry. This file
|
[penguin.nono](penguin.nono) were written by the
|
||||||
is important because it requires some guesses. The random.nono file
|
second author. The [soccer.nono](soccer.nono) was from a wikipedia entry. This file
|
||||||
was randomly generated. The codegolf.nono and face.nono have borrowed
|
is important because it requires some guesses. The [random.nono](random.nono) file
|
||||||
from this site:
|
was randomly generated. The [codegolf.nono](codegolf.nono) and
|
||||||
|
[face.nono](face.nono) were taken from
|
||||||
|
<https://web.archive.org/web/20130218055139/http://codegolf.com/paint-by-numbers>.
|
||||||
|
|
||||||
<http://codegolf.com/paint-by-numbers>
|
You can reproduce [dragon.nono](dragon.nono) from [hamaji.c](hamaji.c) using
|
||||||
|
[c2nono.pl](c2nono.pl):
|
||||||
You can reproduce dragon.nono from hamaji.c using c2nono.pl:
|
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
perl c2nono.pl hamaji.c | ./hamaji
|
perl c2nono.pl hamaji.c | ./hamaji
|
||||||
|
|||||||
Reference in new issue
Block a user