Code Wars V

Program / Task Name:

   Stereo Hookup




Point Value:




Task Description

Hooking up a home theater system can be difficult and confusing. To achieve the best audio and video quality, each source device (such as a DVD player, VCR, or tape deck) must be connected to the receiver (the central ‘hub’, which controls volume and allows switching from one device to another) using the best interconnect method available. For audio signals, a Coax digital connection is preferred, followed closely by an Optical digital connection, and lastly an analog connection. For video signals, a Component video connection is preferred, followed closely by an S-Video connection, and lastly a Composite connection.

Receivers have several inputs to support the connection of a large number of devices (5 or more inputs is typical). Each input may support more than one type of connection (for both audio and video), but to keep down the price of the receiver, most manufacturers limit the quantity of each connection type. For example, a typical receiver contains only two inputs with Component video connectors, while it has five or more inputs with Composite video connectors. This makes sense because not every device supports all of the connection types. For example, a VCR will only provide Composite video connectors, but a DVD player will provide Component, S-Video, and Composite video connectors.

Your task is to create a program to optimize the cable connections to a home theater receiver, by matching each device to the best available input on the receiver, and list instructions showing the device connections and all the cables required.

The source devices must be connected using the following rules:

  • Devices must be connected using the best available connection to a receiver input. Video connections take priority over audio connections (that is, a receiver input selection should be made using the video portion first, and the audio portion second).

  • If the best matching audio or video connection type is not available, the device must be connected using the next best available type. For example, if no Component video connections are available, a typical DVD player would be connected using an S-Video connection.

  • Any device supporting video must have both an audio and a video connection. If no video inputs are available, the device cannot be connected to the receiver.

  • Only one audio and one video cable are required for each device. For example, a device connected using a Coax digital connection does not need an Optical digital or Analog cable.

  • Devices with the most capabilities take priority over devices with fewer capabilities. For example, an audio device with a Coax digital output must be connected (even if it means using the lesser-quality Analog audio connection) before an audio device with only an analog connection. Similarly, devices with video capability take priority over audio-only devices.

  • If two devices have identical capabilities, the device appearing first in the input file takes priority.

Only one device can be connected to each receiver input. If the number of devices exceeds the number of inputs, then the lowest priority device(s) cannot be connected.

Program Input

The input file consists of a receiver section, and a source devices section. The first line of the receiver section contains two strings: the keyword RECEIVER, and the product name of the receiver (maximum of 20 characters). The next lines of the receiver section list each receiver input and its capabilities, one receiver input per line. Each line starts with the name of the receiver input (maximum of 8 characters), followed by the audio connection types available, followed by the available video connection types. The audio connection types will always be in the following order: ANALOG, OPTICAL-DIGITAL, COAX-DIGITAL. The video connection types will always be in the following order: COMPOSITE, S-VIDEO, COMPONENT-VIDEO. Field names are separated by commas. The maximum number of receiver inputs is ten. The receiver section ends with the keyword END-RECEIVER on a separate line.


The device section contains a list of devices to be attached, one device per line. Each line starts with the keyword DEVICE, followed by the name of the device (maximum of 20 characters), followed by the audio connection types, followed by the video connection types (in the same order defined in the receiver section). The device section ends with the keyword END-DEVICE on a separate line.


A very simple example input file is shown below.


DEVICE, Disco Starr 8-Track, ANALOG
DEVICE, Hello Kitty Cassette, ANALOG

Program Output

The program must respond to each query by writing a report to the output file. The report title must include the receiver’s product name. It must show the steps required for connecting the devices to the best receiver input. Each numbered step must describing a connection needed (from device to receiver input), and must be reported in the same order as the devices appeared in the input file. Finally, the report must conclude with a list of the cables required to complete the installation, including both the quantity and type of cable. See the results below for examples.

Home Theater Receiver Connections for: Acme RJX-3900

1. Connect the El Cheapo CD audio to the CD input using an Analog audio cable
2. Connect the Pannasonik VCR audio to the VCR1 input using an Analog audio cable
3. Connect the Pannasonik VCR video to the VCR1 input using a Composite video cable
4. Connect the Suny DVD audio to the DVD input using a Coax-Digital audio cable
5. Connect the Suny DVD video to the DVD input using an S-Video video cable
6. Connect the Disco Starr 8-Track audio to the VCR2 input using an Analog audio cable

NOTE: The Hello Kitty Cassette cannot be connected to this receiver

The cables required to complete this installation are as follows:

Qty   Cable Type
---   --------------------
1      S-Video video
1      Composite video
1      Coax-digital audio
3      Analog audio


hp Code Wars
Hello -- Dominos?...
  (c) 2004 Hewlett-Packard Company