-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_doc.rb
64 lines (53 loc) · 2.03 KB
/
doc_doc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require 'doc_doc/version'
require 'doc_doc/horse_and_buggy'
require 'doc_doc/quarantine'
require 'doc_doc/house_call'
require 'doc_doc/patient'
require 'doc_doc/prescription'
require 'doc_doc/treatment'
require 'doc_doc/configuration'
module DocDoc
def self.prescription(config)
horse_and_buggy = HorseAndBuggy.new(config.throttle)
patients = []
treatments = []
patient_zero = Patient.new(config.danger_zone)
current_iteration = {
patient_zero: patient_zero,
patients: Quarantine.new(horse_and_buggy, patient_zero.home).patients,
treatments: []
}
current_iteration[:patients] = Quarantine.new(horse_and_buggy, current_iteration[:patient_zero].home).patients
treatments += current_iteration[:patients].map do |patient|
treat(current_iteration[:patient_zero].home, horse_and_buggy, patient)
end.compact
patients += current_iteration[:patients]
ill_patients = patients.select do |patient|
treatments.map(&:patient).include?(patient)
end
(1..config.crawling_options.max_spiderings).each do
outbreaks = (current_iteration[:patients] - ill_patients).map do |patient|
[patient, Quarantine.new(horse_and_buggy, patient.home).patients]
end
sub_treatments = outbreaks.flat_map do |sub_patient_zero, sub_sub_patients|
sub_sub_patients.map do |sub_patient|
treat(sub_patient_zero.home, horse_and_buggy, sub_patient)
end.compact
end
treatments += sub_treatments
current_iteration[:patients] = outbreaks.flat_map do |outbreak| outbreak[1] end
patients += current_iteration[:patients]
ill_patients = patients.select do |patient|
treatments.map(&:patient).include?(patient)
end
end
Prescription.new(treatments.sort_by(&:starting_location))
end
private
def self.treat(starting_place, horse_and_buggy, patient)
visit = HouseCall.new(horse_and_buggy, patient, starting_place)
visit.start
illness = visit.illness
Treatment.new(patient, illness, visit) if illness
end
end