/[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

Contents of /symmetry.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6 - (show annotations)
Sun Aug 26 14:15:26 2007 UTC (16 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 #!/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 my $debug = shift @ARGV || 0;
30
31 my @board = map { split(//) } split(/\n/, $board);
32 my @trace;
33 my @visited = (' ') x ($#board + 1);
34
35 # line length
36 my $ll = 8 * 2 + 1;
37
38 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 # path traversed
49 my @directions;
50
51 my @shapes_found;
52
53 my @shapes_start = ( '0,0' ); #( qw/0,0 1,0 0,3/ );
54
55 sub draw {
56 my $o = 0;
57 my $out = "\n " . join('',
58 ('0 1 2 3 4 5 6 7 8 | ') x 3
59 ) . "\n";
60 while ( $o < $#board ) {
61 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 $out .= join('', @board[ $o .. $o + $ll - 1 ]);
71 $out .= " $l ";
72 $out .= join('', @trace[ $o .. $o + $ll - 1 ]);
73 $out .= " $l ";
74 $out .= join('', @visited[ $o .. $o + $ll - 1 ]);
75 $out .= " $l\n";
76 $o += $ll;
77 }
78
79 $out .= "\n";
80
81 return $out;
82 }
83
84 sub trace {
85 warn "## trace $pos\n";
86 $trace[ $pos ] = $board[ $pos ];
87 $visited[$pos]++;
88 }
89
90 my ( $tr_x, $tr_y );
91 my ( $bl_x, $bl_y );
92
93 sub x_y {
94 my $p = shift;
95
96 my $update = 0;
97 if ( ! defined( $p ) ) {
98 $p = $pos;
99 $update = 1;
100 }
101
102 my $y = int($p / ($ll*2));
103 my $x = int(($p % $ll) / 2);
104
105 warn "??? x_y($p) $x,$y tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
106
107 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 return "$x,$y";
141 }
142
143 sub move {
144 $pos += $move_by[ $step ];
145 trace;
146 $pos += $move_by[ $step ];
147 trace;
148 push @directions, $step;
149 warn "move $step $step_name[$step] to ", x_y, "\n";
150 }
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 my $try_step = shift;
162 die "no step?" unless defined $try_step;
163
164 $try_step %= 4;
165
166 my $turn_pos = $pos + $move_by[$try_step];
167
168 my $old = $trace[ $turn_pos ];
169 $trace[ $turn_pos ] = '?';
170 $trace[ $pos ] = '*';
171 warn "TEST ", x_y($turn_pos), "\n",draw;
172 $trace[ $turn_pos ] = $old;
173
174 if ( $board[ $turn_pos ] =~ $ok_path ) {
175 warn "OK can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";
176 $step = $try_step;
177 return 1;
178 } else {
179 warn "NOPE can_turn $try_step $step_name[$try_step] turn_pos = $turn_pos b($board[$turn_pos]) t($trace[$turn_pos])\n";
180 return 0;
181 }
182 }
183
184 sub show_directions {
185 return
186 join('',
187 map {
188 substr($step_name[$_],0,1)
189 } @directions
190 )
191 ;
192 }
193
194 sub shape {
195
196 my ($x,$y) = @_;
197
198 $pos = $y * $ll * 2 + $x * 2;
199
200 @trace = ($unknown) x ( $#board + 1 );
201 @directions = ();
202 trace;
203
204 my $len = 0;
205 ( $tr_x, $tr_y ) = ( $x,$y );
206 ( $bl_x, $bl_y ) = ( $x,$y );
207 $step = 0;
208
209 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 while( 1 ) {
217
218 my $next_pos = $pos + $move_by[ $step ];
219 warn "# pos: $pos next_pos: $next_pos step: $step $step_name[$step] trace: ",show_directions,"\n";
220
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 warn draw;
234
235 warn "## tr: $tr_x,$tr_y bl: $bl_x,$bl_y\n";
236
237 if ( $debug ) {
238 print "WAIT> press enter | ",show_directions; my $foo = <STDIN>;
239 }
240 }
241
242 push @shapes_found, { x => $x, y => $y, len => $len, directions => show_directions };
243
244 my $tr = "$tr_x,$tr_y";
245 my $bl = "$bl_x,$bl_y";
246
247 warn ">>> ended at $pos, line length: $len, directions traversed: ",show_directions," tr: $tr bl: $bl\n";
248
249 if ( ! grep( /\Q$tr\E/, @shapes_start ) && $tr_x < 8 ) {
250 warn "INFO: added $tr top-right\n";
251 push @shapes_start, $tr;
252 }
253 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
258 print "WAIT> press enter"; my $foo = <STDIN>;
259
260 return $len;
261 }
262
263 foreach my $start ( @shapes_start ) {
264 my $len = shape( split(/,/,$start) );
265 warn "## $start has $len elements\n";
266 }
267
268 print ">>> RESULTS:\n";
269 foreach my $r ( @shapes_found ) {
270 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