Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Add clock entry using Measurement #10

Merged
merged 2 commits into from
Dec 28, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CodeChallenge.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
9489866A1BE5CFB000D34976 /* CodeChallengeType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 948986691BE5CFB000D34976 /* CodeChallengeType.swift */; };
948986701BE5D28500D34976 /* ExampleCodeChallenge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9489866F1BE5D28500D34976 /* ExampleCodeChallenge.swift */; };
94FB98CA1BE1B5D800228845 /* TwoSumChallenge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94FB98C91BE1B5D800228845 /* TwoSumChallenge.swift */; };
D5D15B361E0C966A00CC9B1C /* felixdumitClockEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5D15B351E0C966A00CC9B1C /* felixdumitClockEntry.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -79,6 +80,7 @@
948986691BE5CFB000D34976 /* CodeChallengeType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeChallengeType.swift; sourceTree = "<group>"; };
9489866F1BE5D28500D34976 /* ExampleCodeChallenge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExampleCodeChallenge.swift; sourceTree = "<group>"; };
94FB98C91BE1B5D800228845 /* TwoSumChallenge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TwoSumChallenge.swift; sourceTree = "<group>"; };
D5D15B351E0C966A00CC9B1C /* felixdumitClockEntry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = felixdumitClockEntry.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -117,6 +119,7 @@
2EB83A7F1E03290400FAAB32 /* EthanSchatzline-ClockEntry.swift */,
1B31586E1E02FC7E0052ACBD /* FlavioSilverioClockEntry.swift */,
7B925E511E05AFFF0009B0CD /* matthijsClockEntry.swift */,
D5D15B351E0C966A00CC9B1C /* felixdumitClockEntry.swift */,
);
path = Entries;
sourceTree = "<group>";
Expand Down Expand Up @@ -377,6 +380,7 @@
94350F741BE5F04A003592FB /* IanKeen.swift in Sources */,
94350F761BE5F0F6003592FB /* Logan.swift in Sources */,
94350F721BE5EF9F003592FB /* BugKrusha.swift in Sources */,
D5D15B361E0C966A00CC9B1C /* felixdumitClockEntry.swift in Sources */,
809B704A1BF01B68004131BE /* LoganWrightEntry.swift in Sources */,
944CFCD81BE72B3C00FD2E41 /* LetterCombinationsOfPhoneNumberChallenge.swift in Sources */,
94350F691BE5EB90003592FB /* Aranasaurus.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion CodeChallenge/Challenges/Clock/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ struct ClockChallenge: CodeChallengeType {
FlavioSilverioClockEntry,
ethanSchatzlineClockEntry,
brandonShegaClockEntry,
matthijsClockEntry
matthijsClockEntry,
felixdumitClockEntry
]

func verifyOutput(_ output: (hourHandeAnlge: Int, minuteHandAngle: Int, secondHandAngle: Int), forInput input: String) -> Bool {
Expand Down
81 changes: 81 additions & 0 deletions CodeChallenge/Challenges/Clock/Entries/felixdumitClockEntry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// felixdumitClockEntry.swift
// CodeChallenge
//
// Created by Felix Dumit on 12/22/16.
// Copyright © 2016 iosdevelopers. All rights reserved.
//

import Foundation

private var dateFormatter: DateFormatter = {
let df = DateFormatter()
df.dateFormat = "HH:mm:ss"
return df
}()

let felixdumitClockEntry = CodeChallengeEntry<ClockChallenge>(name: "felix-dumit") { input in
if #available(iOS 10.0, *) {

guard let date = dateFormatter.date(from: input) else {
fatalError("bad input")
}

func duration(for component: Calendar.Component) -> Measurement<UnitDuration> {
let cmp = NSCalendar.current.component(component, from: date)
return Measurement<UnitDuration>(value: Double(cmp), unit: component.durationUnit!)
}

let totalDuration = duration(for: .hour) + duration(for: .minute) + duration(for: .second)

func clockAngle(unit: UnitDuration) -> Int {
let percent = (totalDuration % unit.durationForClockFullTurn) / unit.durationForClockFullTurn
return Int(360 * percent)
}

return (clockAngle(unit: .hours), clockAngle(unit: .minutes), clockAngle(unit: .seconds))

} else {
return brandonShegaClockEntry.block(input)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm... Maybe a fatalError here instead of returning someone else's solution?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok just updated it ;)

}
}

@available(iOS 10.0, *)
private func /<D:Dimension>(lhs: Measurement<D>, rhs: Measurement<D>) -> Double {
return lhs.converted(to: D.baseUnit()).value / rhs.converted(to: D.baseUnit()).value
}

@available(iOS 10.0, *)
private func %<D:Dimension>(lhs: Measurement<D>, rhs: Measurement<D>) -> Measurement<D> {
let value = lhs.converted(to: D.baseUnit()).value
.truncatingRemainder(dividingBy: rhs.converted(to: D.baseUnit()).value)
return Measurement(value: value, unit: D.baseUnit())
}

@available(iOS 10.0, *)
private extension UnitDuration {
var durationForClockFullTurn: Measurement<UnitDuration> {
switch self {
case UnitDuration.hours:
return Measurement<UnitDuration>(value:12, unit: .hours)
case UnitDuration.minutes:
return Measurement<UnitDuration>(value:1, unit: .hours)
case UnitDuration.seconds:
return Measurement<UnitDuration>(value: 1, unit: .minutes)
default: //should not happen
return Measurement<UnitDuration>(value: 0, unit: .seconds)
}
}
}

@available(iOS 10.0, *)
private extension Calendar.Component {
var durationUnit: UnitDuration? {
switch self {
case .hour: return .hours
case .minute: return .minutes
case .second: return .seconds
default: return nil
}
}
}