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

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 9.597778s 76 characters Submitted by jenseng on December 12th, 2013 16:23 +2

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

    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
  3. 3

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

    Runs in 9.37432s 79 characters Submitted by pczarn on December 12th, 2013 14:14 +1

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

    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]
  5. 5

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 9.672799s 80 characters Submitted by jenseng on December 12th, 2013 16:23 +1

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

    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

    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
  7. 7

    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
  8. 8

    D171590f55d7e5b649500bcc6e828f1e normal

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

    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
  9. 9

    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
  10. 10

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 11.308779s 99 characters Submitted by jenseng on December 11th, 2013 20:10 +1

    c=m=0;l=nil;quacks.split(?,).map{|d|t=DateTime.parse(d).to_time;c=l&&t-l<=2?c+1:1;l=t;m=c>m ?c:m};m
  11. 11

    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
  12. 12

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 11.712563s 108 characters Submitted by jenseng on December 11th, 2013 18:21 +1

    c=m=0;l=nil;quacks.split(?,).map{|d|t=DateTime.parse(d).to_time;c=[l&&t-l<=2?c+1:-1,1].max;l=t;m=c>m ?c:m};m
  13. 13

    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
  14. 14

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 11.783497s 116 characters Submitted by jenseng on December 11th, 2013 18:05 +1

    c=m=0
    l=nil
    quacks.split(?,).map{|d|
    t=DateTime.parse(d).to_time
    c=[l&&t-l<=2?c+1:-1,1].max
    l=t
    m=c>m ?c:m}
    m
  15. 15

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 11.517283s 119 characters Submitted by jenseng on December 11th, 2013 17:49 +1

    c=m=0
    l=nil
    quacks.split(", ").each{|d|
    t=DateTime.parse(d).to_time
    c=[l&&t-l<=2?c+1:-1,1].max
    l=t
    m=c>m ?c:m}
    m
  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

    D171590f55d7e5b649500bcc6e828f1e normal

    Runs in 10.904119s 123 characters Submitted by jenseng on December 11th, 2013 18:00 +1

    require 'time'
    c=m=0
    l=nil
    quacks.split(", ").each{|d|
    t=Time.parse(d)
    c=[l&&t-l<=2?c+1:-1,1].max
    l=t
    m=c>m ?c:m}
    m
  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

    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]
  20. 20

    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]
  21. 21

    3a9dd51ec1c341ca03a2cfac82cac1cd?d=https%3a%2f%2fidenticons.github.com%2fc379ff649730ae0eedf2ca808070bb6f

    Runs in 20.329985s 184 characters Submitted by mthelander on December 11th, 2013 20:38 +1

    require 'date'
    a = quacks.split(', ').map{|d|DateTime.parse(d)}
    (0...a.size).reduce{|m,i|
      j=i+1
      j+=1 while j < a.size && a[j] < a[j-1]+Rational(3,86400)
      (j-i>m) ? j-i : m
    }
  22. 22

    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
  23. 23

    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
  24. 24

    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
  25. 25

    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
  26. 26

    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
  27. 27

    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
  28. 28

    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
  29. 29

    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
  30. 30

    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

Sponsors

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