[Question]
  [
This question is inspired by [this awesome answer](https://codegolf.stackexchange.com/a/17103/106710).
The challenge is to write a program or function which takes in a positive integer, and outputs "Happy new year to you" or a slight variation thereof, such that the character codes of that string add up to the inputted number. The space counts towards the sum of character codes too.
The case does not matter at all; you may output `HappY New yEAR` or something so long as it satisfies the specifications below.
The specifications are as follows:
The outputted string must contain `happy new year`. As stated above, the case does not matter.
The `happy new year` component of the string may optionally be preceded by `have a` , again in any case. However, there must be a space before `happy` if the string starts with `have a` .
The `happy new year` string may optionally be followed by  `to you`, again in any case. The space before `to` is mandatory.
Note that `Have a happy new year to you`, while not grammatically correct, is an acceptable output for the purposes of this challenge.
Finally, at the very end of the string, any number of exclamation marks can be outputted.
In summary, the outputted string must satisfy the following regular expression:
```
/^(have a )?happy new year( to you)?!*$/i
```
You may assume while writing your program or function that you will only be given numbers for which it is possible to construct a "happy new year" string.
I won't allow outputting in array or list format because it defeats the purpose of the challenge.
If there are multiple solutions, feel free to output any one of them, any subset of them, or all of them.
As this challenge is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), the shortest code, measured in bytes, wins.
## Test cases
```
2014
-> Happy new year to you!
1995
-> HAPPY NEW YEAR TO You!!!!!!!!!!!!!!
989
-> HAPPY NEW YEAR
1795
-> Have a HAPPY new year!
2997
-> Have a HAppY new year to YOU!!!!!!!!!!!!!!!!!!!
```
  
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), ~~159~~ 148 bytes
This code builds the string without resorting to brute force.
```
y=>Buffer((s="happy new year",r=n=y%33)>17&n<20?(n-=7,r-=5,"have a "+s):n<18?s+" to you":(n-=20,s)).map(c=>c-32*(c>32&&n-->0))+'!'.repeat(y/33+r-61)
```
[Try it online!](https://tio.run/##LdBLTsMwEIDhqwyRaGximzwoeVCnAsQpqi4s16GgYlt2WuQ7ILFhCffgPL0ARwguYvMtZn5ppHkWB@Gle7Ij1WajpoFPgfd3@2FQDiHPk62wNoBWrxCUcAlxXPNwXlW4L@qZXpT5EmnKa@Ion5NYHxQISDKPO70omqXPEhgNBLNPulNY5sRjzF6ERZL3klblBZJ9Vc5mmtI@xzhLz1LmlFViROGyqjJHrws83aygbVoCRd3Oo@2/zZ9xXubFVbRta1izwbgHIbcoAO9BGu3NTrGdeUQeOAwoYAIrxphfx0ObvVQIWQISn2oLGUgmt8Ldx3fcjii2edxwCLCE9Of7/fj5EU2hg/T49ZZiPP0C "JavaScript (Node.js) – Try It Online")
### How?
Let \$y\$ be the input year.
Because we can add \$k\times33,k\ge0\$ to the result by adding as many trailing exclamation marks as we want, we need to focus on \$n=y\bmod33\$.
The method that is used here can be split into the 4 following cases:
* If \$n\le17\$:
	+ We use the base string `"happy new year to you"` and put \$n\$ characters in upper case, for a base score of \$2013-32n\$.
	+ We add \$\lfloor y/33\rfloor+n-61\$ exclamation marks.
	+ The final score is:
	$$2013-32n+33\times(\lfloor y/33\rfloor+n-61)\\=33\times\lfloor y/33\rfloor+n=33\times\lfloor y/33\rfloor+(y \bmod 33)=y$$
* If \$n=18\$:
	+ We use the base string `"HAVE A HAPPY New year"`, for a base score of \$1602\$.
	+ We add \$\lfloor y/33\rfloor-48\$ exclamation marks.
	+ The final score is:
	$$1602+33\times(\lfloor y/33\rfloor-48)\\=33\times\lfloor y/33\rfloor+18=33\times\lfloor y/33\rfloor+(y \bmod 33)=y$$
* If \$n=19\$:
	+ We use the base string `"HAVE A HAPPY NEw year"`, for a base score of \$1570\$.
	+ We add \$\lfloor y/33\rfloor-47\$ exclamation marks.
	+ The final score is:
	$$1570+33\times(\lfloor y/33\rfloor-47)\\=33\times\lfloor y/33\rfloor+19=33\times\lfloor y/33\rfloor+(y \bmod 33)=y$$
* If \$n\ge20\$, we use the base string `"happy new year"` and put \$n-20\$ characters in upper case, for a base score of \$2013-32n\$.
	+ We add \$\lfloor y/33\rfloor+n-61\$ exclamation marks.
	+ The computation of the final score is similar to the 1st case.
Obviously, it's not possible to add a negative number of exclamation marks, which is why only some years before 1981 have a happy new year string ([as pointed out by @AnttiP](https://codegolf.stackexchange.com/questions/242683/happy-new-year-string-builder#comment546658_242683)).
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 130 bytes
```
f=(n,s='',...a)=>n|!/^(have a )?happy new year( to you)?!*$/i.test(s)?f(...a,...[...f+f].map(_=>(a=!a)?s+Buffer([i++]):n-i,i=2)):s
```
[Try it online!](https://tio.run/##FczRCsIgFIDhV1EIdk5uDrocOKHXGCsOpcwglekWQu9u7eK//P4X7ZQeq4u58@FparUKfJtU07RSSkI1@i/vb7DQbhgx1AvFWJg3H1YMrcByYCVsqPn51DuZTcqQUFs49LGY/llhZ/mmCHc1AilOqJO4btaaFSYnxIyD71zr1AVxSLX@AA "JavaScript (Node.js) – Try It Online")
Run out of memory on any computer as it use >128^14 args, but theoretically it works
[Answer]
# Excel, 219 bytes
```
=LET(r,MOD(A1,33),m,(r<20)+(r<18)+1,s,REPT("HAVE A ",m=2)&"HAPPY NEW YEAR"&REPT(" TO YOU",m=3),a,INDEX({989;1410;1469},m),b,MOD(a-r,33),d,b+SUM((b>{5,8,12,14})*1),LOWER(LEFT(s,d))&MID(s,d+1,26)&REPT("!",(A1-a-b*32)/33))
```
Inspired by @Arnauld 's answer. One of the ingenious things about his solution is that the numbers of cases that start with "HAVE A" is so small, that a different test to account for the spaces is not needed.
[Link to Spreadsheet](https://1drv.ms/x/s!AkqhtBc2XEHJnhvn64_z-8Ny8jwi?e=AM7Jrc)
`=LET(r,MOD(A1,33),` Set r = \$n \mod 33\$.
`m,(r<20)+(r<18)+1,` Set mode. Mode 1 \$> 19\$; Mode 2 \$[18, 19]\$; mode 3 \$< 18\$.
`s,REPT("HAVE A ",m=2)&"HAPPY NEW YEAR"&REPT(" TO YOU",m=3),` if mode 2, prepend "HAVE A "; if mode 3, append " TO YOU".
`a,INDEX({989;1410;1469},m),` Sum of character values for base string
`b,MOD(a-r,33),` distance of base string from \$n \mod 33\$
`d,b+SUM((b>{5,8,12,14})*1),` portion of base string to be converted to lower case. The numbers in the brackets account for the spaces.
`LOWER(LEFT(s,d))&MID(s,d+1,26)&REPT("!",(A1-a-b*32)/33))` return the string with the appropriate number of characters converted to lower case plus the number of "!" to reach the target number.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 83 bytes
```
ＮθＦ²Ｆ²«≔⁺⁺×ιHAVE A ”＆±″/⍘=↧5"ＳA”×κ TO YOUκＦ⊕Ｌκ⊞υ⭆κ⎇‹νλμ↧μ»Ｗ⬤υ⁻θΣＥκ℅μ≔⁺υ!υ⊟Φυ⁼θΣＥι℅λ
```
[Try it online!](https://tio.run/##VZDBToQwEIbPy1OMnKYJJobrnjhg3GR3aRQ1HCtboaEt0FI3xvjsOCwa9dJpZub/5p@pW@HqXuh53tkhTMdgXqTDkW2j194Bpgx@4ke0ybxXjUWug1@fUhnpUSUQ32VPOWQQs8uf8wqO@TNUeXa/pNa@jmpQFlAVjzGjbEdTNhf8ztZOGmknecK9tM3UYscYAx58iyGBh8kp2xzEsDBK6axw79ToPdoENKFMAvv@LF0tvERDUkJ/RudWaQmYab1ADsqS5ZFoweA3q3AnZYVeJTTw74Ykia8W94FgnAxMyPsBb5We6ERUzccg9D@i@iXqlci285zepOl8/aa/AA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
Ｎθ
```
Input the desired year.
```
Ｆ²Ｆ²«
```
Loop over the possibilities of prefixes and suffixes.
```
≔⁺⁺×ιHAVE A ”＆±″/⍘=↧5"ＳA”×κ TO YOUκ
```
Take the compressed string `HAPPY NEW YEAR` and optionally prefix or suffix it as appropriate.
```
Ｆ⊕Ｌκ⊞υ⭆κ⎇‹νλμ↧μ
```
Generate more than enough permutations of the case of the characters in the strings. (In particular, unnecessary extra copies are created for each space in the string, because it's golfer not to filter them out.)
```
»Ｗ⬤υ⁻θΣＥκ℅μ
```
While none of the strings has the desired character sum...
```
≔⁺υ!υ
```
... append an `!` to each string.
```
⊟Φυ⁼θΣＥι℅λ
```
Output one of the strings with the desired character sum.
74 bytes for a version that's too slow for TIO:
```
ＮθＦθＦ²Ｆ²«≔⁺⁺×κHAVE A ”＆±″/⍘=↧5"ＳA”×λ TO YOUλＦ⊕Ｌλ⊞υ⁺⭆λ⎇‹ξμν↧ν×ι!»⊟Φυ⁼θΣＥι℅λ
```
[Try it online!](https://tio.run/##RY9LT8MwDIDP668wPTlSkEavO/VQxKSxRqyAegxdaCPStM2DhxC/vbidBDk4lmN/n9N00jWDNPO8t2MMx9i/KIcT2yWvgwO82TJYk@z//k42ufe6tShM9JdQ6V55fOOQ3uVPBeSQsjUXooZj8Qx1kT8spUufoTeoSqjLx5RR1ZBus@L3tnGqVzaoMx6UbUOHhjEGIvoOI4dVdgpO2/ZejguoUs5K90Xd3uMnh554lsNh@FCukV6hZexPrEl8RUry/SSCKAHFMOKtNoF@TfxiitJ4nDicYo@LgkZKd9ZWmnWT5ezmOdtm2Xz9bn4B "Charcoal – Try It Online") with the second `θ` changed to `χ` so it does at least work for some years.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 52 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
“ṭ½IṾFhĿ_)Wẓȷ⁻⁸ẉ»ḲŒP“7dx‘ịK€ŒuƤŻoƊ€Ẏ;þḶ”!xⱮƊẎOS⁼ɗƇ⁸Ḣ
```
A monadic Link accepting a positive integer that yields a list of characters (or 0). Or a full program that prints a valid result (or 0).
[Don't Try it online!](https://tio.run/##AXgAh/9qZWxsef//4oCc4bmtwr1J4bm@RmjEv18pV@G6k8i34oG74oG44bqJwrvhuLLFklDigJw3ZHjigJjhu4tL4oKsxZJ1xqTFu2/GiuKCrOG6jjvDvuG4tuKAnSF44rGuxorhuo5PU@KBvMmXxofigbjhuKL///85ODk "Jelly – Try It Online") (it won't finish)
Rather, see this slightly altered **[test-suite](https://tio.run/##y0rNyan8//9Rw5yHO9ce2uv5cOc@t4wj@@M1wx/umnxi@6PG3Y8adzzc1Xlo98Mdm45OCgAqNE@peNQw4@Hubu9HTWuOTio9tuTo7vxjXUDOw1191of3WRkbP9yx7VHDXMWKRxvXHdsEFPUPftS45@T0Y@0gw3Ys@n90z@F2oPpIEP7/P9rSwlJHwdDc0hRIWkJJCzAJFDcyMDQBkpaW5rEA "Jelly – Try It Online")** which considers fewer possible strings (by using \$\frac{1}{33}\$ as many optional trailing `!`s).
### How?
```
“...»ḲŒP“7dx‘ịK€ŒuƤŻoƊ€Ẏ;þḶ”!xⱮƊẎOS⁼ɗƇ⁸Ḣ - Link: integer, N
“...»                                    - "have a happy new year to you"
     Ḳ                                   - split at spaces
      ŒP                                 - powerset
        “7dx‘                            - [55,100,120]
             ị                           - index into -> [["happy", "new", "year"], ["have", "a", "happy", "new", "year"], {"happy", "new", "year", "to", "you"]]
              K€                         - join each with spaces -> ["happy new year", "have a happy new year", "happy new year to you"]
                     Ɗ€                  - last three links as a monad for each:
                ŒuƤ                      -   upper-case prefixes
                   Ż                     -   prefix with a zero
                    o                    -   logical OR (the full lower-case)
                       Ẏ                 - tighten
                               Ɗ         - last three links as a monad - f(N):
                          Ḷ              -   [0..N-1]
                           ”!            -   '!'
                             xⱮ          -   map with times -> ["", "!", "!!", ..., '!'*(N-1)]
                        ;þ               - table with concatenate
                                Ẏ        - tighten
                                      ⁸  - use N as the right argument of...
                                     Ƈ   - filter keep those for which:
                                    ɗ    -   last three links as a dyad - f(possibles, N):
                                 O       -     ordinals
                                  S      -     sum
                                   ⁼     -     equals N?
                                       Ḣ - head -> valid result or 0 if none exists
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), 128 bytes
```
->y{n=y%33
s="HAVE A HAPPY NEW YEAR TO YOU"[n/2==9?0:7,w=21-n/20*7]
w.times{|i|(n=y-s.sum)%33>0&&s[i]=s[i].downcase}
s+?!*n/=33}
```
[Try it online!](https://tio.run/##RY3bCoJAEIbvfYpNKMrW41K2wRheCF2lRAdEvOigoKBJq4ioz24rQd38zDDffP@7ujdDDINsNW0OzZQQgYG4ty8OstHe9jwfHZwr8h37iE4u8t2zGOSqAUB32tbENRi6zHdNMkOhVsoki1jbJd2cu2SmsCpbcKWlzWYsSEIYQ3m@6vxxY1EvsOVuIuUqENIPgU7pCtMNxbrJB4NSM1SyWzHaChQkuISYf@NylIa9QMi/jd9TWOsSIcsvl/644QM "Ruby – Try It Online")
Similar to Arnauld's answer. Some notes on how it works:
* `"HAPPY NEW YEAR".sum % 33` is 32. Converting a letter to lowercase adds 32, reducing the remainder by 1 for each letter down to a minimum of 20 when all 12 letters are in lowercase.
* `"HAVE A HAPPY NEW YEAR".sum % 33` is 24. We convert 5 or 6 letters to lowercase to cover years of remainder 19 and 18.
* `"HAPPY NEW YEAR TO YOU".sum % 33` is 17. We convert 0 to 17 letters to lowercase to cover years of remainder 17 down to 0.
It's a fairly simple approach, avoiding the sophisticated formulas in Arnauld's answer (although it wouldn't work without them being true).
* Select the appropriate substring from `HAVE A HAPPY NEW YEAR TO YOU` as above
* Convert characters from uppercase to lowercase until `(y-s.sum)%33` is zero
* Append `(y-s.sum)/33` exclamation marks
`n` is used in two different ways. First it represents `y%33`. Later it represents `y-s.sum`. The reason for the re-use of `n` is that the second assignment occurs inside a loop and if a new variable were used (implicitly declared) inside the loop, it would not be recognised outside the loop, causing an error.
[Answer]
# Python3, 211 bytes:
```
lambda y:next(f(y,'',s:='happy new year to you'),0)or next(f(y,'','have a '+s))
def f(y,s,j):
 if(v:=sum(map(ord,s)))==y:yield s
 if v<y:
  for i in{(k:=['!',j][j!=''])[0],k[0].upper()}:yield from f(y,s+i,k[1:])
```
[Try it online!](https://tio.run/##VY7RasMgFIbv@xSnV3qojKTbaJX5JFkuHFGqbVQ0ySZjz55ZtkF2c@Dn@/j/E8t0Cf7xHNPq5Ot6U@PboKAIrz8mamhhhLAsJLmoGAt4/Q5FqwRTgBJmgqzBkGArV3PRoIAcMuJu0AbuIDOHYgfW0EXIPI90VJGGNLAqoZRFFKtvA@S7AstLqS6Y2mzB@k96FbIje8Jc37m9JKTHrunZtZ6HOUadKH79FpgUxp/Bg61CK3pcY7J@oo4em/apvvQXW86fN5Gf@Rae/sEj5yfE9Rs)
]