Last active
December 16, 2016 13:28
-
-
Save geekskick/02bb64ae7c5af49858db to your computer and use it in GitHub Desktop.
Daily Programmer X Mas Lights, using as many computed properties as possible
This file contains 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
import Foundation | |
///the led information | |
struct LED{ | |
let voltageDrop :Float! | |
let currentDrawn :Float! | |
let image :String = "-|>|-" | |
init(voltageDrop: Float, currentDrawn: Float){ | |
self.voltageDrop = voltageDrop | |
self.currentDrawn = currentDrawn | |
} | |
} | |
///the battery information | |
struct battery{ | |
var voltage :Float! | |
var capacityMAH :Float! | |
init(voltage: Float, capacity: Float){ | |
self.voltage = voltage | |
self.capacityMAH = capacity | |
} | |
} | |
class circuit{ | |
var maximumNumberOfComponentsInSeries: Int { | |
get{ | |
return Int(powerSupply.voltage/components.voltageDrop) | |
} | |
} | |
var currentDrawOfComponentsInSeries: Float { | |
get{ | |
return components.currentDrawn | |
} | |
} | |
var powerSupply: battery | |
var components: LED | |
var numberOfHoursToRun: Int = 0 | |
var currentAvailableFromCell: Float{ | |
get{ | |
return powerSupply.capacityMAH / Float(numberOfHoursToRun) | |
} | |
} | |
var maxParallelComponents: Int{ | |
get{ | |
return Int(currentAvailableFromCell/currentDrawOfComponentsInSeries) | |
} | |
} | |
var totalComponents:Int{ | |
get{ | |
return maxParallelComponents * maximumNumberOfComponentsInSeries | |
} | |
} | |
var remainingVoltage: Float{ | |
get{ | |
return powerSupply.voltage - (Float(maximumNumberOfComponentsInSeries) * components.voltageDrop) | |
} | |
} | |
var resistorValueNeeded: Float{ | |
get{ | |
///v = I*R, therefore r = V/I | |
return remainingVoltage/(powerSupply.capacityMAH / Float(numberOfHoursToRun)) | |
} | |
} | |
init(components: LED, psuType: battery, hoursToRun: Int = 1){ | |
self.powerSupply = psuType | |
self.components = components | |
self.numberOfHoursToRun = hoursToRun | |
} | |
func draw(){ | |
///show resistor value to 3dp | |
print("Resistor Value: \(String(format: "%.3f",resistorValueNeeded))Ω") | |
///Need to have double the number of rows, so hat I can draw separators between the parallel branches | |
for row in 1...(maxParallelComponents*2){ | |
///The first row has the cell terminals on | |
if row == 1{ | |
print("*", terminator: "") | |
} | |
///if not the first row then there must be whitespace as padding to make everything line up | |
else{ | |
print(" ", terminator: "") | |
} | |
///There need to draw components on the separatos branches | |
if row % 2 == 1{ | |
for comp in 1...(maximumNumberOfComponentsInSeries){ | |
///Put an extra spcaes in front of each LED image | |
print("-\(components.image)", terminator: "") | |
///if it's the last time round then add an end spacer to line everything up | |
if comp == maximumNumberOfComponentsInSeries{ | |
print("-", terminator: "") | |
} | |
} | |
} | |
///now the brnaches separators remain, I only want to print on the ones between the branches, | |
///so miss out the last one | |
else if row != (maxParallelComponents*2){ | |
///draw connection | |
print("|", terminator: "") | |
///skip over the number of whitespaces where components would be | |
for _ in 0...(maximumNumberOfComponentsInSeries){ | |
for _ in 1...components.image.characters.count{ | |
print(" ", terminator: "") | |
} | |
} | |
///conneciton at end | |
print("|", terminator: "") | |
} | |
///cell terminal | |
if row == 1{ | |
print("-*", terminator: "") | |
} | |
///go to next line | |
print("") | |
} | |
} | |
} | |
var c = circuit(components: LED(voltageDrop: 1.7, currentDrawn: 0.02), psuType: battery(voltage: 9, capacity: 1.2), hoursToRun: 20) | |
c.draw() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment