/[symmetry-circle]/symmetry.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /symmetry.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (hide annotations)
Sun Aug 26 14:15:26 2007 UTC (11 years, 7 months ago) by dpavlin
File MIME type: text/plain
File size: 5415 byte(s)
- fix iteration over shapes (a bit, stull not bug-free)
- added grid (x,y) to output
- other tweaks and bugfixes
1 dpavlin 1 #!/usr/bin/perl -w
2    
3     # symmetry.pl
4     #
5     # 08/26/07 02:40:37 CEST Dobrica Pavlinusic <dpavlin@rot13.org>
6    
7     use strict;
8    
9     my $board = << '_BOARD_';
10     +-+-+-+-+-+-+-+-+
11     | | |o| | |
12     + + +-+ +-+ |
13     |o| | o | o |
14     + + +-+ +-+ |
15     | | o | | |
16     +-+-+ +-+-+-+-+
17     | | |o| |
18     +-+ + +-+-+ +-+
19     | |o| |o|o|o|o|
20     + + +-+-+-+-+ +-+
21     |o| | | |
22     + +-+-+-+-+-+-+-+
23     | | o |
24     +-+-+-+-+-+-+-+-+
25     | o | o |
26     +-+-+-+-+-+-+-+-+
27     _BOARD_
28    
29 dpavlin 4 my $debug = shift @ARGV || 0;
30    
31 dpavlin 1 my @board = map { split(//) } split(/\n/, $board);
32 dpavlin 3 my @trace;
33 dpavlin 6 my @visited = (' ') x ($#board + 1);
34 dpavlin 1
35     # line length
36     my $ll = 8 * 2 + 1;
37    
38 dpavlin 3 my @step_name = ( qw/right down left up/ );
39     my @move_by = ( 1, $ll, -1, -$ll );
40     my $step = 0; # right
41    
42     # offset 0, top-left corner
43     my $pos = 0;
44    
45     # unknown trace position
46     my $unknown = ' ';
47    
48 dpavlin 4 # path traversed
49     my @directions;
50    
51 dpavlin 5 my @shapes_found;
52 dpavlin 4
53 dpavlin 5 my @shapes_start = ( '0,0' ); #( qw/0,0 1,0 0,3/ );
54    
55 dpavlin 1 sub draw {
56     my $o = 0;
57 dpavlin 6 my $out = "\n " . join('',
58     ('0 1 2 3 4 5 6 7 8 | ') x 3
59     ) . "\n";
60 dpavlin 1 while ( $o < $#board ) {
61 dpavlin 6 my $l = '|';
62     if ( $o % ($ll*2) == 0) {
63     my $y = int($o / ($ll*2));
64     $out .= "$y ";
65     $l = $y;
66     } else {
67     $out .= " ";
68     }
69    
70 dpavlin 3 $out .= join('', @board[ $o .. $o + $ll - 1 ]);
71 dpavlin 6 $out .= " $l ";
72 dpavlin 3 $out .= join('', @trace[ $o .. $o + $ll - 1 ]);
73 dpavlin 6 $out .= " $l ";
74     $out .= join('', @visited[ $o .. $o + $ll - 1 ]);
75     $out .= " $l\n";
76 dpavlin 1 $o += $ll;
77     }
78 dpavlin 6
79     $out .= "\n";
80    
81 dpavlin 1 return $out;
82     }
83    
84     sub trace {
85 dpavlin 3 warn "## trace $pos\n";
86 dpavlin 1 $trace[ $pos ] = $board[ $pos ];
87 dpavlin 6 $visited[$pos]++;
88 dpavlin 1 }
89    
90 dpavlin 5 my ( $tr_x, $tr_y );
91 dpavlin 6 my ( $bl_x, $bl_y );
92 dpavlin 5
93 dpavlin 6 sub x_y {
94     my $p = shift;
95 dpavlin 5
96 dpavlin 6 my $update = 0;
97     if ( ! defined( $p ) ) {
98     $p = $pos;
99     $update = 1;
100     }
101 dpavlin 5
102 dpavlin 6 my $y = int($p / ($ll*2));
103     my $x = int(($p % $ll) / 2);
104 dpavlin 5
105 dpavlin 6 warn "??? x_y($p) $x,$y tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
106 dpavlin 5
107 dpavlin 6 if ( $update ) {
108    
109     # $tr_x = $x if $x > $tr_x && $y == $tr_y;
110     # $tr_y = $y if $y < $tr_y && $x == $tr_x;
111    
112     if (
113     $y < $tr_y
114     ||
115     $y <= $tr_y && $x > $tr_x
116     ) {
117     ( $tr_x, $tr_y ) = ( $x, $y );
118     warn "## UPDATED tr: $tr_x,$tr_y\n";
119     }
120    
121     if (
122     $x < $bl_x
123     ||
124     $y > $bl_y
125     ) {
126     ( $bl_x, $bl_y ) = ( $x, $y );
127     warn "## UPDATED bl: $bl_x,$bl_y\n";
128     }
129    
130     # $bl_x = $x if $x < $bl_x; # && $y == $bl_y;
131     # $bl_y = $y if $y > $bl_y; # && $x == $bl_x;
132    
133     }
134    
135     warn "## x_y($p) -> $x,$y ",
136     $update ? " tr: $tr_x,$tr_y bl: $bl_x,$bl_y" : '',
137     "\n";
138    
139     # return ($x,$y) if wantarray;
140 dpavlin 5 return "$x,$y";
141     }
142    
143 dpavlin 1 sub move {
144     $pos += $move_by[ $step ];
145     trace;
146     $pos += $move_by[ $step ];
147     trace;
148 dpavlin 4 push @directions, $step;
149 dpavlin 6 warn "move $step $step_name[$step] to ", x_y, "\n";
150 dpavlin 1 }
151    
152     sub follow {
153     $step = shift;
154     $step %= 4;
155     warn "follow $step $step_name[$step]\n";
156     }
157    
158     my $ok_path = qr/[\|\-]/;
159    
160     sub can_turn {
161 dpavlin 2 my $try_step = shift;
162     die "no step?" unless defined $try_step;
163 dpavlin 1
164 dpavlin 2 $try_step %= 4;
165 dpavlin 1
166 dpavlin 2 my $turn_pos = $pos + $move_by[$try_step];
167    
168 dpavlin 1 my $old = $trace[ $turn_pos ];
169     $trace[ $turn_pos ] = '?';
170 dpavlin 6 $trace[ $pos ] = '*';
171     warn "TEST ", x_y($turn_pos), "\n",draw;
172 dpavlin 1 $trace[ $turn_pos ] = $old;
173    
174     if ( $board[ $turn_pos ] =~ $ok_path ) {
175 dpavlin 5 warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";
176 dpavlin 2 $step = $try_step;
177 dpavlin 1 return 1;
178     } else {
179 dpavlin 5 warn "NOPE can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";
180 dpavlin 1 return 0;
181     }
182     }
183    
184 dpavlin 4 sub show_directions {
185     return
186     join('',
187     map {
188     substr($step_name[$_],0,1)
189     } @directions
190     )
191     ;
192     }
193    
194 dpavlin 3 sub shape {
195 dpavlin 1
196 dpavlin 3 my ($x,$y) = @_;
197 dpavlin 1
198 dpavlin 3 $pos = $y * $ll * 2 + $x * 2;
199    
200     @trace = ($unknown) x ( $#board + 1 );
201 dpavlin 4 @directions = ();
202 dpavlin 3 trace;
203    
204     my $len = 0;
205 dpavlin 6 ( $tr_x, $tr_y ) = ( $x,$y );
206     ( $bl_x, $bl_y ) = ( $x,$y );
207     $step = 0;
208 dpavlin 3
209 dpavlin 6 if ( $visited[$pos] > 3 ) {
210     warn "*** shape from $x,$y pos: $pos iterated 4 times, skipping\n";
211     return;
212     }
213    
214     warn "<<< shape from $x,$y pos: $pos\n";
215    
216 dpavlin 3 while( 1 ) {
217    
218     my $next_pos = $pos + $move_by[ $step ];
219 dpavlin 4 warn "# pos: $pos next_pos: $next_pos step: $step $step_name[$step] trace: ",show_directions,"\n";
220 dpavlin 3
221     if ( $trace[ $next_pos ] ne $unknown ) {
222     warn "position $next_pos re-visited, exiting\n";
223     last;
224     } elsif ( $board[ $next_pos ] =~ $ok_path ) {
225     warn "OK next_pos = $next_pos b($board[$next_pos]) t($trace[$next_pos])\n";
226     move;
227     $len++;
228     can_turn( $step + 1 );
229     } else {
230     warn "find line continuation from $step $step_name[$step]...\n";
231     can_turn( $step - 1 ) || can_turn( $step + 1 ) || die "can't find new direction";
232     }
233 dpavlin 6 warn draw;
234 dpavlin 3
235 dpavlin 6 warn "## tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
236    
237 dpavlin 4 if ( $debug ) {
238     print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;
239     }
240 dpavlin 1 }
241    
242 dpavlin 5 push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };
243 dpavlin 4
244 dpavlin 6 my $tr = "$tr_x,$tr_y";
245     my $bl = "$bl_x,$bl_y";
246 dpavlin 5
247 dpavlin 6 warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions," tr: $tr bl: $bl\n";
248    
249 dpavlin 5 if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) {
250 dpavlin 6 warn "INFO: added $tr top-right\n";
251 dpavlin 5 push @shapes_start, $tr;
252     }
253 dpavlin 6 if ( ! grep( /\Q$bl\E/, @shapes_start ) && $bl_y < 8 ) {
254     warn "INFO: added $bl bottom-left\n";
255     push @shapes_start, $bl;
256     }
257 dpavlin 5
258 dpavlin 4 print "WAIT> press enter"; my $foo = <STDIN>;
259    
260 dpavlin 3 return $len;
261 dpavlin 1 }
262    
263 dpavlin 5 foreach my $start ( @shapes_start ) {
264 dpavlin 3 my $len = shape( split(/,/,$start) );
265     warn "## $start has $len elements\n";
266     }
267 dpavlin 4
268     print ">>> RESULTS:\n";
269 dpavlin 5 foreach my $r ( @shapes_found ) {
270 dpavlin 4 printf "%2d,%-2d len: %-4d directions: %s\n", $r->{x}, $r->{y}, $r->{len}, $r->{directions};
271     }

Properties

Name Value
svn:executable

  ViewVC Help
Powered by ViewVC 1.1.26