Carolina Krayts is the best X-Wing podcast

By SaltMaster 5000, in X-Wing

20 minutes ago, sozin said:

Post your code @MajorJuggler , I will see if I can get a better golfing score with Python ;)

Funny part was I wasn't even really trying to make it short... convolution function calls are a wonderful thing.

function WinningOdds = FinalSalvoOdds(YourDice, OpponentDice)

Dist_You = 1;

for dice = 1:YourDice

Dist_You = conv(Dist_You, [0.5 0.5]);

end

Dist_Opponent = 1;

for dice = 1:OpponentDice

Dist_Opponent = conv(Dist_Opponent, [0.5 0.5]);

end

NetDist = conv(Dist_You, fliplr(Dist_Opponent));

WinningOdds = sum(NetDist(end-YourDice+1:end)) / (1 - NetDist(end-YourDice));

end

It can go to 11 pretty easily...

function WinningOdds = FinalSalvoOdds(YourDice, OpponentDice)

Dist_You = 1; Dist_Opponent = 1;

for dice = 1:YourDice

Dist_You = conv(Dist_You, [0.5 0.5]);

end

for dice = 1:OpponentDice

Dist_Opponent = conv(Dist_Opponent, [0.5 0.5]);

end

NetDist = conv(Dist_You, fliplr(Dist_Opponent));

WinningOdds = sum(NetDist(end-YourDice+1:end)) / (1 - NetDist(end-YourDice));

end

Nice, 11 is a pretty good score. I have a four hour airplane ride this afternoon, will see if I can get lower :)

24 minutes ago, sozin said:

Nice, 11 is a pretty good score. I have a four hour airplane ride this afternoon, will see if I can get lower :)

Taking some liberties by condensing the for loops to one line each... down to 7 lines!

Technically you can force it all to one stupid long line, but the below is actually very human-readable. Arguably better than the 11 line version. There's essentially 5 steps in the computation, plus the 2 for the function declaration and end. I'm happy at 7. :P

function WinningOdds = FinalSalvoOdds(YourDice, OpponentDice)

Dist_You = 1; Dist_Opponent = 1;

for dice = 1:YourDice; Dist_You = conv(Dist_You, [0.5 0.5]); end

for dice = 1:OpponentDice; Dist_Opponent = conv(Dist_Opponent, [0.5 0.5]); end

NetDist = conv(Dist_You, fliplr(Dist_Opponent));

WinningOdds = sum(NetDist(end-YourDice+1:end)) / (1 - NetDist(end-YourDice));

end

[edit] 6 lines if you want to move lines 2 into lines 3 and 4, you'll just need shorter variable names to get it to fit in a standard column width. More like 4 steps plus function call.

function WinningOdds = FinalSalvoOdds(YourDice, OpponentDice)

You = 1; for dice = 1:YourDice; You = conv(You, [0.5 0.5]); end ;

Opp = 1; for dice = 1:OpponentDice; Opp = conv(Opp, [0.5 0.5]); end ;

NetDist = conv(You, fliplr(Opp));

WinningOdds = sum(NetDist(end-YourDice+1:end)) / (1 - NetDist(end-YourDice));

end

Edited by MajorJuggler

NERDS!

31 minutes ago, Biophysical said:

NERDS!

BUT WE CAN REDUCE THE CHARACTER COUNT TOO!!

[edit] 198 characters, 147 excluding spaces. Beat that!

function X = FinalSalvoOdds(a, b)

A = 1; for d = 1:a; A = conv(A, [1 1]/2); end

B = 1; for d = 1:b; B = conv(B, [1 1]/2); end

Y = conv(A, fliplr(B));

X = sum(Y(b+2:end)) / (1-Y(b+1));

end

Edited by MajorJuggler
27 minutes ago, Biophysical said:

NERDS!

Yes, but it is beautiful to watch.

you need to put in some restrictions, or I'm going win with one line and it can even be java:

public void calculateFinalSalvoOdds(){ System.out.println("All the precalculated data here"); }

Call a judge on me, I'll lose my next round.

Do you think Salty's objective was to create an interactive Krayts Podcast Experience? If so 10/10.

9 minutes ago, Brunas said:

you need to put in some restrictions, or I'm going win with one line and it can even be java:

public void calculateFinalSalvoOdds(){ System.out.println("All the precalculated data here"); }

Call a judge on me, I'll lose my next round.

Because of course you would. The power of MatLab is having that matrix math built in that really makes it easy. Without hunting down a library, I can't do that in java or c++. At least not simply.

TLDR: I'm too lazy for this.

Edited by Killerardvark

