Good Afternoon Golfers,
Our challenge for today is inspired by XKCD comics 356 and 370. We're going to write a program to calculate the resistance of a group of resistors. A forewarning that this is nearly tough enough to warrant being a code challenge, however I think that there is a certain art to writing slightly more complex programs in a golfed format. The lowest amount of characters wins.
Calculating the resistance relies on the following two formulas:
- If the resistors are in series, the resistance is the sum of the each resistor's resistance
- If the resistors are in parallel, the resistance is the inverse of the sum of the inverse of the resistance of each resistor
So - for example:
Your challenge is to, in the least amount of characters possible, calculate the resistance of a group of up to 64 resistors. My apologies for the complexity, particularly of the input rules. I've attempted to define them in such a way that every language will be usable.
Each resistor will be connected to 2 or more other resistors.
The input is guaranteed to be valid, with only one entry and one exit point, which will connect
The network will be series-parallel to prevent requiring more maths then what is presented
Input will be via file, argument or stdin, depending on what is appropriate for your language.
Input will consist of a series of newline or forward slashed separated statements consisting of an integer of the resistance of the resistor, and spaces separating the IDs of the resistors that one side of the resistor is connected to.
The ID of the first resistor will be 1, incrementing by one for each successive resistor
The start will always have an ID of 0
The final resistor will always have a resistance of 0 ohms, and only have the connections defined in its line
Could be represented as
3 0 6 1 1 0 5 0 0 2 3 4
- Output can be to stdout or file. It may be represented in one of the following ways:
- A number with a minimum of 2 decimal places, followed by a newline
- A fraction consisting of an integer (the numerator), a forward slash and another integer (the denominator), followed by a newline. The fraction does not need to be the to be in its lowest form - 4/4 or 10/8 are, for instance acceptable. The fraction must be accurate within 1/100. There is no bonus for being perfectly accurate - this is provided is a crutch to enable languages without fixed or floating point operations to compete.
I hope that covers all the points. Good luck!