Last active
April 12, 2022 14:55
-
-
Save jordanekay/cbdfb526e211e63f925a8415f664bc4f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum COVID {} | |
extension COVID { | |
struct Conditions { | |
let caseCount: Int | |
let hospitalizationCount: Int | |
let caseIncrease: Float | |
} | |
public struct Response { | |
let level: Level | |
let criteria: Criteria | |
init(_ level: Level) { | |
self.level = level | |
criteria = .init(level) | |
} | |
} | |
} | |
extension COVID.Conditions { | |
func meetCriteria(for response: COVID.Response) -> Bool { | |
let criteria = response.criteria | |
let satisfactions = [ | |
criteria.caseRange.contains(caseCount), | |
criteria.hospitalizationRange.contains(hospitalizationCount), | |
criteria.caseIncreaseRequirement(caseIncrease) | |
] | |
let satisfiedCount = satisfactions.filter { $0 }.count | |
return satisfiedCount >= 2 | |
} | |
} | |
extension COVID.Response { | |
enum Level: Int, CaseIterable { | |
case allClear = 1 | |
case maskPrecautions = 2 | |
case caution = 3 | |
case extremeCaution = 4 | |
} | |
struct Criteria { | |
let caseRange: CountRange | |
let hospitalizationRange: CountRange | |
var caseIncreaseRequirement: (Float) -> Bool = { $0 > 0.5 } | |
} | |
} | |
protocol CountRange { | |
func contains(_ count: Int) -> Bool | |
} | |
extension Range: CountRange where Bound == Int {} | |
extension PartialRangeUpTo: CountRange where Bound == Int {} | |
extension PartialRangeFrom: CountRange where Bound == Int {} | |
extension COVID.Response.Level { | |
init(_ conditions: COVID.Conditions) { | |
let responses = Self.allCases.map(COVID.Response.init) | |
self = responses.first(where: conditions.meetCriteria)!.level | |
} | |
} | |
extension COVID.Response.Criteria { | |
init(_ level: COVID.Response.Level) { | |
switch level { | |
case .allClear: | |
caseRange = ..<100 | |
hospitalizationRange = ..<50 | |
caseIncreaseRequirement = { $0 < 0.5 } | |
case .maskPrecautions: | |
caseRange = ..<225 | |
hospitalizationRange = ..<100 | |
case .caution: | |
caseRange = 225..<500 | |
hospitalizationRange = 100..<500 | |
case .extremeCaution: | |
caseRange = 500... | |
hospitalizationRange = 500... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment