Back
Close

Equivalent Resistance, Circuit Building

Statement

 Goal

Calculate the equivalent resistance of a circuit containing only resistors.

A resistor is a component used in electrical circuits. A resistor is quantified by its Resistance, which is measured in Ohms. We are interested in knowing the total resistance of a circuit of only resistors. There are two key definitions needed to determine the resistance of multiple resistors.

1. Series

The resistance of resistors in a line is equivalent to the sum of the resistance of those resistors.

    ---[R_1]---[R_2]---

Resistors in series will be noted with parentheses ( R_1 R_2 R_3 ... and so on ).

The resistance of a series arrangement is: R_eq = R_1 + R_2 + R_3 + ... and so on, where R_eq is the equivalent resistance of the series arrangement.

2. Parallel

The resistance of resistors in branching paths of the circuit is equal to 1 over the sum of 1 over the resistance of each branching path.

       +---[R_1]---+
| |
---+ +---
| |
+---[R_2]---+


Resistors in parallel will be noted with brackets [ R_1 R_2 R_3 ... and so on ].

The resistance of resistors in parallel is R_eq = 1/(1/R_1 + 1/R_2 + 1/R_3 + 1/... and so on).

A branch can be treated as a single resistor by determining its equivalent resistance.

Example:

N = 3
A 24
B 8
C 48
[ ( A B ) [ C A ] ]

This will look something like this:

       +---[C]---+
| |
+--+ +--+
| | | |
| +---[A]---+ |
| |
+---[A]---[B]---+
| |
+---[Battery]---+

[ ( A B ) [ C A ] ] => [ 24+8 1/(1/48+1/24) ] => [ 32 16 ] => 1/(1/32+1/16) => 32/3 => 10.666... => 10.7
Input
Line 1: An integer N for the number of unique resistors present in the circuit
Next N lines: A space separated name and the integer resistance R of a resistor
Last line: A space separated combination of parentheses, brackets, and names of resistors
Output
The equivalent resistance expressed as a float rounded to the nearest 0.1 Ohms.
Constraints
0 < N < 10
0 < R < 100
Example
Input
2
A 20
B 10
( A B )
Output
30.0

Tags
ParsingPhysics

Difficulty
Easy

Test cases
Series Test
Input
2 A 20 B 10 ( A B )
Output
30.0

Series Validator Validator
Input
2 E 5 F 15 ( F E )
Output
20.0

Parallel Test
Input
2 C 20 D 25 [ C D ]
Output
11.1

Parallel Validator Validator
Input
2 G 27 H 9 [ G H ]
Output
6.8

Combined (Example Diagram) Test
Input
3 A 24 B 8 C 48 [ ( A B ) [ C A ] ]
Output
10.7

Combined Validator Validator
Input
3 D 32 E 24 F 48 [ [ F E ] ( F D ) ]
Output
13.3

Complex Test
Input
7 Alfa 1 Bravo 1 Charlie 12 Delta 4 Echo 2 Foxtrot 10 Golf 8 ( Alfa [ Charlie Delta ( Bravo [ Echo ( Foxtrot Golf ) ] ) ] )
Output
2.4

Complex Validator Validator
Input
5 Hotel 7 India 56 Juliet 24 Kilo 5 Lima 7 ( Hotel [ India ( [ Juliet ( Kilo Lima ) ] ) ] )
Output
14.0

More Complex Test
Input
3 Alef 30 Bet 20 Vet 10 ( Alef [ ( Bet Bet Bet ) ( Vet [ ( Vet Vet ) ( Vet [ Bet Bet ] ) ] ) ] )
Output
45.0

More Complex Validator Validator
Input
3 Gimel 40 Dalet 14 He 10 ( Gimel [ ( Dalet Dalet ) ( He [ ( He He ) ( He [ Dalet Dalet ] ) ] ) ] )
Output
51.4

5-pointed Star Test
Input
1 Star 78 [ ( [ Star ( Star Star ) ] [ Star ( Star Star ) ] Star ) ( [ Star ( Star Star ) ] [ Star ( Star Star ) ] Star ) ]
Output
91.0

5-pointed Star Validator Validator
Input
1 ratS 66 [ ( ratS [ ratS ( ratS ratS ) ] [ ratS ( ratS ratS ) ] ) ( ratS [ ratS ( ratS ratS ) ] [ ratS ( ratS ratS ) ] ) ]
Output
77.0

Solution language

Solution

Stub generator input
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants