-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
trizen
committed
Jul 24, 2015
1 parent
4cef601
commit bfb281f
Showing
18 changed files
with
584 additions
and
2,073 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#!/usr/bin/perl | ||
|
||
# Author: Daniel "Trizen" Șuteu | ||
# License: GPLv3 | ||
# Date: 23 July 2015 | ||
# http://github.com/trizen | ||
|
||
# Find files which have almost the same content. | ||
|
||
use 5.014; | ||
use strict; | ||
use warnings; | ||
|
||
use Math::BigInt (try => 'GMP'); | ||
|
||
use File::Find qw(find); | ||
use Getopt::Long qw(GetOptions); | ||
|
||
sub help { | ||
my ($code) = @_; | ||
|
||
print <<"HELP"; | ||
usage: $0 [options] /my/path [...] | ||
Options: | ||
-w --whitespaces! : remove whitespaces (default: false) | ||
-u --unique! : don't include a file in more groups (default: false) | ||
-h --help : print this message and exit | ||
Example: | ||
$0 -w ~/Documents | ||
HELP | ||
|
||
exit($code // 0); | ||
} | ||
|
||
my $strip_spaces = 0; # bool | ||
my $unique = 0; # bool | ||
|
||
GetOptions( | ||
'w|whitespaces!' => \$strip_spaces, | ||
'u|unique!' => \$unique, | ||
'h|help' => \&help, | ||
) | ||
or die("Error in command line arguments"); | ||
|
||
sub hash ($) { | ||
my ($str) = @_; | ||
|
||
$strip_spaces | ||
and $str =~ s/\s+//g; | ||
|
||
state $ten = Math::BigInt->new(10); | ||
|
||
my $hash1 = Math::BigInt->new(0); | ||
my $pow = Math::BigInt->new(1); | ||
|
||
state $chars = {}; | ||
my @chars = map { $chars->{$_} //= Math::BigInt->new($_) } unpack("C*", $str); | ||
|
||
foreach my $char (@chars) { | ||
$hash1->badd($pow->copy->bmul($char)); | ||
$pow->bmul($ten); | ||
} | ||
|
||
return $hash1; | ||
} | ||
|
||
sub hash_file ($) { | ||
my ($file) = @_; | ||
open my $fh, '<:raw', $file; | ||
hash( | ||
do { local $/; <$fh> } | ||
); | ||
} | ||
|
||
sub alike_hashes ($$) { | ||
my ($h1, $h2) = @_; | ||
|
||
my $pow = abs($h1->copy->blog(10) - $h2->copy->blog(10)); | ||
|
||
my $ratio = ($h2 > $h1 ? ($h2 / $h1) : ($h1 / $h2)); | ||
my $limit = 10**$pow; | ||
|
||
$ratio == $limit; | ||
} | ||
|
||
sub find_similar_files (&@) { | ||
my $code = shift; | ||
|
||
my @files; | ||
find { | ||
wanted => sub { | ||
(-f) | ||
&& push @files, | ||
{ | ||
hash => hash_file($File::Find::name), | ||
name => $File::Find::name, | ||
}; | ||
} | ||
} => @_; | ||
|
||
my %dups; | ||
foreach my $i (0 .. $#files - 1) { | ||
for (my $j = $i + 1 ; $j <= $#files ; $j++) { | ||
if (alike_hashes($files[$i]{hash}, $files[$j]{hash})) { | ||
push @{$dups{$files[$i]{name}}}, | ||
( | ||
$unique | ||
? ${splice @files, $j--, 1}{name} | ||
: $files[$j]{name} | ||
); | ||
} | ||
} | ||
} | ||
|
||
while (my ($fparent, $fdups) = each %dups) { | ||
$code->(sort $fparent, @{$fdups}); | ||
} | ||
|
||
return 1; | ||
} | ||
|
||
{ | ||
@ARGV || help(1); | ||
local $, = "\n"; | ||
find_similar_files { | ||
say @_, "-" x 80 if @_; | ||
} | ||
@ARGV; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/perl | ||
|
||
# Author: Daniel "Trizen" Șuteu | ||
# License: GPLv3 | ||
# Date: 20 July 2015 | ||
# Website: https://github.com/trizen | ||
|
||
# An image generator based on the following formula: n!/(n-1)!, n!/(n-2)!, ... n!/(n-n)! | ||
|
||
# Simplified as: | ||
# n!/(n-1)! = n | ||
# n!/(n-2)! = n * (n-1) | ||
# n!/(n-3)! = n * (n-1) * (n-2) | ||
|
||
use 5.010; | ||
use strict; | ||
use warnings; | ||
use GD::Simple; | ||
|
||
use bignum (try => 'GMP'); | ||
use File::Spec::Functions qw(catfile); | ||
|
||
my $beg = 3; # start point | ||
my $end = 30; # end point | ||
my $dir = 'Factorial turtles'; # where to output the images | ||
|
||
if (not -d $dir) { | ||
mkdir($dir) | ||
or die "Can't mkdir `$dir': $!"; | ||
} | ||
|
||
foreach my $n ($beg .. $end) { | ||
|
||
{ | ||
local $| = 1; | ||
printf("[%3d of %3d]\r", $n, $end); | ||
} | ||
|
||
my $img = 'GD::Simple'->new(5000, 5000); | ||
$img->moveTo(2500, 2500); | ||
$img->fgcolor('red'); | ||
|
||
my @values; | ||
my $p = 1; | ||
foreach my $j (0 .. $n - 1) { | ||
$p *= $n - $j; | ||
push @values, $p; | ||
} | ||
|
||
for my $i (1 .. 100) { | ||
foreach my $value (@values) { | ||
$img->line($i); | ||
$img->turn($value); | ||
} | ||
} | ||
|
||
my $image_name = catfile($dir, sprintf('%03d.png', $n)); | ||
|
||
open my $fh, '>:raw', $image_name or die $!; | ||
print {$fh} $img->png; | ||
close $fh; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/perl | ||
|
||
# Author: Daniel "Trizen" Șuteu | ||
# License: GPLv3 | ||
# Date: 18 July 2015 | ||
# https://github.com/trizen | ||
|
||
# | ||
## Generate a Fibonacci cluster of spirals. | ||
# | ||
|
||
use 5.010; | ||
use strict; | ||
use warnings; | ||
use GD::Simple; | ||
|
||
my $img = 'GD::Simple'->new(8000, 8000); | ||
$img->moveTo(3500, 3500); | ||
|
||
sub t($) { | ||
$img->turn(shift); | ||
} | ||
|
||
sub l($) { | ||
$img->line(shift); | ||
} | ||
|
||
sub c($) { | ||
$img->fgcolor(shift); | ||
} | ||
|
||
sub fibonacci(&$) { | ||
my ($callback, $n) = @_; | ||
my @fib = (1, 1); | ||
for (1 .. $n - 2) { | ||
$callback->($fib[0]); | ||
@fib = ($fib[-1], $fib[-1] + $fib[-2]); | ||
} | ||
$callback->($_) for @fib; | ||
} | ||
|
||
c 'red'; | ||
for my $i (1 .. 180) { | ||
fibonacci { | ||
l $_[0]**(1 / 11); | ||
t $i; | ||
} | ||
$i; | ||
t 0; | ||
} | ||
|
||
my $image_name = 'fibonacci_spirals.png'; | ||
|
||
open my $fh, '>:raw', $image_name or die $!; | ||
print {$fh} $img->png; | ||
close $fh; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/perl | ||
|
||
# Author: Daniel "Trizen" Șuteu | ||
# License: GPLv3 | ||
# Date: 21 July 2015 | ||
# http://github.com/trizen | ||
|
||
# | ||
## Generate a complex machine-like art based on simple mathematics. | ||
# | ||
|
||
use 5.010; | ||
use strict; | ||
use warnings; | ||
use GD::Simple; | ||
use List::Util qw(shuffle); | ||
|
||
my $max = 1_000_000; | ||
my $limit = int(sqrt($max)); | ||
|
||
say "Possible combinations: $limit!"; | ||
|
||
# create a new image | ||
my $img = GD::Simple->new($limit * 3, $limit * 3); | ||
|
||
# move to the center | ||
$img->moveTo($limit * 1.5, $limit * 1.5); | ||
|
||
my $i = 1; | ||
my $j = 1; | ||
|
||
for my $m (shuffle(1 .. $limit)) { | ||
|
||
for my $n ($j .. $i**2) { | ||
$img->line(1); | ||
$img->turn($n * $i + $m); | ||
++$j; | ||
} | ||
|
||
++$i; | ||
} | ||
|
||
open my $fh, '>:raw', "random_machinery.png"; | ||
print $fh $img->png; | ||
close $fh; |
Oops, something went wrong.