Last active
August 29, 2015 14:21
-
-
Save ciarand/c953713198f35e641464 to your computer and use it in GitHub Desktop.
Solving the blowing fuses problem in Pascal
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
program Fuses; | |
{ | |
The input consists of several test cases. Each test case describes a set of | |
electrical devices and gives a sequence of turn on/off operations for these | |
devices. | |
The input will be terminated by a test case starting with n = m = c = 0. This | |
test case should not be processed. | |
} | |
type | |
{ Device represents anything that can draw power } | |
Device = record | |
turnedOn : boolean; | |
consumption : integer; | |
end; | |
{ Circuit represents a collection of Devices. It has a maximum draw | |
(capacity) } | |
Circuit = record | |
capacity : integer; | |
state : array of Device; { resizable array } | |
end; | |
var | |
n, m, c, i, j, currentDraw, maximum, seqNum : integer; | |
tcircuit : Circuit; | |
begin | |
seqNum := 1; | |
repeat { loop while n, m, and c are not all == 0 } | |
{ | |
The first line of each test case contains three integers n, m and c, where | |
n is the number of devices (n <= 20), m the number of operations performed | |
on these devices and c is the capacity of the fuse (in Amperes). | |
} | |
read(n); read(m); read(c); | |
if (n = 0) and (m = 0) and (c = 0) then break; | |
setLength(tcircuit.state, n); | |
tcircuit.capacity := c; | |
{ | |
The following n lines contain one positive integer ci each, the consumption | |
(in Amperes) of the i-th device. | |
} | |
for i := 0 to n - 1 do | |
begin | |
read(tcircuit.state[i].consumption); | |
tcircuit.state[i].turnedOn := false; | |
end; | |
{ | |
This is followed by m lines also containing one integer each, between 1 and | |
n inclusive. They describe a sequence of turn on/turn off operations | |
performed on the devices. For every number, the state of that particular | |
devices is toggled, i.e. if it is currently running, it is turned off, and | |
if it is currently turned off, it will by switched on. At the beginning all | |
devices are turned off. | |
} | |
maximum := 0; | |
for i := 1 to m do | |
begin | |
read(j); dec(j); { 0-based indices, but the input is 1-based } | |
tcircuit.state[j].turnedOn := not tcircuit.state[j].turnedOn; | |
currentDraw := 0; | |
for j := 0 to Length(tcircuit.state) - 1 do | |
begin | |
if tcircuit.state[j].turnedOn then | |
currentDraw := currentDraw + tcircuit.state[j].consumption; | |
end; | |
if currentDraw > maximum then maximum := currentDraw; | |
end; | |
{ print out the summary } | |
writeln('Sequence ', seqNum); | |
if maximum > tcircuit.capacity then | |
writeln('Fuse was blown.') | |
else | |
begin | |
writeln('Fuse was not blown.'); | |
writeln('Maximal power consumption was ', maximum, ' amperes.') | |
end; | |
writeln(); | |
inc(seqNum); | |
until false; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment