Sudoku solver
No replies — Read more | Post response
|
by waldner
on Jun 11, 2008 at 14:38
|
|
|
This is a simple sudoku solver (if written unformatted, it's about 7x80-char lines of code). I'm a beginner in Perl, and this looked like a good first program, but of course there are surely many naiveties in the code. Any criticism/remark/comments welcome.
use strict;
use warnings;my @r;
sub r{my $l=shift;my%x=();
for(0..8){$x{int($l/9)*9+$_}=""
;$x{($l%9)+($_*9)}="";}my$k=int($l
/27)*27+int( ($l%9)/3)*3;
@x{$k..$k+2, $k+9..$k+11,
$k+18..$k+20 }=("")x9;
delete $x{$l};
return %x;}sub
v{for(0..80){
next if($_[$_
]eq".");return
0 if (join("",
@_[@{$r[$_]}])
=~/$_[$_]/) }
return 1}sub z{
my($p,@s)= (shift,@_); v(
@s)||return 0; print"Cur: ",
@s;for(;$s[$p ]ne".";$p++){
return print"end: ",@s if($p==81);
}for(1..9){$s[$p]=$_;return 1 if
(z($p+1,@s));}}for(0..80){my
%r=r($_);$r[$_]=[keys %r];
};z(0,split(//,<>));
To use it, just give it the 81-chars string representing the board on stdin, eg
echo ".21.3.74.9.8...1 (snip) 3.9.65.1.42." | sudoku.pl
|
Guess what it does ... and a challenge
6 direct replies — Read more / Contribute
|
by liverpole
on May 24, 2008 at 13:42
|
|
|
First, can you guess what the following code does before running it? ...
1x~@;
This node by shmem gave me the idea, and may give you a hint as well.
Now for the challenge ... what's the shortest program which does the same thing? My guess is the obvious:
How about the smallest program that does the same thing slowly? The following takes about 11 seconds on my laptop:
I have no idea if a shorter such program exists.
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
|
eval JAPH
4 direct replies — Read more / Contribute
|
by kyle
on Apr 23, 2008 at 16:24
|
|
|
The goal here was to JAPH using only eval and operators on eval. I bent that rule a little (most obviously in using the string literal, 'eval'), but this is pretty close.
Things that bent the "only eval" rule:
Other operators I used: = * x - ~ . << ! | + ^ &
Here's a brief description of how this works:
|
srand & rand to encode strings
1 direct reply — Read more / Contribute
|
by elmex
on Apr 18, 2008 at 10:11
|
|
|
The last obfuscation w.r.t. fft got me inspired,
so I spent some of my CPU time to do this:
print join (' ', map {
srand ($_->[0]);
my $l;
for (1..$_->[1]) {
$l = ''; $l .= chr (int (rand (58))+65) for 1..6;
}
substr ($l, 0, $_->[0] < 1024 ? 4 : 6)
} [346, 5305], [59235, 9374], [512, 2698], [104315, 5127]), "\n";
I hope it works everywhere, as I'm not sure whether srand&rand have the same algo everywhere.
|
fft japh
1 direct reply — Read more / Contribute
|
by jdporter
on Apr 17, 2008 at 11:53
|
|
|
print chr(91.540
+ 0.765*cos(0.251*$_) + 3.886*sin(0.251*$_)
- 1.470*cos(0.503*$_) - 7.599*sin(0.503*$_)
+ 4.480*cos(0.754*$_) - 1.094*sin(0.754*$_)
- 11.495*cos(1.005*$_) + 22.147*sin(1.005*$_)
+ 1.792*cos(1.257*$_) + 8.512*sin(1.257*$_)
- 20.389*cos(1.508*$_) - 1.167*sin(1.508*$_)
- 1.011*cos(1.759*$_) + 2.549*sin(1.759*$_)
+ 2.712*cos(2.011*$_) + 3.110*sin(2.011*$_)
+ 6.179*cos(2.262*$_) - 2.177*sin(2.262*$_)
- 2.232*cos(2.513*$_) + 16.511*sin(2.513*$_)
+ 5.236*cos(2.765*$_) + 1.260*sin(2.765*$_)
- 1.607*cos(3.016*$_) + 4.017*sin(3.016*$_)
)for 0..24;
Update: Small change, props to ambrus.
A word spoken in Mind will reach its own level, in the objective world, by its own wei ght
|
Wall of Text
1 direct reply — Read more / Contribute
|
by kyle
on Apr 10, 2008 at 12:39
|
|
|
This is another along a similar vein to my last obfuscation, Brute force JAPH. Here again, crypt is used to generate the output, so deciphering all this is not straight-forward. This also means this may be a bit platform dependent (i.e., it depends on what your crypt() does).
The format of the text comes from the Unix banner program.
I'll explain the workings of this in the spoiler.
|
encryption golf (one time pad)
2 direct replies — Read more / Contribute
|
by mr_mischief
on Mar 17, 2008 at 03:19
|
|
|
undef$/;print<>^<>
In the spirit of the RSA algorithm in the shape of a dolphin, RC5 in six lines of Perl, and similar feats of encryption methods shrunk down to tattoo-worthy sizes, I present a one-time pad cipher golfed to 18 characters.
The arguments to this should be the pad file (acting as the key) and the message file. You should use a pad file the same length as the message file, or possibly longer. If you use one that's longer, you'll end up with unused pad data after your message.
Obviously don't use a pad that's shorter than the message. Also, it's called a one-time pad for a reason -- don't reuse the pad file for another message if you want the best security.
While public-key crypto has advantages there's also a place for shared-key ciphers, and this one should cost a lot less to put on a shirt or into the skin than some others. ;-)
|
a little japh
No replies — Read more | Post response
|
by kFish
on Mar 12, 2008 at 12:52
|
|
|
whelp, after looking on wikipedia i realized someone used basically the same idea and executed it a little more fu :( oh well - here goes!
#!/usr/bin/perl
$_[$.++]=chr for($_=(--$|.$|-$|.$|-$|)-$|---$|---$|-
$|)..$_-(-$|-$|.-(-$|-$|-$|-$|-$|)),($|-(-$|).--$|),
(--$|.$|-$|);$,=$_[-$|-$|];$\=$_[-$|];print$_[$_=--
$|.$|---$|].$_[$_-=-($|.$|)].$_[$_-=$|-(-$|)].$_[$_-
(-$|)],$_[--$|].$_[$_-=-($|---$|-$|-$|-$|-$|)].$_[$_
-=$|---$|---$|-$|].$_[$_-=-$|-$|-$|-$|-$|].$_[$_-=$|.
-(-$|-$|)].$_[$_-=-(-$|-$|-$|)].$_[$_-=-($|.$|-(-$|-
$|))],$_[$_-=-(-$|-$|)].$_[$_-=$|.$|].$_[$_-=-($|.$|
-(-$|-$|))].$_[$_-=-(-$|-$|-$|-$|-$|-$|)],$_[$_-=-(
-$|-$|-$|-$|)].$_[$_=--$|].$_[$_-=-($|---(-$|-$|))]
.$_[$_-(-$|-$|-$|-$|-$|-$|-$|-$|)].$_[$_-=(-$|-$|)].
$_[$|.($_-(-$|-$|-$|))];
|
Discover inner truths with PERL
2 direct replies — Read more / Contribute
|
by Anonymous Monk
on Feb 28, 2008 at 11:40
|
|
|
$a="tye";
$a++ for 1..165444;
print $a;
|
number sifter
1 direct reply — Read more / Contribute
|
by ysth
on Feb 21, 2008 at 03:56
|
|
|
use strict;
use warnings;
use 5.010;
my @n; my @p;
sub mk {
my $l = shift;
no warnings "redefine";
*is=eval 'sub { ' . join(' && ', map('$_[0] % '.$_, grep $_*$_ <=
+$n[-1], @p), <<'EOCODE');
($_[0] <= $l
? (push(@p, $_[0]), say($_[0]))
: (unshift(@n, $_[0]), mk($p[-1]**2))
) }
EOCODE
}
@n=2..(shift//200);
mk(2);
is(shift @n) while @n;
|