Skip to main content
added code snippets, and fixed several errors
Source Link

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'
))

Your challenge is to take a 1D-list of groceries and a 2D pantry as input; and output an assorted pantry (more details below). The two variables can be of your choice, 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 using the number of bytes your program is (normal scoring)

Examples ([] is used for readability)

Your challenge is to take a 1D-list of groceries and a 2D pantry as input; and output an newly assorted pantry. 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.).

  • This is code golf, so the shortest answer in bytes wins

Examples ([ and ] 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'
))

fixed scoring
Source Link

JavaScript ES6 (221 Characters: 9.0425531914893611768 Byte)

  • Calculate your score using the number of bytes your program is (number of charactersnormal scoring): program size / 24.44

Test #1, 4x4 (4.00 pts)pantry

Test #2, 7x6 (8.23 pts)pantry

Test #3, 10x10 (3.52 pts)pantry

Test #4, 16x16 pantry (7.69 pts)pantry

Test #5, 2x2 (1.00 pt)pantry

JavaScript ES6 (221 Characters: 9.042553191489361)

  • Calculate your score using (number of characters): program size / 24.44

Test #1, 4x4 (4.00 pts)

Test #2, 7x6 (8.23 pts)

Test #3, 10x10 (3.52 pts)

Test #4, 16x16 pantry (7.69 pts)

Test #5, 2x2 (1.00 pt)

JavaScript ES6 (1768 Byte)

  • Calculate your score using the number of bytes your program is (normal scoring)

Test #1, 4x4 pantry

Test #2, 7x6 pantry

Test #3, 10x10 pantry

Test #4, 16x16 pantry pantry

Test #5, 2x2 pantry

Source Link

Sandbox:

Is this question already available (duplicate)?

Are things too vague?

Does providing the example help or hinder?

Tidy the Pantry (easy)

I hate grocery shopping, particularly the part where I put groceries away--so I'm calling upon the collective hive-mind to handle that.

Challenge

Your challenge is to take a 1D-list of groceries and a 2D pantry as input; and output an assorted pantry (more details below). The two variables can be of your choice, 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 (221 Characters: 9.042553191489361)

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");
}

Rules & Additional info.

Scoring

  • Calculate your score using (number of characters): program size / 24.44

Rules

  • The pantry should be ordered alphabetically (A - Z, left to right, top to bottom)
    • For simplicity, the pantry is case-insensitive
  • The pantry must retain its horizontal size (but trailing newlines are optional)
  • "Pockets" (empty spaces) should be filled between items (i.e. only the last item is allowed to have a trailing pocket)
  • If the pantry is too small for the incoming groceries, then the pantry must replace older items (Z being the oldest, A the youngest)
    • Z from groceries is younger than A in pantry
  • Standard loopholes are forbidden

Examples ([] is used for readability)

Input (4x4 pantry):

[A][A][ ][ ]
[ ][ ][B][ ]
[C][ ][ ][ ]
[ ][ ][ ][D]

AAD

Output:

[A][A][A][A]
[B][C][D][D]
[ ][ ][ ][ ]
[ ][ ][ ][ ]

Input (2x2 pantry):

[A][B]
[C][D]

XYZ

Output:

[A][X]
[Y][Z]

Test Cases:

Test #1, 4x4 (4.00 pts)

TVCX <- pantry
ABCD
ATDJ
UAIK

XYXY <- groceries
----
AAAB <- expected output
CCDD
IJKT
XYXY

Test #2, 7x6 (8.23 pts)

AJCHDJE
JJ   JA
    ASD
OOQ I U
Q     W
      R

AHJBCJHDHHATTGEH
-------
AAAAABC
CDDDEGH
HHHHJJT
T

Test #3, 10x10 (3.52 pts)

AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA

ZZZZZZZZZZZZZZZZZZZZ
----------
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
AAAAAAAAAA
ZZZZZZZZZZ
ZZZZZZZZZZ

Test #4, 16x16 pantry (7.69 pts)

ASDFGHJKLZXCVBNM
QJKAJ  KAKSJD  J
KJASDKFHI YOIER
W   OSDOFJ    DK
E PPPASP     AS
R
TASD 
YAAAAAAAAAAAA
U          JHOLK
IIAUSHODUYOAISUO
OASD  AUSODI 
PIASND JUASJNOIJ
A ASJDH PPOIO 
QHIAIUSOIUOOO
WYYAIUSNNAJSDASD
EAISDUUIOPJPIJPJ
ROQPEWIHRNXCAISD

QWERTYUIOP
----------------
AAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAA
ABCCDDDDDDDDDDDD
DDDEEEEEFFFGHHHH
HHHIIIIIIIIIIIII
IIIIIIJJJJJJJJJJ
JJJJJJKKKKKKKKLL
MNNNNNNOOOOOOOOO
OOOOOOOOOOPPPPPP
PPPPPPQQQQRRRRRS
SSSSSSSSSSSSSSSS
SSSTTUUUUUUUUUUU
UVWWY

Test #5, 2x2 (1.00 pt)

HE
LO

[no groceries]
--
HE
LO