Player (36)
Joined: 9/11/2004
Posts: 2631
I've worked on it some, but I haven't gotten it to actually print anything yet. >.> I have some sort of logic bug somewhere probably with the scoping of @_ (bad stuff) or a misnamed variable (ok if that's it), here's what I have so far:
<>;split;sub p{for(;$i<$#{$_[0]};$i++){p(splice(@{$_[0]},$i,1),"$$_[1]$$_[$i] ");}print"$$_[1] $$_[0][0]"if(!$#{$_[0]});}p(@_);
I tried to get that for block into postfix notation but it wouldn't take, silly parser limitations. :)
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Joined: 4/16/2005
Posts: 251
Stuff that I don't understand there:
$i<$#{$_[0]}
If you call that with an array won't it get interpolated, and thus a variable of the name of the first argument used? Also, the $i should be my$i, or it will get used globally and the recursion breaks. But it's interesting that you started with the algorithm that proved shortest for me.... my first version was rather strange:
-nal
$e='@s=(0..@F);print"@F[map{splice@s,pop@p,1}@p=(0 )]"';
map{($e="for\$$_(0..$_){$e}")=~s/ /,\$$_ /}1..$#F;eval$e
Former player
Joined: 5/3/2004
Posts: 366
I don't know any Perl, and it looks almost like random gibberish to me, but I was wondering... Basically, a simple little puzzler we were given in college (for .1% overall extra credit in the class, hahaha) was to make a program that prints itself out. Rules were simple; commands like Basic's "List" (printed out the program... old 1980s programming) weren't allowed, and the program wasn't allowed to search for itself in memory/on the disk/etc and print itself out. It was pretty fun, although admittedly simple when complete. Anyways, I was kind of wondering what a compact Perl solution to the problem would look like. Anybody?
Player (36)
Joined: 9/11/2004
Posts: 2631
Sure
seek DATA, 0, 0;
print while <DATA>;
__DATA__
And for a less cheating version that I didn't write off of the top of my head:
$_=q(print"\$_=q($_);eval;");eval;
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Player (36)
Joined: 9/11/2004
Posts: 2631
Gorash wrote:
Stuff that I don't understand there:
$i<$#{$_[0]}
If you call that with an array won't it get interpolated, and thus a variable of the name of the first argument used?
I was trying to locate an answer on that from a few of my buddies, but no one seemed to know. And $#$_[0] is a syntax error (As well as $$#_[0] which was my first attempt). So I tried that and it parsed. Does anyone know how to return the length of list inside a list passed by a reference? Perhaps it's like
for(;$i<$#{$_[0]};$i++){p(splice(@{$_[0]},$i,1),"$$_[1]$$_[$i] ");}
vs.
p(splice(@{$_[0]},$i,1),"$$_[1]$$_[$i] ")for(;$i<$#{$_[0]};$i++);
It might simply be a parser limitation, the statement *should* work, but doesn't because the programmers didn't think that anyone would be that silly.
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Editor, Player (69)
Joined: 6/22/2005
Posts: 1050
Gigafrost wrote:
Basically, a simple little puzzler we were given in college (for .1% overall extra credit in the class, hahaha) was to make a program that prints itself out.
That's called a quine. Here's a page that has examples in various languages.
Current Projects: TAS: Wizards & Warriors III.
Former player
Joined: 5/3/2004
Posts: 366
Dacicus wrote:
Gigafrost wrote:
Basically, a simple little puzzler we were given in college (for .1% overall extra credit in the class, hahaha) was to make a program that prints itself out.
That's called a quine. Here's a page that has examples in various languages.
That's right, they had a name. Hahaha... It's bad enough not to remember the name, but forgetting that they even had a name? Thanks for the link. :)
Joined: 4/16/2005
Posts: 251
OmnipotentEntity wrote:
I was trying to locate an answer on that from a few of my buddies, but no one seemed to know. And $#$_[0] is a syntax error (As well as $$#_[0] which was my first attempt). So I tried that and it parsed. Does anyone know how to return the length of list inside a list passed by a reference?
Oh that's what you wanted to do. I checked, $#{$_[0]} seems to work. But then you should use a reference in the initital invocation of the function too. Which way of notation you use should also make no difference, but who knows... Here is my current minimum, I think it's very similar to what you're trying to do:
-nal
sub p{@_?map{my$e=$_;map[$e,@$_],p(grep{$e ne$_}@_)}@_:0}map{print"@$_"}p@F
Joined: 4/3/2005
Posts: 575
Location: Spain
The winner of the "Worst Abuse of the Rules" award in the 1994 Obfuscated C Code contest will never cease to amuse me.
No.
Player (36)
Joined: 9/11/2004
Posts: 2631
Gorash wrote:
Oh that's what you wanted to do. I checked, $#{$_[0]} seems to work. But then you should use a reference in the initital invocation of the function too.
My perl must be a bit rusty, because I thought one of the more annoying things about perl is it automatically passes all lists passed to a function by reference. And it seems that I won't get anywhere near your length. I was never very good at byte compressing.
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Joined: 4/16/2005
Posts: 251
The annoying thing is that you can't put an array into an array without references... One shouldn't do that in programming anyway, I use golfing only to obfuscate Matlab scripts so that the guy who has to evaluate my papers gets what he deserves. Funny enough, the harder I make it for them to understand how I fulfilled their stupid tasks, the higher is my score...
Player (36)
Joined: 9/11/2004
Posts: 2631
Well, that's how it is normally in most languages, the only difference is that in most modern languages the dereferencing is done automagically. So perl is strange in that aspect, but then again perl is strange in pretty much every aspect.
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Player (36)
Joined: 9/11/2004
Posts: 2631
KERBUMP! Give the set of numbers whose amount of coprimes less than or equal to itself is not divisible by four. (Set of all n where phi(n) is not congruent to 0 mod 4, and phi(x) is the Euler Totient Function.) And also, prove that using quarter face turns only, a Rubik's Cube scrambled with an even number of moves can only be solved with an even number of moves. Same for odd.
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Editor, Expert player (2080)
Joined: 6/15/2005
Posts: 3284
OmnipotentEntity wrote:
Give the set of numbers whose amount of coprimes less than or equal to itself is not divisible by four. (Set of all n where phi(n) is not congruent to 0 mod 4, and phi(x) is the Euler Totient Function.)
I assume you mean positive (non-negative?) integers:
  • 1
  • 2
  • 4
  • 2*p^a, where p is prime of form 4*n+3 and a>0
  • p^a, where p is prime of form 4*n+3 and a>0
I'll leave out the proof to see if anyone else can get it.
Player (87)
Joined: 2/10/2006
Posts: 113
Location: US
OmnipotentEntity wrote:
And also, prove that using quarter face turns only, a Rubik's Cube scrambled with an even number of moves can only be solved with an even number of moves. Same for odd.
You can check that a 1/4-turn is an odd permutation of the faces (observe that 2n-cycles are odd permutations, and a 1/4 turn is five 4-cycles). Then even number of moves <=> even permutation <=> even number of moves required to solve it. PS I'm a math major :D
Use the force
Experienced player (702)
Joined: 2/19/2006
Posts: 742
Location: Quincy, MA
i cant solve this rubik's cube!
Super Mario Bros. console speedrunner - Andrew Gardikis
Player (81)
Joined: 3/11/2005
Posts: 352
Location: Oregon
This is a fun one you can try at home. If you take a narrow strip of paper with parallel edges, tie it in a knot and flatten the knot, it will make a regular pentagon. For an idealized knot, prove that this works. I'm reasonably sure a proof exists but haven't found one yet.
ideamagnate| .seen aqfaq <nothing happens> DK64_MASTER| .seen nesvideoagent * DK64_MASTER slaps forehead
Active player (287)
Joined: 3/4/2006
Posts: 341
Tying a narrow strip of paper into a knot requires three folds: two to make a loop and one to put the paper through the loop. Thus the shape formed will be a pentagon, with one side where the paper enters, one side for each fold, and one side where the paper leaves. We will now show that the pentagon is regular. The "path" of the paper through the knot consists of four trapezoids whose vertices are four of the vertices of the pentagon. In addition, these trapezoids have the same height, namely the width of the paper. Consider one such trapezoid with vertices ABCE. Since all but one set of four vertices from ABCDE form a trapezoid, one of the following is true: 1) BCDA and EABD are trapezoids of equal height, and thus the distance from A to BC equals the distance from B to EA. 2) DEAC and CDEB are trapezoids of equal height, and thus the distance from E to AC equals the distance from C to EB. Either way, we can use congruent triangles to show that ABCE is an isosceles trapezoid. Hence ABCE is cyclic, and the edges EA and BC are congruent. Since a circle is defined uniquely by three points, considering all four trapezoids shows that ABCDE is cyclic and all five sides are congruent. Hence ABCDE is a regular pentagon. EDIT: Instead of saying ABCE is cyclic, it is easier to say that angles EAB and ABC are congruent, hence all five angels are congruent. The proof of regularity is then slightly more direct.
Player (201)
Joined: 7/6/2004
Posts: 511
OmnipotentEntity wrote:
Old puzzle, new twist: You work for the Secret Service, but are not particularly well respected. You were given sixteen coins, and told that two of them are counterfeit. The counterfeit coins either weigh less than or more than the other fourteen coins, and they both weigh the same amount. You also have a scale. Except this scale is triangle shaped and has three pans. (No one ever gives you the good equipment.) Unfortunately, the Secret Service is beholden to the Scale Industry Corporate Interests, and are forcing the Secret Service to pay them every time a scale is used by anyone. How many weighings are required to determine which two coins are false?
Revival an ancient topic time. I just read another variant of this type of problem, you have a regular scale (2 sides). How and how many weighings does it take to find the 1 odd ball from a group of 12 balls? How about 120 balls? Note the odd ball may be heavier or lighter than the others. I haven't worked on them yet but the 120 sounds very challenging. (Both have been solved already, without the use of a computer as aide.) When I read the problem it gave the answer of how many weighings and just asked for how it is done, but I think the problem is better if that part is unknown too.
g,o,p,i=1e4,a[10001];main(x){for(;p?g=g/x*p+a[p]*i+2*!o: 53^(printf("%.4d",o+g/i),p=i,o=g%i);a[p--]=g%x)x=p*2-1;}
Tub
Joined: 6/25/2005
Posts: 1377
there's a generic strategy to that, requiring 4 steps for 12 oddballs, 6 for 120. Before I post that strategy, anyone found a better solution?
m00
Player (36)
Joined: 9/11/2004
Posts: 2631
Here's one that I'm working on and off on (I have one but not two or three.) If a set of seven cylinders are sufficiently thin, (ie, the ratio between the width and the height is small enough,) then it's possible to arrange them in such a manner that each cylinder touches the other six. 1) Find the arrangement. 2) Find the required ratio. 3) Why isn't eight possible? (Is eight possible?)
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Player (201)
Joined: 7/6/2004
Posts: 511
Tub, the generic strategy would be nice to hear, the optimal is one less than that though. Omni, do the cylinders all have to be the same height and diameter?
g,o,p,i=1e4,a[10001];main(x){for(;p?g=g/x*p+a[p]*i+2*!o: 53^(printf("%.4d",o+g/i),p=i,o=g%i);a[p--]=g%x)x=p*2-1;}
Player (36)
Joined: 9/11/2004
Posts: 2631
In the solution I'm thinking of, they are.
Build a man a fire, warm him for a day, Set a man on fire, warm him for the rest of his life.
Tub
Joined: 6/25/2005
Posts: 1377
flagitious wrote:
Tub, the generic strategy would be nice to hear, the optimal is one less than that though.
I thought so, because my strategy can handle up to 27 balls with 4 moves, not just 12. ;) step 1: put 1/3rd of the balls left, 1/3rd right (equal amounts), leave the rest off the scale. step 2: - if they were equal, the oddball is in the last 3rd. Compare that last 3rd with an equal amount of normal balls, to find out if the oddball weights more or less. - if they weren't equal, compare one of the sides against the last 3rd, that's known to be normal. basically, after the first two steps, you have reduced the suspicious balls to 1/3rd, and you know if the oddball weights more or less. step 3 onward: put 1/3rd left, 1/3rd right, and you can reduce the amount of suspicious balls by 3 each step. the number of moves for that strategy is: ceil ( log3 n ) + 1 (for any amount > 3) determining the relative weight of the oddball can probably be done without wasting a whole step. Unfortunately, the solution doesn't occur to me right now, but I'm currently just posting while waiting for my compiler. ;) Find two steps that determine the relative weight and reduce the balls by 4 instead of 3, and you got your 12 balls in 3, 120 in 5.
m00
Editor, Reviewer, Experienced player (980)
Joined: 4/17/2004
Posts: 3109
Location: Sweden
OmnipotentEntity: What's the elastic modulus and yield strength of the cylinders?