Cellophane is a thin, transparent sheet made of regenerated cellulose. Cellulose fiber from wood, cotton or hemp is dissolved in alkali and treated with carbon disulfide to make a solution called viscose. This solution is then extruded through a slit into a bath of dilute sulfuric acid and sodium sulfate to reconvert the viscose into cellulose. The film is then passed through several more baths, one to remove sulfur, one to bleach the film, and one to add glycerin to prevent the film from becoming brittle.
Cellophane was invented by Swiss chemist Jacques E. Brandenberger. It took ten years for Brandenberger to perfect his transparent film, before cellophane was patented in 1912. Cellulose film has been manufactured continuously since the mid-1930s and is still used today. As well as packaging a variety of food items, there are also industrial applications, such as as a base for such self-adhesive tapes (including Sellotape and Scotch Tape) and as a semi-permeable membrane in a certain type of battery. Cellophane sales have dwindled since the 1960s, due to alternative packaging options and due to the polluting effects of carbon disulfide and other by-products of the process used to make viscose.
A rectangular grid is drawn on a flat surface — for example a window or a lighted plate used by doctors to view x-ray images. The rows of the grid have been numbered left to right starting from zero, as do the columns from top to bottom. Some rectangular cellophane sheets are attached to the surface, with each sheet exactly covering some of the squares in the grid. These transparent sheets are either colored red or blue. If a square is covered by one or more red sheets it is colored red. If a square is covered by one ore more blue sheets it is colored blue. If a square is covered by at least one read and at least one blue sheet, it is colored purple.
Determine how many purple squares you get if the grid on a surface is covered by a given series of red and blue sheets. This is done in the following way.
Write a function purple that takes a list of the positions of a series of cellophane sheets. Each sheet is represented by a tuple containing five elements. The first two elements are integers that indicate the column and row number of the square in the grid that is covered by the top left corner of the sheet. The next two elements are integers that indicate the width and height of the sheet, expressed as a number of squares. The final element is a string that indicates the color of the sheet: R (red) or B (blue). The function must return the number of squares in the grid that are finally colored purple, after all sheets have been attached on their given positions.
Use the function purple to write a function cellophane, that takes the location of a text file as an argument. Each line of this text file contains the description of the position where a cellophane sheet should be attached on a rectangular grid. The first character is a letter that indicates the color (R for red or B for blue) of the sheet. This is followed by four integers that are separated from each other by a single space (not that there is no space between the first letter and the first integer). The integers represent the column and row numbers of the square in the grid that is covered by the top left corner of the sheet, and the width and height of the sheet expressed as a number of squares. The function must return the number of squares in the grid that are finally colored purple, after all sheets have been attached on their given positions.
In the following interactive session, we assume that the file cellophane.txt1 is located in the current directory. The arrangement of the cellophane sheets on the grid corresponds in both cases to the arrangement in the above figure.
>>> purple([(0, 0, 5, 5, 'R'), (10, 0, 5, 5, 'R'), (3, 2, 9, 2, 'B')])
8
>>> cellophane('cellophane.txt')
8