Tosia uses instructions to control three different-coloured lights. The instructions can turn each light on or off, and also wait for a specific amount of time before moving on to the next instruction. Tosia writes the following set of instructions and has them run \(10\) times in a row.
turn red light on
wait(3sec)
turn blue light on
turn green light on
wait(5sec)
turn green light off
wait(2sec)
turn green light on
turn red light off
wait(2sec)
turn green light off
turn blue light off
How long does it take for the instructions to run completely \(10\) times from start to finish? Assume that it doesn't take any time at all to turn the lights on and off.
Which lights are on exactly \(33\) seconds after the instructions start?
First we will calculate how many seconds it takes to go through the instructions once. Since \(3+5+2+2=12\), it takes \(12\) seconds to go through the instructions once. Since we go through the instructions \(10\) times, it takes \(12 \times 10=120\) seconds (or \(2\) minutes) for the instructions to run completely from start to finish.
There are two solutions for this part.
Solution 1:
We know from part (a) that it takes \(12\) seconds to go through the instructions once. Since \(12\times 2+9=33\), that means after \(33\) seconds, the program will have gone through the instructions twice, and be \(9\) seconds into its third pass.
If we look at the set of instructions, we can see that all three lights are turned on in the beginning and turned off at the end. This tells us we don't need to consider the first two times through the instructions and can focus only on which lights are on \(9\) seconds into the instructions. We will walk through the code until we reach \(9\) seconds.
turn red light on
wait(3sec)
Thus, after \(3\) seconds, only the red light is on. We will look through the next part of the code.
turn blue light on
turn green light on
wait(5sec)
After \(3+5=8\) seconds, the red, blue, and green lights are all on. We will look through the next part of the code.
turn green light off
wait(2sec)
The green light turns off, and then the program waits for \(2\) seconds. Since \(8+1=9\) seconds, that means \(9\) seconds into the instructions, the program is waiting. At this point, only the red and blue lights are on.
Therefore, \(33\) seconds after the program starts running, only the red and blue lights are on.
Solution 2:
We can walk through the first \(33\) seconds of the code and record which lights are on at each second. We will let "R" represent the red light, "B" represent the blue light, "G" represent the green light, and summarize our findings in a table.
| Seconds Elapsed | Lights On |
|---|---|
| \(1\) | R |
| \(2\) | R |
| \(3\) | R |
| \(4\) | R B G |
| \(5\) | R B G |
| \(6\) | R B G |
| \(7\) | R B G |
| \(8\) | R B G |
| \(9\) | R B |
| \(10\) | R B |
| \(11\) | B G |
| \(12\) | B G |
| Seconds Elapsed | Lights On |
|---|---|
| \(13\) | R |
| \(14\) | R |
| \(15\) | R |
| \(16\) | R B G |
| \(17\) | R B G |
| \(18\) | R B G |
| \(19\) | R B G |
| \(20\) | R B G |
| \(21\) | R B |
| \(22\) | R B |
| \(23\) | B G |
| \(24\) | B G |
| Seconds Elapsed | Lights On |
|---|---|
| \(25\) | R |
| \(26\) | R |
| \(27\) | R |
| \(28\) | R B G |
| \(29\) | R B G |
| \(30\) | R B G |
| \(31\) | R B G |
| \(32\) | R B G |
| \(33\) | R B |
| \(34\) | R B |
| \(35\) | B G |
| \(36\) | B G |
From the table, we can determine that \(33\) seconds after the program starts running, only the red and blue lights are on.