Cooking with Code
Haskell, 48 bytes
foldl(?)
is an anonymous function taking two list arguments and returning a list, with all elements of the same (Eq
-comparable) type.
Use as foldl(?)["pepper", "paprika", "salt", "cumin", "oregano"]["oregano", "cumin", "cumin", "salt", "salt", "salt"]
.
foldl(?)
(x:y:r)?n|y==n=y:x:r|s<-y:r=x:s?n
s?n=s
Try it online!
foldl(?) s m
starts with the (spice rack) lists
and combines it with each element (spice) fromm
in order, using the operator?
.s?n
uses the spicen
from the spice racks
and returns the resulting spice rack.- If
s
has at least two elements,?
checks whether the second one is equal ton
, and if so switches the first two elements. If not equal,?
keeps the first element fixed and recurses on the rest. - If
s
has at most one element,?
returns it unchanged.
- If
Chef, 875 843 bytes
S.
Ingredients.
1 g T
0 g Z
J
I
Method.
Put Z into mixing bowl.Take I from refrigerator.B I.Put I into mixing bowl.Take I from refrigerator.B until bed.Take I from refrigerator.V I.Put Z into 2nd mixing bowl.N T.Fold J into mixing bowl.Put J into mixing bowl.Remove I.Fold T into mixing bowl.Put J into 2nd mixing bowl.N until ned.Fold T into mixing bowl.Put T into mixing bowl.Fold J into 2nd mixing bowl.Put J into mixing bowl.C T.Stir for 1 minute.Put Z into mixing bowl.Fold T into mixing bowl.C until ced.Fold T into 2nd mixing bowl.G T.Put T into mixing bowl.Fold T into 2nd mixing bowl.G until ged.Put I into mixing bowl.Fold T into mixing bowl.Take I from refrigerator.V until ved.Fold T into mixing bowl.L T.Put T into 2nd mixing bowl.Fold T into mixing bowl.L until led.Pour contents of 2nd mixing bowl into baking dish.
Serves 1.
-32 bytes thanks to Jonathan Allan by removing the
where I wouldn't think it will work.
Chef has no string types, so the ingredients are positive integers. 0 is used to separate the starting list from used ingredients and to end the used ingredients list. See the TIO link for an example.
Pseudocode explaination:
A, B: stack
T, J, IN: int
T = 1
A.push(0) // used as the marker for the bottom of the stack
IN = input() // input the first list
while(IN):
A.push(IN)
IN = input()
IN = input() // for each used ingredient
while(IN):
B.push(0)
while(T): // move all ingredients up to and including the one we are moving right now to the second stack
T = A.peek() - IN
B.push(A.pop())
A.push(B.pop())
if(A.peekUnderTop() != 0):
A.swapTopTwoItems()
T = B.pop() // move the ingredients from the second stack back to the first
while(T):
A.push(T)
T = B.pop()
T = IN // to make it non-zero for next iteration
IN = input(0
print(A.inverted())
Try it online!
JavaScript, 61 bytes
a=>b=>b.map(v=>(p=a.indexOf(v))&&a.splice(p-1,2,a[p],a[p-1]))
Input format:
- f(list_of_spices)(list_of_what_spices_got_used)
- two list are array of string
Output:
- list_of_spices is modified in-place.
f=
a=>b=>b.map(v=>(p=a.indexOf(v))&&a.splice(p-1,2,a[p],a[p-1]))
a = ['pepper', 'paprika', 'salt', 'cumin', 'oregano'];
b = ['salt', 'salt', 'salt', 'salt', 'salt', 'salt', 'salt', 'salt', 'salt', 'salt', 'salt', 'salt', 'oregano'];
f(a)(b);
document.write(JSON.stringify(a))