When do I get my sandwich?

Given one of the following inputs:

Sweet Onion Chicken Teriyaki
Oven Roasted Chicken
Turkey Breast
Italian BMT
Tuna
Black Forest Ham
Meatball Marinara
output a number from 1-7 (or 0-6) representing the day of the week that you get that deal, starting with Monday as the lowest number. Input can be all lowercase or uppercase if preferred (i.e "italian bmt"). No internet allowed.

• Don't really see how this Kolmogorov complexity. The text is the input...numbers are the output. – geokavel Sep 5 '17 at 15:45
• Also does Sunday say "Meatball Marinara" or "Meatball Marina Ra"? – Erik the Outgolfer Sep 5 '17 at 16:09
• I'm pretty sure it's supposed to be Marinara but the keming in the image is pretty awful... – totallyhuman Sep 5 '17 at 16:14
• If it's useful to anyone: the number of a's plus the number of e's in each input is [5,4,3,2,1,3,6] respectively. – geokavel Sep 5 '17 at 18:14
• When you use sudo. – jpmc26 Sep 5 '17 at 19:10

Python 2, 3830 28 bytes

lambda S:6793**164[len(S)]


Try it online!

Unfortunately still one byte longer than the best Python 2 answer thus far; though not using the enklact-approach.

Now one byte shorter than i cri everytim's answer!

How does it work?

After a lot of brute force, I found an expression that results in a number which has just the right digits.
I noticed that looking at only one specific digit of the given string's lengths required 3 bytes (%10). So I wrote another Python program (Pastebin link) to further search for numbers who directly map the input strings' lengths to the day of the week.

The magic number looks like this: 6793**164 = 28714733692312345620167113260575862840674216760386883406587492336415023761043044176257567032312859371641211117824224067391750766520256112063756278010050204239810862527958109285342869876264808102743173594017101607983288521836082497514383184553444755034407847810524083812459571382103831904835921560285915349760536969265992879312869538914200854305957428078269094250817029486005437991820466986793657301214564264748923199288698278615871481529585816783654841131577178922192383679718074693535597651237893794976519274268917335387876260270630339777501802739852278932279775510324916969726203688466311848240746465178859847331248655567344801 (a number with an impressive 629 decimal digits)

And as you can see, the number provides the necessary mapping from [28, 20, 13, 11, 4, 16, 17] to [0, 1, 2, 3, 4, 5, 6] (Python strings are 0-indexed):

2871 4 733692 3 1 2 34 5 6 20 1 6711326 0 5758628406742167603868834... [4]^ [11]^ [13]^ [16]^ ^[17] ^[20] ^[28]

My program also found other expressions which yield numbers with the required property, though they take more bytes to represent (29 instead of 28): 19439**540, 34052**726, 39311**604, 44873**182, 67930**164 and 78579**469. (Those are all the expressions found by the linked program; its execution took several hours.)

Alternative function that requires 28 bytes: lambda S:7954<<850[len(S)]
Alternative function that requires 29 bytes: lambda S:9699<<2291[len(S)]
Alternative function that requires 30 bytes: lambda S:853<<4390[len(S)+9]
Alternative function that requires 31 bytes: lambda S:1052<<3330[len(S)+8]

How does it work? How did I generate that number? (30 byte answer)

The 30 byte answer was lambda S:3879**41[len(S)%10].

Looking at the input string's lengths [28, 20, 13, 11, 4, 16, 17], I noticed that all last digits in base ten differ, resulting in the list [8, 0, 3, 1, 4, 6, 7]. So I only needed a mapping from that list to the list of all seven days of the week, [0, 1, 2, 3, 4, 5, 6].

My first approach simply used a string to perform the mapping: lambda S:"13*24*560"[len(S)%10], though the string required eleven bytes ("13*24*560").
So I wrote a Python program (Pastebin link) to test for arithmetic expressions which result in an integer with matching digits, hoping to further golf the program. What I came up with thus far is 3879**41 (only ten bytes, the only and thereby smallest expression my program finds).

Of course, there are many different possible expressions one could try; I just got lucky that there was one in the form a**b with a reasonably small result that fit my need.

Just for anyone curious, 3879**41 = 1372495608710279938309112732193682350992788476725725221643007306215781514348937145528919415861895033279220952836384201346579163035594383625990271079 = 1.372... * 10**147.

