Your challenge is to take a 1D-list of groceries and a 2D pantry as input; and output an newly assorted pantry (more details below). The two variables can be of your type choice, and in any order, but please specify what item types your program requires (e.g. string, array, etc.).
For example, here's a quick mockup (and score)
JavaScript ES6 (1768 Byte)
f=(a,b,n)=>{
a=a.replace(/\W/g,"").split("").sort();
a=a.slice(0, a.length - b.length).concat(b.split("").sort().join("")).join("").split("").sort().join("");
return a.replace(RegExp(`(.{${n}})`, 'g')," $1\n");
}
- Calculate your score usingThis is code golf, so the number ofshortest answer in bytes your program is (normal scoring)wins
Examples ([][ isand ] are used for readability)
Example solution
JavaScript ES6 (989 bytes)
// (String, String) -> String
let organise = (pantry, groceries) => {
let n = pantry.split("\n").sort((a, b) => b.length - a.length); // used at the end of the function for horizontal sizing
n = n[0].length;
pantry = pantry
.replace(/\W/g, "") // get rid of all non-alphanumeric characters
.split(""); // turn the string into an array
// we need the properties of the new array
// so the extra `pantry = pantry` is needed
pantry = pantry
.slice(0, pantry.length - groceries.length) // go ahead and remove the last overlapping elements
.concat(groceries) // add the groceries to the pantry
.join("") // turn into a string
.split("") // turn into an array
.sort() // sort the array
.join(""); // turn into a string
return pantry.replace(RegExp(`(.{${n}})`, 'g'), "$1\n");
};
/** Testing below **/
console.log("Test #2:\n" + organise(
`AJCHDJE
JJ JA
ASD
OOQ I U
Q W
R`,
'AHJBCJHDHHATTGEH'
))