Skip to content

Instantly share code, notes, and snippets.

@lukerandall
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukerandall/6d12414029e9a88e0051 to your computer and use it in GitHub Desktop.
Save lukerandall/6d12414029e9a88e0051 to your computer and use it in GitHub Desktop.
Distressed ducks
require 'time'
start_time = Time.now
readings, streaks, latest_streak = File.open('quacklog.txt').gets.split(', ').collect{|t| Time.parse(t)}, [], 0
readings.each_with_index do |t, i|
next if i == 0
if t - readings[i-1] <= 2.0
latest_streak += 1
else
streaks << latest_streak if latest_streak >= 3
latest_streak = 0
end
end
streaks << latest_streak
puts streaks.max
puts "Duck Detective took #{ Time.now - start_time } seconds to find the worst distress call"
require 'time'
class QuackAnalysis
attr_reader :quacks
attr_accessor :distress_calls
def initialize(raw_quacks)
@quacks = sorted_quacks(raw_quacks)
@distress_calls = {}
identify_distress_calls
end
def identify_distress_calls
previous_previous_time = quacks[0]
previous_time = quacks[1]
last_distress_call = nil
quacks.each_with_index do |current_time, index|
# Skip the first two - analysis is only concerned with distress calls
# which can only be gauged from the third element on.
next if index < 2
if current_time - previous_previous_time < 2
last_distress_call ||= previous_previous_time
unless @distress_calls.include?(last_distress_call)
@distress_calls[last_distress_call] = [
previous_previous_time, previous_time]
end
@distress_calls[last_distress_call] << current_time
else
last_distress_call = nil
end
previous_time = current_time
end
end
def most_quacks
worst_call = distress_calls.max_by { |k, v| v }
if worst_call
puts "The distress call starting at #{ worst_call[0] } has #{ worst_call[1].length } distress calls"
else
puts "There weren't any distress calls"
end
end
private
def sorted_quacks(quacks)
parsed_quacks = quacks.map { |q| Time.parse(q) }
parsed_quacks.sort
end
end
quacks = File.open('quacklog.txt').gets.split(', ')
start_time = Time.now
ducktective = QuackAnalysis.new(quacks)
# ducktective.distress_calls
puts ducktective.most_quacks
puts "Duck Detective took #{ Time.now - start_time } seconds to find the worst distress call"
2014-09-26 11:20:21, 2014-09-26 11:20:23, 2014-09-26 11:20:25, 2014-09-26 11:20:46, 2014-09-26 11:20:48, 2014-09-26 11:20:50, 2014-09-26 11:20:51, 2014-09-26 11:20:53, 2014-09-26 11:20:54, 2 014-09-26 11:21:20, 2014-09-26 11:21:21, 2014-09-26 11:21:22, 2014-09-26 11:21:26, 2014-09-26 11:21:35, 2014-09-26 11:21:36, 2014-09-26 11:21:41, 2014-09-26 11:21:42, 2014-09-26 11:21:44, 20 14-09-26 11:21:45, 2014-09-26 11:22:14, 2014-09-26 11:22:16, 2014-09-26 11:22:17, 2014-09-26 11:22:18, 2014-09-26 11:22:19, 2014-09-26 11:22:20, 2014-09-26 11:22:21, 2014-09-26 11:22:22, 201 4-09-26 11:22:24, 2014-09-26 11:22:25, 2014-09-26 11:22:27, 2014-09-26 11:22:28, 2014-09-26 11:22:50, 2014-09-26 11:22:52, 2014-09-26 11:22:54, 2014-09-26 11:22:55, 2014-09-26 11:23:04, 2014 -09-26 11:23:11, 2014-09-26 11:23:12, 2014-09-26 11:23:14, 2014-09-26 11:23:15, 2014-09-26 11:23:17, 2014-09-26 11:23:19, 2014-09-26 11:23:40, 2014-09-26 11:23:42, 2014-09-26 11:23:43, 2014- 09-26 11:23:44, 2014-09-26 11:24:11, 2014-09-26 11:24:12, 2014-09-26 11:24:14, 2014-09-26 11:24:16, 2014-09-26 11:24:18, 2014-09-26 11:24:19, 2014-09-26 11:24:20, 2014-09-26 11:24:22, 2014-0 9-26 11:24:24, 2014-09-26 11:24:26, 2014-09-26 11:24:28, 2014-09-26 11:24:52, 2014-09-26 11:24:53, 2014-09-26 11:24:54, 2014-09-26 11:25:14, 2014-09-26 11:25:23, 2014-09-26 11:25:24, 2014-09 -26 11:25:29, 2014-09-26 11:25:31, 2014-09-26 11:25:32, 2014-09-26 11:25:34, 2014-09-26 11:25:53, 2014-09-26 11:25:55, 2014-09-26 11:26:11, 2014-09-26 11:26:12, 2014-09-26 11:26:14, 2014-09- 26 11:26:15, 2014-09-26 11:26:16, 2014-09-26 11:26:18, 2014-09-26 11:26:19, 2014-09-26 11:26:21, 2014-09-26 11:26:23, 2014-09-26 11:26:25, 2014-09-26 11:26:27, 2014-09-26 11:26:29, 2014-09-2 6 11:26:31, 2014-09-26 11:26:32, 2014-09-26 11:26:34, 2014-09-26 11:26:58, 2014-09-26 11:26:59, 2014-09-26 11:27:00, 2014-09-26 11:27:02, 2014-09-26 11:27:03, 2014-09-26 11:27:05, 2014-09-26 11:27:07, 2014-09-26 11:27:09, 2014-09-26 11:27:24, 2014-09-26 11:27:25, 2014-09-26 11:27:27, 2014-09-26 11:27:28, 2014-09-26 11:27:31, 2014-09-26 11:27:33, 2014-09-26 11:27:35, 2014-09-26 11:27:37, 2014-09-26 11:27:38, 2014-09-26 11:27:40, 2014-09-26 11:27:41, 2014-09-26 11:27:42, 2014-09-26 11:27:44, 2014-09-26 11:27:45, 2014-09-26 11:27:47, 2014-09-26 11:27:57, 2014-09-26 1 1:28:20, 2014-09-26 11:28:21, 2014-09-26 11:28:23, 2014-09-26 11:28:25, 2014-09-26 11:28:26, 2014-09-26 11:28:38, 2014-09-26 11:28:40, 2014-09-26 11:28:42, 2014-09-26 11:28:44, 2014-09-26 11 :28:46, 2014-09-26 11:28:47, 2014-09-26 11:28:49, 2014-09-26 11:28:51, 2014-09-26 11:28:52, 2014-09-26 11:28:53, 2014-09-26 11:29:09, 2014-09-26 11:29:10, 2014-09-26 11:29:11, 2014-09-26 11: 29:40, 2014-09-26 11:29:42, 2014-09-26 11:29:48, 2014-09-26 11:29:50, 2014-09-26 11:29:51, 2014-09-26 11:29:55, 2014-09-26 11:29:56, 2014-09-26 11:29:59, 2014-09-26 11:30:00, 2014-09-26 11:3 0:01, 2014-09-26 11:30:02, 2014-09-26 11:30:03, 2014-09-26 11:30:04, 2014-09-26 11:30:06, 2014-09-26 11:30:28, 2014-09-26 11:30:30, 2014-09-26 11:30:31, 2014-09-26 11:30:33, 2014-09-26 11:30 :34, 2014-09-26 11:30:35, 2014-09-26 11:30:44, 2014-09-26 11:30:45, 2014-09-26 11:30:47, 2014-09-26 11:30:59, 2014-09-26 11:31:01, 2014-09-26 11:31:03, 2014-09-26 11:31:04, 2014-09-26 11:31: 05, 2014-09-26 11:31:07, 2014-09-26 11:31:09, 2014-09-26 11:31:11, 2014-09-26 11:31:12, 2014-09-26 11:31:13, 2014-09-26 11:31:14, 2014-09-26 11:31:16, 2014-09-26 11:31:32, 2014-09-26 11:31:3 3, 2014-09-26 11:31:35, 2014-09-26 11:31:37, 2014-09-26 11:31:39, 2014-09-26 11:31:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment