Bale grade ledger
MediumOnline Assessment
Grade the bales a recycling plant presses from the truckloads it receives, one material at a time.
Each load is [material, kg, contaminantKg]: the load's weight and how much of it is contamination (food, wrong plastics, and so on). Loads arrive in the order of the list, and each material is pressed separately.
- A load with
kg == 0is a clerical entry: ignore it completely (it does not even make its material appear in the report). - A load whose contamination is more than 40% of its weight (
contaminantKg * 100 > 40 * kg) is turned away at the gate. It is counted as gate-rejected and never baled. - Every other load goes into its material's open bale. As soon as the open bale's total weight is at least
targetkg, the bale is closed and graded, and the next load of that material starts a new one. The load that closes a bale always goes in whole; nothing is split between bales. - A bale's purity is
floor(100 * (totalKg - totalContaminantKg) / totalKg). Purity95or more is gradeA,85to94is gradeB, anything lower is arejectbale. However, if the bale contains a hot load, one with contamination more than 20% of its own weight (contaminantKg * 100 > 20 * kg), then a bale that would beAis gradedBinstead. - A bale still open after the last load is never graded; its weight is the material's leftover.
Input: loads, a list of [material, kg, contaminantKg] (lowercase string, integer, integer with 0 <= contaminantKg <= kg), and target, an integer.
Output: a list with one entry [material, a, b, reject, leftoverKg, gateRejected] per material that had at least one load with kg > 0, ordered alphabetically by material. The first four numbers after the name count bales of each grade (a, b, reject) and the leftover weight; gateRejected counts the loads turned away. Return [] if there is nothing to report.
Example 1
- Input:
- [[["paper",300,10],["tin",120,0],["paper",250,5],["steel",0,0],["pet",500,100],["paper",200,60],["paper",100,0],["paper",250,5]],500]
- Output:
- [["paper",1,1,0,0,0],["pet",0,0,1,0,0],["tin",0,0,0,120,0]]
- Explanation
- With
target = 500, the first two paper loads (300 and 250 kg, 15 kg of contamination) close a bale of 550 kg with purityfloor(100 * 535 / 550) = 97: grade A. The next paper bale is 550 kg with 65 kg of contamination: purity 88, grade B (the 60 kg contamination in a 200 kg load is 30%, but that is allowed at the gate, and a hot load only matters for a bale that would be A). The two plastic loads of 500 kg and 100 kg of contamination are 20%, so not hot, and purity is 80: a reject bale. Tin has only 120 kg and stays open. The zero-weight steel entry is ignored entirely.
Example 2
- Input:
- [[["glass",480,0],["glass",30,7],["glass",100,41],["foil",10,9],["glass",100,40]],500]
- Output:
- [["foil",0,0,0,0,1],["glass",0,1,0,100,1]]
- Explanation
- The glass bale (480 + 30 kg, 7 kg contaminated) has purity 98, but the 30 kg load is 23% contaminated, so it is a hot load and the bale is graded B. The 100 kg load with 41 kg of contamination is turned away, while the one with 40 kg is exactly 40% and is accepted (it starts a new bale, still open: leftover 100). Foil only ever had a gate-rejected load but still appears.
Example 3
- Input:
- [[["card",499,0],["foam",2,2],["card",1,0],["card",900,0],["board",1000,51],["foam",1,1],["card",120,0]],500]
- Output:
- [["board",0,1,0,0,0],["card",2,0,0,120,0],["foam",0,0,0,0,2]]
- Explanation
cardreaches 499 kg without closing; the next 1 kg closes the bale at exactly the target, and the 900 kg load is a bale all by itself; the final 120 kg is the leftover. Bothfoamloads are 100% contaminated and turned away, sofoamreports no bales and no leftover. The 1,000 kgboardload has 949 clean kg: purity is floored to 94, so the bale is grade B, not A.
Constraints
0 <= loads.length <= 2,0001 <= target <= 100,000;0 <= kg <= 5,000- material names are 1 to 12 lowercase letters