Another valid function I found whilst searching for alternative expressions that unfortunately requires 32 bytes: lambda S:7**416[len(S)%10+290]

• How did you generate that number? :o – totallyhuman Sep 5 '17 at 18:57
• @icrieverytim Brute force. – Jonathan Frech Sep 5 '17 at 19:34
• -9114**28 is a smaller integer* that also works (*in absolute terms not just because it is negative -- 111 digits rather than 629). Does not save on bytes though. – Jonathan Allan Sep 7 '17 at 17:01
• Every once in a while I check the PCG questions in HNQ, and am usually disappointed to find golfing languages are the top three spots. Today i was not disappointed. Thank you! – Sidney Sep 7 '17 at 19:01
• @icrieverytim ಠ_ಠ that's just the same number but halved with a shift factor of one more ಠ_ಠ :P – hyper-neutrino Sep 25 '17 at 14:15

Python 2, 29 bytes

lambda s:'enklact'.find(s[3])


Try it online!

Explanation

The magic string, enklact, was found by looking for the first column with unique letters.

The first column goes SOTITBM which is not useful 'cause it contains duplicates. The second and third ones also don't work because they are wvutule and eeranaa respectively. The fourth column, however works as it has all unique letters.

lambda s:'enklact'.find(s[3])

lambda s:                      # declare a function that takes a single paramater s
.find(    )  # find the index of the first instance of...
s[3]   # the fourth (0-indexing) character of s...
'enklact'             # in the magic string


Python, 26 bytes

lambda S:1923136>>len(S)&7


Try it online!

With a debt of thanks (for my second straight code-golf try) to Jonathan Frech's answer -- I wouldn't have thought to use the string length instead of a distinguishing letter!

This code derives from my experience with De Bruijn Sequences and programming for chess.

In chess, you often work with several 64-bit integers, where each bit indicates something is true or false about the corresponding square on the chessboard, such as "there is a white piece here" or "this square contains a pawn".

It is therefore useful to be able to quickly convert 2**n to n quickly and cheaply. In C and C++, the quickest way to do this is to multiply by a 64-bit De Bruijn sequence -- equivalent to shifting by n bits -- then right-shift 58 (to put the first six bits last -- make sure you're using an unsigned int or you'll get 1s half the time) and look up this 0..63 number in a table that gives you the corresponding n which is in the same range, but rarely the same number.

This is kind of related. Instead of changing from 2**n to n, however, we want to change from n to some other 3-bit number. So, we hide our 3-bit numbers in a magic 31-bit number (a 28-bit shift requires bits 28-30, with numbering starting at 0.)

I generated the number needed by just seeing what values had to fall where (trying both 0..6 and 1..7 as the output sets). Fortunately, the overlapping values (14, 16, and 17) happen to work out! And since the first tri-bit is 000 and the next is 001, we don't need the leftmost 7 bits, resulting in fewer digits -> fewer bytes of source.

The required number is 000xxxx001110101011xxxx100xxxx, where the x's can be 1 or 0 and it doesn't affect the result for these particular subs -- I set them to 0 just to minimize the number, but changing any of the last 8 xs shouldn't affect the source code's length. Setting all the xs to 0, and leaving off the start, gives 1923136 in decimal (or 1D5840 in hex, but then you need the 0x prefix -- shame!) The &7 at the end just masks the last 3 bits, you could also use %8, but then you'd need parentheses due to python's operator precedence rules.

tl;dr: 1923136 encodes each of the three-bit combinations from 0 to 6 in exactly the right spots that these sandwich names happen to fall in place, and then it's a matter of taking the last three bits after a right shift.

• Quite similar to my bit mask approach, but even better. – Bruno Costa Dec 7 '17 at 22:06

Jelly, 10 bytes

What's with all this "enklact" business?

⁽ṃXị“Ð$¥»i  A monadic link taking a list of characters and returning the Monday=1 day of the week. Try it online! or see the test-suite How? ⁽ṃXị“Ð$¥»i - Link: list of characters, sandwichName  e.g. "Tuna"
⁽ṃX        - base 250 literal                             -7761
ị       - index into sandwichName (1-indexed & modular) 'n'
“Ð$¥» - dictionary word "retinal" i - index of 5  • How did you find the -7761/retinal combo? – Emigna Sep 6 '17 at 8:20 • Wrote a loop that ran through creating "words" and seeing if they existed in the dictionary. "retinal" was the only one between -32249 and 32250 (the range of ⁽...) – Jonathan Allan Sep 6 '17 at 8:23 • ...slight mistake - the range of ⁽.. is actually [-31349,32250]-[-99,999] (there are also other numbers one may represent with three or less bytes like 7!! or ȷ76) – Jonathan Allan Sep 7 '17 at 17:24 C (gcc), 7271564641 39 bytes f(char*s){s=index(s="enklact",s[3])-s;}  Try it online! • i;char x[]="enklact"; is shorter than i,x[]={101,110,107,108,97,99,116};. – Jonathan Frech Sep 5 '17 at 15:54 • Was about to post that to: char*x="enklact" is even shorter: Try it online! – scottinet Sep 5 '17 at 15:56 • Also, you can remove i=0. – Jonathan Frech Sep 5 '17 at 15:59 • Even shorter version using index: Try it online! – scottinet Sep 5 '17 at 16:01 • @scottinet Nice! I've never used index() before. Thanks – cleblanc Sep 5 '17 at 16:04 MATL, 16 15 bytes O5OHlO7KI6vjYm)  Explanation O5OHlO7KI6 % Push numbers 0, 5, 0, 2, 1, 0, 7, 4, 3, 6 v % Concatenate all numbers into a column vector j % Push input as a string Ym % Mean (of ASCII codes) ) % Index into the column vector (modular, 1-based, with implicit rounding) % Implicit display  Jelly, 11 bytes 4ị“-ƭɼoṚ0»i  Try it online! Explanation: 4ị“-ƭɼoṚ0»i 4ị 4th element of z “-ƭɼoṚ0»i First index in "enklactate"  • @Mr.Xcoder unless I use enklactate instead of enklact for my string, in which case it brings it down to 11 ;) – Erik the Outgolfer Sep 5 '17 at 15:58 • Oh, for fudge's sakes, why is enklactate in Jelly's dictionary? – totallyhuman Sep 5 '17 at 16:00 • @icrieverytim It's not, enk is a string and lactate is a word. EDIT: just confirmed, enklactate isn't in the dictionary. – Erik the Outgolfer Sep 5 '17 at 16:00 • @icrieverytim I think it's just lactate. – Mr. Xcoder Sep 5 '17 at 16:00 • Aw maaan, I wanted to find a way to mock Jelly's dictionary... Maybe next time. :P – totallyhuman Sep 5 '17 at 16:03 Perl 5, 24 bytes 23 bytes code + 1 for -p. /[Svrl\s](?!a)/;$_="@+"


-4 bytes thanks to @nwellnhof!

Try it online!

I thought I would post a couple of other alternatives

Javascript 38 bytes

a=s=>(271474896&7<<s.Length)>>s.Length


Javascript 27 bytes

s=>"240350671"[s.length%10]

• the 2nd alternative is 29 bytes long forgot a= – Bruno Costa Sep 7 '17 at 14:18
• Why is the a= part needed? Look at Shaggy's answer. – geokavel Sep 7 '17 at 14:19
• @BrunoCosta On this site, we don't require you to name your functions. Anonymous ones work, so you don't need the a=. – Rɪᴋᴇʀ Sep 7 '17 at 14:20
• @BrunoCosta tha'ts cool, it's really up to you. You can also just not count it in the header byte count, and include it in the snippet for ease of testing. – Rɪᴋᴇʀ Sep 8 '17 at 2:39
• @MichaelBoger Unfofrtantly I don't think it can because the minimum amount of code to make that work I believe it's something along the lines of a=s=>{b=s.Length;return(271474896&7<<b)>>b} – Bruno Costa Dec 7 '17 at 21:43

Japt, 12 bytes

0-indexed, takes input in lowercase.

kÇXsmbUg#


Test it

Explanation

Implicit input of lowercase string U

kÇXsm


The compressed string kotinsm.

bUg#


Get the first index (b) of the character at index (g) 26 (#) in U. (Yay, index wrapping!)

Implicit output of integer result.

Alternative

Same as the above (and everyone else!), just using the characters at index 3 instead, allowing for title case input.

klactbUg3


Test it

• Where'd the en go? :P – totallyhuman Sep 5 '17 at 15:52
• @icrieverytim: that's a compressed string; en is compressed to an unprintable. – Shaggy Sep 5 '17 at 15:53
• I think I see a bUg in your code. – shenles Sep 5 '17 at 19:03

05AB1E, 11 bytes

Saved 1 byte thanks to Erik the Outgolfer and 1 byte thanks to Magic Octopus Urn.

.•ΛΓ2º•I3èk


Try it online!

• "enklact" -> ’enkl†¼’ – Erik the Outgolfer Sep 5 '17 at 16:04
• ’enkl†¼’ -> .•ΛΓ2º• – Magic Octopus Urn Sep 6 '17 at 14:14
• @MagicOctopusUrn Oh thanks – Mr. Xcoder Sep 6 '17 at 14:16
• @Mr.Xcoder this is how I got that. – Magic Octopus Urn Sep 6 '17 at 15:55
• @MagicOctopusUrn Will keep in the archive, thanks :p – Mr. Xcoder Sep 6 '17 at 15:56

JavaScript (ES6), 25 bytes

0-indexed.

s=>"enklact".search(s[3])


Test it

o.innerText=(f=
s=>"enklact".search(s[3])
)(i.value);oninput=_=>o.innerText=f(i.value)
<select id=i><option selected value="Sweet Onion Chicken Teriyaki">Sweet Onion Chicken Teriyaki</option><option value="Oven Roasted Chicken">Oven Roasted Chicken</option><option value="Turkey Breast">Turkey Breast</option><option value="Italian BMT">Italian BMT</option><option value="Tuna">Tuna</option><option value="Black Forest Ham">Black Forest Ham</option><option value="Meatball Marinara">Meatball Marinara</option></select><pre id=o>

• This is 25 bytes! – geokavel Sep 7 '17 at 14:26
• Thanks, @geokavel. Don't know where I got those extra 3 bytes from; even counting f= would only make it 27. – Shaggy Sep 7 '17 at 14:31
• simple and effective solution. +1 :) – Brian H. Dec 7 '17 at 11:13
• @Shaggy Maybe your third extra byte was a trailing newline. – Michael Boger Dec 7 '17 at 21:31

GolfScript, 12 bytes

