# Does the string contain 2016? [closed]

Given a string input, your task is to write a program that prints a truthy value to STDOUT or equivalent if the input contains "2016" anywhere in it without using any built-in contains, match, replace, split or other functions that use patterns or regexes.

A truthy value also must be printed if the input contains "2016" with the same number of spaces between each number.

## Test Cases

2016
=> true
"2016"
=> true
2202012016016166
^^^^ => true
220201 2 0 1 6 016166
=> true
Happy New Year! It's 2016!
=> true
2016 is coming!
=> true

2_0_1_6
=> false
22001166
=> false
It's not 2015 any more
=> false
6102
=> false
201.6
=> false
20 16
=> false
2 0  1   6
=> false

## Rules

• Regexes and pattern matching cannot be used
• Built-in contains, match, and replace cannot be used
• Built-in split functions cannot be used (the only exception is when splitting the string into individual characters for looping through them, such as input.split(""))
• The code cannot contain the string "2016" in any form (unicode literals and constructing the string included - that doesn't mean that the characters 0, 1, 2, or 6 can't be used)
• The input may either be taken from STDIN or as an argument to a function
• It is safe to assume that the input will not contain linefeeds or newlines.
• The input may contain any printable ASCII characters (spaces included)
• A truthy value must be printed to STDOUT or equivalent if the input contains 2016.
• A falsey value must be printed to STDOUT or equivalent if the input does not contain 2016
• If using a function, instead of using STDOUT, the output can be the return value of the function
• The truthy/falsey value cannot be printed to STDERR.
• Standard loopholes apply
• This is , so the shortest program in bytes wins. Good luck!

## Bonus

-30 Bytes if the input also returns a truthy value if the spaces between the characters in 2016 are the same.

Happy 2 0 1 6
=> true
I can't wait for  2   0   1   6
=> true
It's almost 2 0  1   6
=> false

This is a Stack Snippet that generates both a leaderboard and an overview of winners by language.

## Language Name, N bytes

Where N is the size, in bytes, of your submission

If you want to include multiple numbers in your header (for example, striking through old scores, or including flags in the byte count), just make sure that the actual score is the last number in your header

## Language Name, <s>K</s> X + 2 = N bytes

## closed as unclear what you're asking by Lynn, Blackhole, El'endia Starman, Deusovi, catDec 24 '15 at 4:06

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• It's too vague what built ins are allowed. For example, can I split on 2016 and check if the result has multiple parts? What about a builtin that checks if a string is a prefix of another string? – xnor Dec 24 '15 at 1:22
• Ok, then can I use join to intersperse a string with spaces? Slice a string at some position? Check if strings are equal? It would be nice to have a criterion for the boundary rather than checking case by case. – xnor Dec 24 '15 at 1:36
• I think they mean it's unclear what code you're asking them to produce because of the vague rules about what built-ins are allowed, no string constructing, etc... – user81655 Dec 24 '15 at 2:02
• -1; rules are ad-hoc and arbitrary – lirtosiast Dec 24 '15 at 3:08
• The addition of many arbitrary-looking rules often means that a challenge is uninteresting or flawed. – lirtosiast Dec 24 '15 at 3:30

# CJam, 35 - 30 = 5 bytes

qS*_,,_Sf*{"2 0 1 6"\*}%\:)@few:+&

## Explanation

qS*                                   Push s p a c e d   i n p u t.
_,,                                Push [0..N-1] where N = len(input).
_Sf*                            Push ["", " ", "  ", "   ", ...].
{"2 0 1 6"\*}%              Make it ["2 0 1 6", "2   0   1   6", ...].
\             Swap [0..N-1] to top of stack.
:)           Increment each, giving [1..N].
@          Rotate spaced input I to top of stack.
few       Push [I1ew I2ew .. INew], i.e.
:+      when concatenated, all substrings of I.
&    Intersect sets.
Print as CJam syntax.

Essentially, given an input string abcd, this executes a "substring search" by intersecting the sets:

• 2 0 1 6, 2 0 1 6, ... up to 2*len(input())-1 = more than enough spaces,
• a, , b, ..., a b, ... a b c d.

If a result is found, such a spaced 2016 string appeared as a substring of the input, and a truthy list containing this result is printed, like ["2 0 1 6"]. Otherwise, the empty list is printed.

The string "2016" is never constructed as the code runs; only its spaced-out versions are.

# JavaScript (ES6), 125 - 30 = 95 bytes

x=>(y=[...x]).map((_,i)=>y.map((_,j)=>r?0:(n=x.substr(j,a.length),r=n<a&n>b),s=" ".repeat(i),a=2+s+0+s+1+s,b=a+5,a+=7),r=0)|r

## Explanation

Returns 1 if it matched, otherwise 0.

Brute forces every possible combination of spaces between 2016 and checks for a match at each substring. Note that it never constructs the string 2016 because it generates a as 2017 and b as 2015 (both with spaces) and checks if b < substring < a.

x=>                             // x = input string
(y=[...x])                    // y = input as array
.map((_,i)=>                  // loop over every possible amount of spaces between digits
y.map((_,j)=>               // loop over every character to check for a match
r?0:(                     // if r is set we don't have to do anything, if not:
n=x.substr(j,t.length), // n = substring to compare
r=n<a&n>b               // check if r is 2016
),
s=" ".repeat(i),          // s = spaces between letters
a=2+s+0+s+1+s,            // get string to compare with
b=a+5,                    // b = 2015 (with spaces)
a+=7                      // a = 2017 (with spaces)
),
r=0                         // r = result
)
|r                            // return result

## Test

var solution = x=>(y=[...x]).map((_,i)=>y.map((_,j)=>r?0:(n=x.substr(j,a.length),r=n<a&n>b),s=" ".repeat(i),a=2+s+0+s+1+s,b=a+5,a+=7),r=0)|r
<input type="text" id="input" value="I can't wait for  2   0   1   6" />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>

• It looks like this returns false for the input "220201 2 0 1 6 016166" - I should probably add that as a test case – Jojodmo Dec 24 '15 at 2:20
• @Jojodmo You're right. I changed to a brute-force approach because it was shorter than fixing my old method. – user81655 Dec 24 '15 at 3:02

## Swift 2, 307 297 - 30 = 267 Bytes

func d(s:String)->Int{var c=0;var n=0;var l=0;for v in s.characters{if(v=="2"){c=1;l=0;n=0};if(c==1&&v=="0"){c=2;l=n;n=0};if(c==2&&v=="1"){c=(l==n ?3:0);l=n;n=0};if(c==3&&v=="6"){if(l==n){return 1}else{c=0}};if(!(v=="2" || v=="0" || v=="1" || v=="6")){if(v==" "){n++}else{c=0;l=0;n=0;}}};return 0}

### Ungolfed

func d(s: String) -> Int{
var c=0
var n=0
var l=0
for v in s.characters{
if(v == "2"){
c = 1
l = 0
n = 0
}
if(c == 1 && v == "0"){
c = 2
l = n
n = 0
}
if(c == 2 && v == "1"){
c = (l == n ? 3 : 0)
l = n
n = 0
}
if(c == 3 && v == "6"){
if(l == n){
return 1
}
else{
c = 0
}
}
//you may be wondering why I didn't use &&. It's because
//using && would require me to use !=, which needs spaces
//on both sides
if(!(v == "2" || v == "0" || v == "1" || v == "6")){
if(v == " "){
n++
}
else{
c=0
l=0
n=0
}
}
}
return 0
}