This is the last time I play golf. It’s addictive and I should be
working.
a=$<.read.split(“\n”);s=%w{d c h
s};c=‘AKQJT9’;t=a.shift;u=(t[0]+32).chr;i=s.index(u);v=s[i-2];w=[1];puts
t,a.sort_by{|k|(k=~/J[#{u+v}]/?0:99)+(s.index(k[1,1])-i)%4*10+c.index(k[0,1])}
OOPS! I was just looking at ways I could golf my solution and I realized
I
had a big bug:
replace
VAL = [?A,?Q,?K,?J,?T,?9]
with
VAL = [?A,?K,?Q,?J,?T,?9]
Got to get back to work. 166 chars;
a=$<.readlines;s=‘dchs’;c=‘AKQJT9’;t=a.shift;u=(t[0]+32).chr;i=s.index(u);v=s[i-2].chr;puts
t,a.sort_by{|k|(k=~/J[#{u+v}]/?0:50)+(s.index(k[1])-i)%4*10+c.index(k[0])}
Got to get back to work. 166 chars;
a=$<.readlines;s=‘dchs’;c=‘AKQJT9’;t=a.shift;u=(t[0]+32).chr;i=s.index(u);v=s[i-2].chr;puts
t,a.sort_by{|k|(k=~/J[#{u+v}]/?0:50)+(s.index(k[1])-i)%4*10+c.index(k[0])}
Brilliant. I’m extremely impressed. I learned quite a bit from it too,
which is one of the reasons I like code golfing so much. Plus it is
fun and rather addictive, as you have learned.
For those wondering, here is what I learned from David’s changes to my
code:
The extra newlines don’t need to be cleaned out from the input
since “puts” seems to ignore them.
For compactness it is better to use strings rather than %w{} arrays.
Ruby’s array and string indexing already takes care of
“wrap-around” when indexing since negative indexes work. So given a
string s of length 4, s[(i+2)%4] and s[i-2] are equivalent for values
of i from 0 to 5.
You can get the index of either a string or a fixnum representing a
character using String#index.
Using % on a negative number yields a positive number, which is
cleverly used above in calculating the weight. My math is rusty I
guess, or in fact, I’m not sure I ever learned what performing modulus
on a negative number is supposed to return. But I Googled it and
learned now.
But there is a problem in my code (both the above and the longer
version) which I just noticed (and that Zed brought up earlier): if a
suit is missing in certain cases the suits are not interlaced
properly. I’m wondering if I will bother going back to the drawing
board or not
unless cards.any? { |card| card[1] == o[15] }
# if not replace second suit by the last
o[14, 12] = o[36, 12]
end
puts trump, cards.sort_by { |card| o.index(card) }
second and fourth suits if the hand was void in the second suit. Given
that adjustment, any combination of suits in the hand should yield
properly alternating colors.
I did it kind of bottom-up to your approach’s top-down – starting
with array of the suits present in the hand, in arbitrary order, I
moved the trump suit to the front. Then, if there were three or more
suits, if the second suit was the same color as the trump suit, I
swapped the second and third suits.
A couple of my early attempts depended on the idea that the trump suit
absolutely determined the ranking of the cards, independent of knowing
anything else. It doesn’t work, though.
…
Ordering the hand can’t come without considering what suits are present.
I addressed this by generating a suit order in alternating colors
starting with the trump suit. Then some hand checking of the various
possibilities showed me that the only adjustment needed was to swap the
second and fourth suits if the hand was void in the second suit. Given
that adjustment, any combination of suits in the hand should yield
properly alternating colors.