File: r5.pl

package info (click to toggle)
libmath-planepath-perl 117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,988 kB
  • ctags: 5,587
  • sloc: perl: 99,131; ansic: 299; sh: 233; lisp: 73; makefile: 4
file content (106 lines) | stat: -rw-r--r-- 2,499 bytes parent folder | download | duplicates (4)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/perl -w

# Copyright 2010 Kevin Ryde

# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.

use 5.010;
use strict;
use warnings;
use POSIX ();
use List::Util 'min', 'max';

# uncomment this to run the ### lines
use Smart::Comments;

my $width = 79;
my $height = 23;

my @turn_right = (0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0);

sub xturn_right {
  my ($n) = @_;
  return $turn_right[$n-1];
}
sub turn_right {
  my ($n) = @_;
  while (($n % 5) == 0) {
    $n = int($n/5);
  }
  return ($n % 5) > 2;
}

{
  my %rows;
  my $x_min = 0;
  my $x_max = 0;
  my $y_min = 0;
  my $y_max = 0;
  my $cellwidth = 1;

  my $xd = 1;
  my $yd = 0;
  my $x = 0;
  my $y = 0;
  my $n = 1;
  foreach my $n (1 .. 500) {
    $x += $xd;
    $y += $yd;

    my $cell = $rows{$x}{$y};
    if ($cell) { $cell .= '/'; }
    $cell .= $n;
    $rows{$x}{$y} = $cell;
    $cellwidth = max ($cellwidth, length($cell)+1);
    ### draw: "$x,$y  $cell"

    $x_min = min ($x_min, $x);
    $x_max = max ($x_max, $x);
    $y_min = min ($y_min, $y);
    $y_max = max ($y_max, $y);

    my $turn_right = turn_right($n) // last;
    if ($turn_right) {
      ### right: "$xd,$yd -> $yd,@{[-$xd]}"
      ($xd,$yd) = ($yd,-$xd);
    } else {
      ### left: "$xd,$yd -> @{[-$yd]},$xd"
      ($xd,$yd) = (-$yd,$xd);
    }
  }

  ### $x_min
  ### $x_max
  ### $y_min
  ### $y_max

  foreach my $y (reverse $y_min .. $y_max) {
    foreach my $x ($x_min .. $x_max) {
      printf ('%*s', $cellwidth, $rows{$x}{$y} || '');
    }
    print "\n";
  }

  exit 0;
}

{
  foreach my $n (1 .. 50) {
    print turn($n),",";
  }
  print "\n";
  exit 0;
}