The Ruby Game

## the question is can you write the fastest or shortest code?
## do you dare to take on your peers in a game of speed?

View 3 comments about this challenge

  1. 1

    3bb15c29d3bf26cdad6c344b30bd9a29?d=https%3a%2f%2fidenticons.github.com%2f102e7f7a2a9908f881b608f4aa2afbb0

    Runs in 1.156151s 456 characters Submitted by tommetge on December 12th, 2013 16:59 +1

    longest_streak = 0
    streak = 0
    last = nil
    quacks.split(", ").each do |quack|
      minute = quack[14..15].to_i
      second = quack[17..18].to_i
      if last
        m_diff = minute >= last[0] ? (minute - last[0]) : (minute + 60 - last[0])
        diff = m_diff + second - last[1]
        if diff > 2
          longest_streak = streak > longest_streak ? streak : longest_streak
          streak = 0
        end
      end
      streak += 1
      last = [minute, second]
    end
    longest_streak
  2. 2

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 1.339661s 536 characters Submitted by joekarma on December 12th, 2013 06:40 +1

    lng = cur = last_quack = 1
    quacks.split(?,).each { |q|
      this_quack = q[18,2].to_i + # First quack has a quote before date, others have a space. Offset is consistent regardless.
                   q[15,2].to_i * 60 +
                   q[12,2].to_i * 60 * 60 + 
                   q[9,2].to_i * 60 * 60 * 24 # assume all quacks take place in same month. stupid, but...
      if (this_quack - last_quack) < 3
        cur += 1
      else
        if cur > lng
          lng = cur
        end
        cur = 1
      end
      last_quack = this_quack
    }
    cur > lng ? cur : lng
  3. 3

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 1.378388s 533 characters Submitted by joekarma on December 12th, 2013 06:10 +1

    last_quack = lng = cur = i = 1
    while i < quacks.length - 1 # - 1 to take into account a trailing quote mark: "
      i+=21
      this_quack = quacks[i+19-2,2].to_i +
                   quacks[i+19-5,2].to_i * 60 +
                   quacks[i+19-8,2].to_i * 60 * 60 + 
                   quacks[i+19-11,2].to_i * 60 * 60 * 24 # assume all quacks take place in same month. stupid, but...
      if (this_quack - last_quack) <= 2
        cur += 1
      else
        lng = cur if cur > lng
        cur = 1
      end
      last_quack = this_quack
    end
    cur > lng ? cur : lng
  4. 4

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 1.38856s 555 characters Submitted by joekarma on December 12th, 2013 05:11 +1

    quacks = quacks.split(?,)
    quacks[quacks.length-1].sub!(?","") # input is wrapped in quotes. probably a bug.
    
    last_quack = lng = cur = 1
    
    quacks.each { |quack|
      this_quack = quack[-2,2].to_i +
                   quack[-5,2].to_i * 60 +
                   quack[-8,2].to_i * 60 * 60 + 
                   quack[-11,2].to_i * 60 * 60 * 24 # assume all quacks take place in same month. stupid, but...
      
      if (this_quack - last_quack) <= 2
        cur += 1
      else
        lng = cur if cur > lng
        cur = 1
      end
    
      last_quack = this_quack
    }
    
    [cur, lng].max
  5. 5

    3bb15c29d3bf26cdad6c344b30bd9a29?d=https%3a%2f%2fidenticons.github.com%2f102e7f7a2a9908f881b608f4aa2afbb0

    Runs in 1.468328s 435 characters Submitted by tommetge on December 12th, 2013 16:57 +1

    longest_streak = 0
    streak = 0
    last = nil
    quacks.split(", ").each do |quack|
      minute = quack[14..15].to_i
      second = quack[17..18].to_i
      if last
        m_diff = minute >= last[0] ? (minute - last[0]) : (minute + 60 - last[0])
        diff = m_diff + second - last[1]
        if diff > 2
          longest_streak = [streak, longest_streak].max
          streak = 0
        end
      end
      streak += 1
      last = [minute, second]
    end
    longest_streak
  6. 6

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 1.497469s 744 characters Submitted by joekarma on December 12th, 2013 04:43 +1

    quacks = quacks.split(?,)
    quacks[quacks.length-1].sub!(?","") # input is wrapped in quotes. probably a bug.
    
    i = lng = cur = 1
    last_quack = quacks[0][-2,2].to_i +
                 quacks[0][-5,2].to_i * 60 +
                 quacks[0][-8,2].to_i * 60 * 60 + 
                 quacks[0][-11,2].to_i * 60 * 60 * 24 # assume all quacks take place in same month
    
    while i < quacks.length
      this_quack = quacks[i][-2,2].to_i +
                   quacks[i][-5,2].to_i * 60 +
                   quacks[i][-8,2].to_i * 60 * 60 + 
                   quacks[i][-11,2].to_i * 60 * 60 * 24 # heh.
      
      if (this_quack - last_quack) <= 2
        cur += 1
      else
        lng = cur if cur > lng
        cur = 1
      end
      i += 1
      last_quack = this_quack
    end 
    
    [cur, lng].max
  7. 7

    3bb15c29d3bf26cdad6c344b30bd9a29?d=https%3a%2f%2fidenticons.github.com%2f102e7f7a2a9908f881b608f4aa2afbb0

    Runs in 1.572738s 473 characters Submitted by tommetge on December 12th, 2013 16:27 +1

    longest_streak = 0
      streak = []
      quacks.split(", ").each do |quack|
        minute = quack[14..15].to_i
        second = quack[17..18].to_i
        last = streak.last
        if last
          m_diff = minute >= last[0] ? (minute - last[0]) : (minute + 60 - last[0])
          diff = m_diff + second - last[1]
          if diff > 2
            longest_streak = [streak.count, longest_streak].max
            streak = []
          end
        end
        streak << [minute, second]
      end
      longest_streak
  8. 8

    08fd3c9d074526abb96bbd3081931344?d=https%3a%2f%2fidenticons.github.com%2fbe3e0f50b57cda7f0d81b1ff2f6379fb

    Runs in 1.595587s 374 characters Submitted by kotay on December 11th, 2013 10:51 +1

    res            = Hash.new(0)
    on_streak      = false
    as_i =         ->(x){x[8..-1].tr('^0-9','')[-2, 2].to_i}
    quacks.split(?,).each_cons(2).each do |t1,t2|
      if as_i[t2]-as_i[t1] <= 2
        if on_streak
          res[on_streak] +=1
        else
          res[t1.to_sym] = 2
          on_streak      = t1.to_sym
        end
      else
        on_streak = false
      end
    end
    res.values.sort.last
  9. 9

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 1.620341s 547 characters Submitted by joekarma on December 12th, 2013 06:15 +1

    last_quack = lng = cur = i = 1
    while i < quacks.length - 1 # - 1 to take into account a trailing quote mark: "
      this_quack = quacks[i+19-2,2].to_f +
                   quacks[i+19-5,2].to_f * 60.0 +
                   quacks[i+19-8,2].to_f * 60.0 * 60.0 + 
                   quacks[i+19-11,2].to_f * 60.0 * 60.0 * 24.0 # assume all quacks take place in same month. stupid, but...
      if (this_quack - last_quack) <= 2
        cur += 1
      else
        lng = cur if cur > lng
        cur = 1
      end
      i += 21
      last_quack = this_quack
    end
    cur > lng ? cur : lng
  10. 10

    08fd3c9d074526abb96bbd3081931344?d=https%3a%2f%2fidenticons.github.com%2fbe3e0f50b57cda7f0d81b1ff2f6379fb

    Runs in 1.904653s 154 characters Submitted by kotay on December 11th, 2013 11:27 +2

    d=nil;i=->s{s.tr('^0-9','')[-2,2].to_i};quacks.split(?,).each_cons(2).with_object(Hash.new(2)){|(a,b),h|i[b]-i[a]<3?d ?h[d]+=1:d=a :d=nil}.values.sort[-1]
  11. 11

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 2.329325s 680 characters Submitted by joekarma on December 12th, 2013 07:57 +1

    months = [0,31,59,90,120,151,181,212,243,273,304,334,365] # fails to account for leap years. oh well. faster.
    longest = current = last_quack = 1
    
    quacks.split(?,).each { |q|
      this_quack = q[18,2].to_i + #sec
                   q[15,2].to_i * 60 + #min
                   q[12,2].to_i * 60 * 60 + #hr
                   q[9,2].to_i * 60 * 60 * 24 + #day
                   months[q[6,2].to_i-1] * 60 * 60 * 24 + #month
                   q[1,4].to_i * 60 * 60 * 24 * 365 #year
      if (this_quack - last_quack) < 3
        current += 1
      else
        if current > longest
          longest = current
        end
        current = 1
      end
      last_quack = this_quack
    }
    current > longest ? current : longest
  12. 12

    08fd3c9d074526abb96bbd3081931344?d=https%3a%2f%2fidenticons.github.com%2fbe3e0f50b57cda7f0d81b1ff2f6379fb

    Runs in 2.355438s 151 characters Submitted by kotay on December 12th, 2013 07:57 +1

    i=->s{s.tr('^0-9','')[-2,2].to_i};Hash[*quacks.split(?,).each_cons(2).chunk{|a,b|i[b]-i[a]<3}.select{|x,_|x}.max_by{|_,y|y.size}].map{|k,v|v.size+1}[0]
  13. 13

    263ec107ea95c0df1c3d107f96ec079e?d=https%3a%2f%2fidenticons.github.com%2f83f60c740a596e51ddfdff85deef1770

    Runs in 2.441996s 93 characters Submitted by pczarn on December 13th, 2013 16:48 +1

    m=c=l=0;quacks.scan(/\d+/).each_slice(6){|d|d=Time.utc(*d).to_i;c=d-l<3?c+1:1;l=d;c>m&&m=c};m
  14. 14

    263ec107ea95c0df1c3d107f96ec079e?d=https%3a%2f%2fidenticons.github.com%2f83f60c740a596e51ddfdff85deef1770

    Runs in 2.627949s 90 characters Submitted by pczarn on December 13th, 2013 16:20 +1

    c=l=0;quacks.scan(/\d+/).each_slice(6).map{|d|d=Time.utc(*d).to_i;c=d-l<3?c+1:1;l=d;c}.max
  15. 15

    263ec107ea95c0df1c3d107f96ec079e?d=https%3a%2f%2fidenticons.github.com%2f83f60c740a596e51ddfdff85deef1770

    Runs in 2.84043s 106 characters Submitted by pczarn on December 13th, 2013 15:21 +1

    c=1;quacks.split(', ').map{|d|Time.utc(*d.split(/[\-:\s]/)).to_i}.each_cons(2).map{|a,b|c=b-a<3?c+1:1}.max
  16. 16

    263ec107ea95c0df1c3d107f96ec079e?d=https%3a%2f%2fidenticons.github.com%2f83f60c740a596e51ddfdff85deef1770

    Runs in 2.919725s 120 characters Submitted by pczarn on December 13th, 2013 15:58 +1

    c=1;quacks.split(?\s).each_slice(2).map{|_,t|Time.utc(1,1,1,*t.split(?:)).to_i}.each_cons(2).map{|a,b|c=b-a<3?c+1:1}.max
  17. 17

    263ec107ea95c0df1c3d107f96ec079e?d=https%3a%2f%2fidenticons.github.com%2f83f60c740a596e51ddfdff85deef1770

    Runs in 3.166908s 109 characters Submitted by pczarn on December 12th, 2013 13:19 +1

    quacks.split(?,).map{|d|Time.utc(*d.scan(/\d+/))}.each_cons(2).chunk{|a,b|b-a<3}.map{|a,b|a&&b.size||0}.max+1
  18. 18

    5191c46958937376eaad2da13b5b520b?d=https%3a%2f%2fidenticons.github.com%2fc437908f88de917f849278f61b7dd318

    Runs in 3.435634s 128 characters Submitted by tdg5 on December 20th, 2013 11:42 +2

    max=c=0;quacks.scan(/\d+/).map!(&:to_i).each_slice(6).map{|g| Time.new(*g)}.inject{|x,y|y-x<=2?(c+=1)>max&&max+=1:c=0;y};max+1
  19. 19

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 5.204938s 392 characters Submitted by joekarma on December 12th, 2013 04:59 +1

    result = quacks.scan(/\d+/)
      .enum_for(:each_slice, 6).lazy
      .map { |parts| Time.new(*parts.map(&:to_i)) }
      .enum_for(:each_cons, 2).lazy
      .map { |ts|
        ts[1] - ts[0]
      }.reduce({:max=>1,:cur=>1}) { |m, c|
        if (c <= 2)
          m[:cur] += 1
        else
          m[:max] = m[:cur] if m[:cur] > m[:max]
          m[:cur] = 1
        end
        
        m
      }
    
    [result[:max], result[:cur]].max
  20. 20

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 5.756927s 413 characters Submitted by joekarma on December 12th, 2013 03:36 +1

    result = quacks.scan(/\d+/)
      .enum_for(:each_slice, 6).lazy
      .map { |parts| DateTime.new(*parts.map(&:to_i)) }
      .enum_for(:each_cons, 2).lazy
      .map { |ts|
        (ts[1] - ts[0]) * 24 * 60 * 60
      }.reduce({:max=>1,:cur=>1}) { |m, c|
        if (c <= 2)
          m[:cur] += 1
        else
          m[:max] = m[:cur] if m[:cur] > m[:max]
          m[:cur] = 1
        end
        
        m
      }
    
    [result[:max], result[:cur]].max
  21. 21

    D7c285a1be73b554c57e39cfa93a14e9?d=https%3a%2f%2fidenticons.github.com%2f48bbab63ca0f722d32298573fff263ab

    Runs in 6.898606s 396 characters Submitted by darkphnx on December 11th, 2013 10:49 0

    require 'time'
    
    longest_streak = 1
    all_quacks = quacks.split(', ').map{|t| Time.parse(t)}
    
    this_streak = 1
    all_quacks.each_cons(2) do |(quack1, quack2)|
      if (quack2 - quack1) <= 3
        this_streak += 1
      else
        longest_streak = this_streak if this_streak > longest_streak
        this_streak = 1
      end
    end
    longest_streak = this_streak if this_streak > longest_streak
    
    longest_streak
  22. 22

    263ec107ea95c0df1c3d107f96ec079e?d=https%3a%2f%2fidenticons.github.com%2f83f60c740a596e51ddfdff85deef1770

    Runs in 9.212714s 88 characters Submitted by pczarn on December 12th, 2013 13:51 +1 2 similars

    c,m,l=0,1;quacks.split(?,).map{|d|d=DateTime.parse d;c=l&&d-l<3e-5?c+1:1;l=d;c>m&&m=c};m
  23. 23

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 9.339401s 395 characters Submitted by joekarma on December 11th, 2013 18:19 +1

    quacks = quacks.split(", ").map { |dt| DateTime.parse(dt) }
    
    quack_streaks = quacks.enum_for(:each_cons, 2).reduce([]) { |runs, quacks|
      difference_in_days = quacks[1] - quacks[0]
      difference_in_seconds = difference_in_days * 24 * 60 * 60
      mere_seconds_apart = difference_in_seconds <= 2
      
      if mere_seconds_apart
        runs[-1] += 1
      else
        runs.push(1)
      end
      
      runs
    }.max
  24. 24

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 9.419469s 320 characters Submitted by joekarma on December 12th, 2013 04:08 +1

    i = 20
    lng = cur = 1
    last_quack = DateTime.parse(quacks[0, 20])
    
    while i < quacks.length-1
      this_quack = DateTime.parse(quacks[i+2, 19])
      if (this_quack - last_quack)*24*60*60 <= 2
        cur += 1
      else
        lng = cur if cur > lng
        cur = 1
      end
      i += 21
      last_quack = this_quack
    end 
    
    [cur, lng].max
  25. 25

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 9.538643s 92 characters Submitted by jenseng on December 12th, 2013 04:09 +3 1 similar

    c,m,l=0,1;quacks.split(?,).map{|d|d=DateTime.parse d;c=l&&d-l<3/8.64e4?c+1:1;l=d;c>m&&m=c};m
  26. 26

    263ec107ea95c0df1c3d107f96ec079e?d=https%3a%2f%2fidenticons.github.com%2f83f60c740a596e51ddfdff85deef1770

    Runs in 9.664716s 80 characters Submitted by pczarn on December 12th, 2013 14:32 +1

    c,l=0;quacks.split(?,).map{|d|d=DateTime.parse d;c,l=l&&d-l<3e-5?c+1:1,d}.max[0]
  27. 27

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 10.296783s 757 characters Submitted by joekarma on December 12th, 2013 02:04 +1

    current_streak = 1
    longest_streak = 1
    
    while comma_place = quacks.index(?,)
      this_quack = quacks[0..comma_place - 1].split(/[- :]/)
      this_quack = DateTime.new(*this_quack.map(&:to_i))
      quacks[0..comma_place + 1] = ""
    
      next_comma_place = quacks.index(?,) || quacks.length
      next_quack = quacks[0..next_comma_place - 1].split(/[- :]/)
      next_quack = DateTime.new(*next_quack.map(&:to_i))
    
      difference_in_days = next_quack - this_quack
      difference_in_seconds = difference_in_days * 24 * 60 * 60
      mere_seconds_apart = difference_in_seconds <= 2
    
      if mere_seconds_apart
        current_streak += 1
      else
        longest_streak = [longest_streak, current_streak].max
        current_streak = 1
      end
    end
    
    [current_streak, longest_streak].max
  28. 28

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 10.333577s 77 characters Submitted by jenseng on December 12th, 2013 20:44 +1

    c=l=0;quacks.gsub(/[^,]+/).map{d=DateTime.parse$&;c=d-l<3e-5?c+1:1;l=d;c}.max
  29. 29

    Bccf6cf98f0946348665f57e9e402312?d=https%3a%2f%2fidenticons.github.com%2f09eca040ba5cd8dfb572a0cf57f3c9c5

    Runs in 10.424644s 370 characters Submitted by joekarma on December 12th, 2013 04:29 +1

    require 'time'
    
    quacks = quacks.split(?,)
    quacks[quacks.length-1].sub!(?","")
    
    i = lng = cur = 1
    last_quack = Time.parse(quacks[0])
    
    while i < quacks.length
      this_quack = Time.parse(quacks[i])
      if (this_quack - last_quack) <= 2
        cur += 1
      else
        lng = cur if cur > lng
        cur = 1
      end
      i += 1
      last_quack = this_quack
    end 
    
    [cur, lng].max
  30. 30

    Ca11ae08feb22522f7651533ec8d311c?d=https%3a%2f%2fidenticons.github.com%2f9efa4cfba721ce5957092f03a92255f4

    Runs in 10.623041s 403 characters Submitted by dipth on December 12th, 2013 10:10 +1

    require 'time'
    
    longest_streak = 0
    
    quacks.split(",").inject([nil, 1]) do |memo,quack|
      current_quack = Time.parse(quack)
      last_quack    = memo[0]
      streak        = memo[1]
      
      if last_quack.nil? || current_quack - last_quack <= 2
        streak += 1 
      else
        streak = 1
      end
      
      longest_streak = streak if streak > longest_streak
      
      [current_quack, streak]
    end
    
    longest_streak

Sponsors

aTech Media provide development time & hosting resources for this site. Why not check out some of their other things?