{+}*93&(5?7%


Try it online!

Maps the inputs (via the sum of their code points) to 0 to 6.

Explanation

Found with a GolfScript snippet brute force tool I wrote a while ago...

{+}*  # Sum all code points.
93&   # ... AND 93.
(     # Decrement.
5?    # ... raised to the fifth power.
7%    # ... modulo 7.


Here is how this transforms each of the inputs to the desired result:

                                 {+}*   93&     (            5?   7%
Sweet Onion Chicken Teriyaki     2658    64    63     992436543    0
Oven Roasted Chicken             1887    93    92    6590815232    1
Turkey Breast                    1285     5     4          1024    2
Italian BMT                       965    69    68    1453933568    3
Tuna                              408    24    23       6436343    4
Black Forest Ham                 1446     4     3           243    5
Meatball Marinara                1645    77    76    2535525376    6


Excel, 28 bytes

Using the enklact method:

=FIND(MID(A1,4,1),"enklact")

• You can drop 2 bytes by converting this to Google Sheets and dropping the ") – Taylor Scott Mar 1 '18 at 20:48

Perl 6, 22 bytes

tr/enklact/^6/.comb[3]


Try it online!

• perl 2017.07 accepts tr/enklact/^6/.comb[3] which would be 22 bytes, but tio.run apparently does not have it yet. – Massa Sep 5 '17 at 18:29
• (tio.run runs perl6 v2017.6) – Massa Sep 5 '17 at 18:38
• As long as an interpreter exists that can run the 22 byte version, it's valid. – Shaggy Sep 6 '17 at 13:18

CJam, 11 bytes

l1b93&(5#7%


Try it online!

A port of my GolfScript answer. It costs 1 byte to read the input explicitly, but we save two when summing the code points.

Husk, 10 bytes

%7^5←n93ṁc


Try it online!

Another port of my GolfScript answer. I'm sure eventually I'll find a language that can sum the code points for a single byte...

Husk (post-challenge update), 9 bytes

%7^5←n93Σ


Try it online!

Now, Σ does sum code points directly. Since this was added per a request after I answered this challenge, I'm not going to use it as my primary score though.

Pyth, 13 bytes

x"enklact"@w3


Verify all the test cases.

Alternative:

x."atÖÅû"@w3


3 can be substituted by any of the following values: [3, 4, 11, 13, 21, 24, 25, 26]

Pyke, 12 bytes

3@"enklact"@


Try it here!

Proton, 23 bytes

s=>'enklact'.find(s[3])


Try it online!

:P

for$i(S,O,TUR,I,TUN,B,M){$"++;$_=$"if/^$i/}  Try it online! Requires the first three characters of input to be upper case. • It has to be all uppercase, all lowercase, or regular. – geokavel Sep 5 '17 at 21:41 • Call it all uppercase, then. Anything beyond the first three characters is ignored. – Xcali Sep 5 '17 at 21:52 Java 8, 26 bytes Credit to @icrieverytim Takes input as a char[] s->"enklact".indexOf(s[3])  • You can do s->"enklact".indexOf(s[3]) if you specify that you take input as a char array. – shooqie Sep 5 '17 at 17:17 • Is that okay? I've a few answers that can be shortened like that – Roberto Graham Sep 5 '17 at 17:30 Haskell, 36 bytes -9 bytes thanks to H.PWiz. f s=length$fst\$span(/=s!!3)"enklact"


Try it online!

Alternate solution, 45 bytes

This uses the indexOf function in Data.List as elemIndex.

import Data.List
(elemIndex"enklact").(!!3)


Try it online!

• 37 bytes – H.PWiz Oct 28 '17 at 21:51
• @H.PWiz Oh, neat abuse of break. Thanks! – totallyhuman Oct 28 '17 at 22:00
• 36 – H.PWiz Oct 28 '17 at 22:03
• @H.PWiz Haha, how'd I miss that. XD – totallyhuman Oct 28 '17 at 22:05

C++, 1191187776 73 bytes

-41 bytes thanks to Peter Cordes
-1 byte thanks to Zacharý
-3 bytes thanks to Michael Boger

At the string index 3, character for every sandwich is different

#include<string>
int s(char*p){return std::string("enklact").find(p[3]);}

std::initializer_list<char*> t{
"Sweet Onion Chicken Teriyaki",
"Oven Roasted Chicken",
"Turkey Breast",
"Italian BMT",
"Tuna",
"Black Forest Ham",
"Meatball Marinara"
};

for (auto& a : t) {
std::cout << s(a);
}


Golfing with std::string, that was obvious ofc... what was i thinking...

• std::find in a string literal (or maybe a std::string) seems like the obvious way to go. Basically the same idea as index or strchr that the C answers are using, in a data structure where the 0-5 is implicit from the position. – Peter Cordes Sep 8 '17 at 4:30
• You can remove the newline inbetween lines 2 and 3. – Adalynn Oct 22 '17 at 21:06
• You don't need a variable to store enklact. std::string("enklact").find(p[3]) works fine. This brings it down 3 characters. – Michael Boger Dec 6 '17 at 23:41
• You CAN pass string literals to your function (g++ only raises a warning) so the memcpy stuff isn't needed. – Michael Boger Dec 6 '17 at 23:42

C# (.NET Core), 289 bytes

using System;class c{delegate int s(string r);static void Main(){s y=x=>{if(x[0]==83)return 1;if(x[0]==79)return 2;if (x[0]==84&x[2]=='r')return 3;if(x[0]==73)return 4;if(x[0]==84)return 5;if(x[0] ==66)return 6;if(x[0]==77)return 7;return 0;};Console.Write(y.Invoke(Console.ReadLine()));}}


Run online

Golfscript, 13 bytes

Try it online!

'enklact'\3=?


Takes the 4th character (which, for each, will be unique) and looks it up in the string "enklact".

Alternatively:

'nklact'\3=?)


This takes advantage of the fact that Golfscript's ? function returns -1 if the element searched for is not found (which, for Monday, it will not). If this were allowed, the solution could be reduced by 1 byte.

Dyalog APL, 13 bytes

'enklact'⍳4⌷⍞


Try it online!

K (oK), 13 bytes

Solution:

"enklact"?*3_


Try it online!

Examples:

> "enklact"?*3_"Black Forest Ham"
5
> "enklact"?*3_"Turkey Breast"
2


Explanation:

Interpretted right-to-left, pull out the 4th element from the input and return the zero-index location in the "enklact" list:

"enklact"?*3_  / the solution
3_  / 3 drop, 3_"Turkey Breast" => "key Breast"
*    / first, *"key Breast" => "k"
?     / lookup right in left
"enklact"      / list to lookup element in