View 3 comments about this challenge

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

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

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+192,2].to_i + quacks[i+195,2].to_i * 60 + quacks[i+198,2].to_i * 60 * 60 + quacks[i+1911,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

quacks = quacks.split(?,) quacks[quacks.length1].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

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

quacks = quacks.split(?,) quacks[quacks.length1].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

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

res = Hash.new(0) on_streak = false as_i = >(x){x[8..1].tr('^09','')[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

last_quack = lng = cur = i = 1 while i < quacks.length  1 #  1 to take into account a trailing quote mark: " this_quack = quacks[i+192,2].to_f + quacks[i+195,2].to_f * 60.0 + quacks[i+198,2].to_f * 60.0 * 60.0 + quacks[i+1911,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

d=nil;i=>s{s.tr('^09','')[2,2].to_i};quacks.split(?,).each_cons(2).with_object(Hash.new(2)){(a,b),hi[b]i[a]<3?d ?h[d]+=1:d=a :d=nil}.values.sort[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_i1] * 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

i=>s{s.tr('^09','')[2,2].to_i};Hash[*quacks.split(?,).each_cons(2).chunk{a,bi[b]i[a]<3}.select{x,_x}.max_by{_,yy.size}].map{k,vv.size+1}[0]

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

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

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

c=1;quacks.split(?\s).each_slice(2).map{_,tTime.utc(1,1,1,*t.split(?:)).to_i}.each_cons(2).map{a,bc=ba<3?c+1:1}.max

quacks.split(?,).map{dTime.utc(*d.scan(/\d+/))}.each_cons(2).chunk{a,bba<3}.map{a,ba&&b.size0}.max+1

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

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

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

c,m,l=0,1;quacks.split(?,).map{dd=DateTime.parse d;c=l&&dl<3e5?c+1:1;l=d;c>m&&m=c};m

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

i = 20 lng = cur = 1 last_quack = DateTime.parse(quacks[0, 20]) while i < quacks.length1 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

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

c,l=0;quacks.split(?,).map{dd=DateTime.parse d;c,l=l&&dl<3e5?c+1:1,d}.max[0]

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

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

require 'time' quacks = quacks.split(?,) quacks[quacks.length1].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

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
Comments (3)
joekarma, the third set of test data violates your assumption that all quacks take place in the same month.
posted by nbaum, 12 Dec 09:54
You are correct. I posted it to remain competitive with the then fastest solution which assumed all quacks took place in the same minute, but still returned the "right" answers. I updated my solution afterward to work up to the year. Truth be told, it was a way of hedging my bets on what will pass the final round; my completely Time based 5.204928s solution was also the fastest in its arena at the time I posted it, but it as since been superseded by pczam's blazingly quick one. Hopefully the others are filtered out and his wins.
posted by joekarma, 12 Dec 16:20
i've got an even shorter solution that works, though it hits a timeout so it gets rejected: c=l=0;quacks.gsub(/^,/).map{d=DateTime.parse$';c=dl<3e5?c+1:1;l=d;c}.max and even shorter still (though it would fail on more robust test cases): c=l=0;quacks.gsub(?,).map{d=DateTime.parse$';c=dl<3e5?c+1:1;l=d;c}.max
posted by jenseng, 12 Dec 20:22