OK I'm really done now. Because it's a 50-50 binomial distribution, you don't need the second loop at all. For the statistically minded, note that reversing a symmetrical distribution still equals itself.

4 lines, 124 characters, 96 without spaces.

function X = FinalSalvoOdds(a, b)

D = 1; for d = 1:a+b; D = conv(D, [1 1]/2); end

X = sum(D(b+2:end)) / (1-D(b+1));

end

[edit] P.S. Scum and Villainy has now commandeered this podcast thread with code. Mission Accomplished.

Edited by MajorJuggler
16 minutes ago, MajorJuggler said:

OK I'm really done now. Because it's a 50-50 binomial distribution, you don't need the second loop at all. For the statistically minded, note that reversing a symmetrical distribution still equals itself.

4 lines, 124 characters, 96 without spaces.

function X = FinalSalvoOdds(a, b)

D = 1; for d = 1:a+b; D = conv(D, [1 1]/2); end

X = sum(D(b+2:end)) / (1-D(b+1));

end

[edit] P.S. Scum and Villainy has now commandeered this podcast thread with code. Mission Accomplished.

Math is for nerds, we have computers to be our math slaves and make them just actually final salvo billions of times and tell us how it went!

42 minutes ago, MajorJuggler said:

[edit] 198 characters, 147 excluding spaces. Beat that!

spaces not tabs? You're never going to get a job at Pied Piper.

31 minutes ago, jagsba said:

spaces not tabs? You're never going to get a job at Pied Piper.

Noted, not working at Pied Piper. I hope you're exaggerating. Choosing employees on their whitespace preference is asinine.

11 minutes ago, Killerardvark said:

Noted, not working at Pied Piper. I hope you're exaggerating. Choosing employees on their whitespace preference is asinine.

It's a reference from the show Silicon Valley on HBO.

Just now, Tbetts94 said:

It's a reference from the show Silicon Valley on HBO.

I've been told I need to watch that. Note to self...

I've met coders like that though. They just can't accept that other people have different preferences. Let your lead developer pick what they want and set a formatting function in your IDE. Then move onto more important things like actually coding.

3 hours ago, sozin said:

Post your code @MajorJuggler , I will see if I can get a better golfing score with Python ;)

upside is that if you're tricky 99% of simple tasks like this can be <5 lines.

Yeah I might need to bust out Perl though - Bob's score is pretty good right now

12 minutes ago, sozin said:

Yeah I might need to bust out Perl though - Bob's score is pretty good right now

Honestly though. Why do you care? If it takes you 3x as long to write code that is 1 line shorter, you missed the point. Code is supposed to make your life easier.

Golfing is just stunt programming for funsies. In all my "real" software I make try to make sure the code is maintainable, readable, tested, pragmatic, etc. 80 percent of the cost of software is maintaining it (DiMarco). H-e-l-l is other people's code, as they say.

So yeah in the real world you are totally right. The Pragmatic Programmer is a good guide for writing maintainable code.

Edited by sozin

I prefer racing for shortest run time. Number of lines/characters, variable name length, number of functions. This is all irrelevant at runtime. Maintainability then speed if that's significant. Decreasing a runtime from 5 milliseconds to 3 milliseconds doesn't seem like a big deal, but if you're running it a couple hundred million times per day then that adds up quick!

22 minutes ago, Killerardvark said:

I prefer racing for shortest run time. Number of lines/characters, variable name length, number of functions. This is all irrelevant at runtime. Maintainability then speed if that's significant. Decreasing a runtime from 5 milliseconds to 3 milliseconds doesn't seem like a big deal, but if you're running it a couple hundred million times per day then that adds up quick!

Yeah, there's some optimizations that can be made there. The first version is faster due to the geometric nature of convolution computation time, which is what I left in my executable code. It could be further optimized for speed, but running it 1000 vs 999 still only takes a couple milliseconds, so it doesn't really matter in this case.

For the rest of my code, it matters a lot, as I have a lot of calculations slowing stuff down my main MathWing 3.0 script. Hasn't been bad enough to get me to overhaul the whole thing yet though.

Edited by MajorJuggler

This thread gave me cancer....

This making code good is pretty antithetical to the thread. New challenge. Longest and/or most inefficient way to calculate final salvo.

2 minutes ago, jagsba said:

This making code good is pretty antithetical to the thread. New challenge. Longest and/or most inefficient way to calculate final salvo.

1. Roll final salvo once.

2. Make absolute blanketing declaration based on results.

3. Argue its accuracy for the rest of your life.

9 minutes ago, Killerardvark said:

1. Roll final salvo once.

2. Make absolute blanketing declaration based on results.

3. Argue its accuracy for the rest of your life.

Ah, the "I saw 3 dice beat 7 once" strategy