Kiln firing controller
MediumOnline Assessment
Simulate the controller of a pottery kiln that follows a firing schedule, one thermocouple reading per minute.
The program is a list of segments run in order: "ramp <target> <rate>" raises the setpoint by rate degrees per minute up to target, and "hold <minutes>" keeps the setpoint where it is until the kiln has really stayed there for that many minutes. readings[i] is the temperature measured in minute i.
The controller keeps a setpoint sp, which starts equal to readings[0], and a heater that starts off. Handle each minute i in this order:
- If the controller has tripped, the output is
X; stop here. - If the program has finished, the output is
.(heater off); stop here. Nothing is checked after the end. - Over-temperature trip: if
readings[i] > sp + 30, orreadings[i] >= 1300, the controller trips for good: outputX. This usesspas it was at the end of the previous minute. - Advance the program, but not in minute
0:
- Current segment is a ramp: only if the kiln keeps up, i.e.
readings[i] >= sp - 20, setsp = min(target, sp + rate). Ifspreachestarget, the ramp is done. - Current segment is a hold: only if
readings[i] >= sp - 5does this minute count towards the hold. When the count reaches the hold'sminutes, the hold is done. A segment that finishes this minute lets the next segment start working from the next minute. After the last segment finishes the program is finished, but this minute's heater decision is still made.
- Heater: with the possibly updated
sp, switch the heater on ifreadings[i] <= sp - 3, off ifreadings[i] >= sp + 3, otherwise leave it as it was. OutputHif it is on,.if off.
Input: program (each ramp target is above the setpoint when that ramp begins) and readings.
Output: a string with one character (H, . or X) per reading; "" if readings is empty.
Example 1
- Input:
- [["ramp 50 10","hold 2"],[20,24,31,40,49,52,54,50,45,40]]
- Output:
- ".HHHHH...."
- Explanation
- The setpoint starts at 20 and rises 10 a minute. Minute 0 only checks the heater: 20 is within 3 of the setpoint, so it stays off. Minutes 1 to 3 the setpoint is 30, 40, 50 and the kiln is below it by 6, 9 and 10, so the heater switches on. The ramp finishes in minute 3. The hold needs 2 minutes with
readings >= 45: minutes 4 (49) and 5 (52) count, and the program ends in minute 5 after a normal heater decision (52 is not yet 3 above 50, so the heater stays on). From minute 6 on the output is.:.HHHHH.....
Example 2
- Input:
- [["ramp 100 20","hold 3","ramp 160 30"],[20,35,50,70,95,98,93,99,104,100,96,97,120,150,158,160,171]]
- Output:
- ".HHHHHHH.HHHH...."
- Explanation
- The ramp reaches 100 in minute 4. The hold needs readings of at least 95: minute 5 (98) counts, minute 6 (93) does not, minutes 7 (99) and 8 (104) count, so the hold ends in minute 8, where 104 is 4 above the setpoint and the heater turns off. The next ramp starts in minute 9: 100 is at least 80 so
spbecomes 130. In minutes 10 and 11 the readings 96 and 97 are belowsp - 20 = 110, so the setpoint waits at 130 and the heater stays on. Minute 12 (120) letsspreach 160 and the program ends; every later minute is..
Example 3
- Input:
- [["ramp 60 10","hold 2"],[20,25,38,44,40,29,26,27,30,60,91,90]]
- Output:
- ".HHHHHHHHHXX"
- Explanation
- The ramp reaches 60 in minute 4. During the hold the readings 29, 26, 27 and 30 are far below 55, so none of those minutes count. Minute 9 (60) is the first that counts, with the hold at 1 of 2. In minute 10 the reading 91 is above
sp + 30 = 90, so the over-temperature trip fires and the output isXfrom then on.
Constraints
1 <= program.length <= 30; ramp targets are integers up to1,290, rates from1to50, hold lengths from1to6000 <= readings.length <= 3,000; readings are integers from0to1,400- each ramp target is strictly above the setpoint at the moment that ramp begins