37
\$\begingroup\$

In the Anno video game series there are 6 games with a 7th one announced for early 2019. Their titles always feature a year in a specific pattern:

Anno 1602, Anno 1503, Anno 1701, Anno 1404, Anno 2070, Anno 2205, Anno 1800

  • The digital sum is always 9.
  • The years are four digits long.
  • They contain at least one zero.

Within these constrains there exist 109 possible titles:

[1008,1017,1026,1035,1044,1053,1062,1071,1080,1107,1170,1206,1260,1305,1350,1404,1440,1503,1530,1602,1620,1701,1710,1800,2007,2016,2025,2034,2043,2052,2061,2070,2106,2160,2205,2250,2304,2340,2403,2430,2502,2520,2601,2610,2700,3006,3015,3024,3033,3042,3051,3060,3105,3150,3204,3240,3303,3330,3402,3420,3501,3510,3600,4005,4014,4023,4032,4041,4050,4104,4140,4203,4230,4302,4320,4401,4410,4500,5004,5013,5022,5031,5040,5103,5130,5202,5220,5301,5310,5400,6003,6012,6021,6030,6102,6120,6201,6210,6300,7002,7011,7020,7101,7110,7200,8001,8010,8100,9000]

Your objective is to list them all in any reasonable form in the fewest number of bytes.

\$\endgroup\$
9
  • \$\begingroup\$ How flexible is the output format? Is this acceptable? \$\endgroup\$
    – Luis Mendo
    Oct 15, 2018 at 14:50
  • 1
    \$\begingroup\$ @LuisMendo Yes, that's fine with me. \$\endgroup\$
    – Laikoni
    Oct 15, 2018 at 14:54
  • \$\begingroup\$ Are lists of digits allowed? \$\endgroup\$ Oct 15, 2018 at 18:18
  • 1
    \$\begingroup\$ @aslum I assume you mean a lot of spaces, not just one, right? Comment markdown doesn't allow for a good representation of that. And I would assume that's allowed, given that Luis's format above is allowed. ;-) \$\endgroup\$ Oct 15, 2018 at 20:20
  • 1
    \$\begingroup\$ @EriktheOutgolfer I'd say no to lists of digits because they really do not look like years anymore. \$\endgroup\$
    – Laikoni
    Oct 15, 2018 at 21:50

53 Answers 53

1
2
1
\$\begingroup\$

Stax, 13 bytes

ü┌o☻≈;╫▄mI░↨Z

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

VM      Constant one million
        this program produces its output pretty quickly, but takes much longer to end
f       filter the numbers [1..n] using the rest of the program as a predicate
  $     convert to ascii decimal string e.g. "1234"
  |+    sum of ascii codes
  201=  is equal to 201 [result a]
  _E:*  product of the decimal digits [result b]
  >     [result a] is greater than [result b]

Run this one

\$\endgroup\$
1
  • \$\begingroup\$ @Laikoni: I read the challenge wrong, and missed that requirement. I've updated my submission, at the cost of 4 bytes. \$\endgroup\$
    – recursive
    Oct 16, 2018 at 0:20
1
\$\begingroup\$

J, 38 bytes

-1 byte and bug fixed thanks to Quintec

echo(#~(4#10)(0&e.*9=+/)@#:])1e3+i.9e3

Try it online!

Thanks to Laikoni for findng an even bigger bug!

\$\endgroup\$
4
  • \$\begingroup\$ 1000 can be 1e3 for a byte saved, right? \$\endgroup\$
    – Quintec
    Oct 15, 2018 at 17:12
  • 1
    \$\begingroup\$ (Also, should it be 9e3 instead of 9e4?) \$\endgroup\$
    – Quintec
    Oct 15, 2018 at 17:17
  • \$\begingroup\$ @Quintec Yes, sure. I tried it, but forgot it (golfing on the phone) \$\endgroup\$ Oct 15, 2018 at 17:18
  • \$\begingroup\$ @Laikoni As in my Red answer, I forgot this, sorry! Fixed. \$\endgroup\$ Oct 16, 2018 at 6:29
1
\$\begingroup\$

K (oK), 37 bytes

x@&{9=+/x*0in x}'(4#10)\'x:1000+!9000

Try it online!

On mobile, will add explanation later. Couldn't get 0 in' to work without the lambda which wastes bytes...

\$\endgroup\$
1
  • \$\begingroup\$ 36 - x@&9=+/+y*|/~+y:(4#10)\'x:1000+!9000 \$\endgroup\$
    – mkst
    Oct 16, 2018 at 22:45
1
\$\begingroup\$

JavaScript (SpiderMonkey), 57 bytes

for(y=0;y<9e3;)/(?=([0-4].?){3}).*0/.test(y+=9)&&print(y)

Try it online!


JavaScript (SpiderMonkey), 58 bytes

for(y=999;++y<1e4;9-p-q-r-s|q*r*s||print(y))[p,q,r,s]=''+y

Try it online!

You may change print to alert to test it in your browser. :)

\$\endgroup\$
1
  • \$\begingroup\$ Maybe the first time I realize that submitting a whole program is even shorter than a function in JavaScript... \$\endgroup\$
    – tsh
    Oct 16, 2018 at 8:19
1
\$\begingroup\$

K (ngn/k), 33 bytes

t@&"0"=*'t@'<'t:$&201=+/'i:$!9001

Try it online!

Will edit with explanation later.

\$\endgroup\$
0
1
\$\begingroup\$

Julia 1.0, 52 bytes

f()=[x for x=1:9^5 if'0' in"$x"&&sum(Int,"$x")==201]

Try it online!

\$\endgroup\$
0
1
\$\begingroup\$

C (clang), 95 91 bytes

f(i){char*c;for(i=1e4;--i;*c+c[1]+c[2]+c[3]-201||index(c,48)&&puts(c))asprintf(&c,"%d",i);}

Try it online!

-4 bytes thanks to @ceilingcat

\$\endgroup\$
0
1
\$\begingroup\$

Common Lisp, 140 bytes

(defun x()(loop for x from 1008 to 9000 for y =(map'list #'digit-char-p(prin1-to-string x))when(and(member 0 y)(=(apply #'+ y)9))collect x))

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ This does not ensure that each year contains at least one zero. \$\endgroup\$
    – Laikoni
    Oct 18, 2018 at 21:27
  • 1
    \$\begingroup\$ Edited, i must have overlooked that \$\endgroup\$
    – JRowan
    Oct 21, 2018 at 22:36
  • \$\begingroup\$ It looks like you can drop the space in map 'list. Also there are two trailing newlines that do not appear to be necessary. \$\endgroup\$
    – Laikoni
    Oct 22, 2018 at 5:40
1
\$\begingroup\$

Haskell, 66 79 74 bytes

drop 55[x>>=show|x<-sequence.replicate 4$[0..9],sum x==9,elem 0x]

Damn you with your type juggling, you can treat numbers as strings :(

Basically just an exact description of whats asked here. Just no spaces.

Try it online! (Modifications for it to run were necessary, tio doesn't support pure functions in Haskell.)

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Nice approach, but unfortunately lists of digits are not valid output. However, there a few ways you can save some bytes to make up for the additional ones you need for the final conversion to a string. First of all, a list comprehension is shorter than filter and you can drop the second space in elem 0 x. \$\endgroup\$
    – Laikoni
    Oct 22, 2018 at 22:01
  • \$\begingroup\$ A few more: Instead of using map the conversion to string can be put into the list comprehension. concat.map is common enough that there exists a build-in called concatMap, however the overloading of the bind operator =<< on lists has the same effect and is even shorter. \$\endgroup\$
    – Laikoni
    Oct 23, 2018 at 14:13
  • \$\begingroup\$ The default bind operator is (>>=), what you showed is the reverse bind operator. \$\endgroup\$ Oct 24, 2018 at 9:59
1
\$\begingroup\$

MathGolf, 16 13 bytes

♫♪↨Ç{▒ε*\Σ8=┌

Try it online!

Explanation:

  ↨              Range from 
♫♪               1000 to 10000
   Ç{            Filter out by
     ▒ε*         The product of all digits is not 0
        Σ8=┌     The digit sum of the loop index (0 based) is not equal to 8
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Does MathGolf have an ord command to transform characters/digits to unicode values? If yes, you might be able to use the same ord sum == 201 trick other answers use, which covers both the length == 4 and digit sum == 9 at the same time. \$\endgroup\$ Oct 16, 2018 at 8:02
  • \$\begingroup\$ @KevinCruijssen Unfortunately, it doesn't, but I've requested it \$\endgroup\$
    – Jo King
    Oct 16, 2018 at 11:48
  • \$\begingroup\$ @JoKing I'm a bit late to the party, but both of these things have been implemented, and have been submitted for pulling to TIO. check out the README for instructions. I've had a very stressful month, but I'm trying to get everything implemented. \$\endgroup\$
    – maxb
    Nov 27, 2018 at 10:18
0
\$\begingroup\$

Retina 0.8.2, 52 bytes


9001$*
.
$.`=>$.`;
>|\G\d
$*
M!`\d{4}(?==1{9};)
G`0

Try it online! Explanation:


9001$*

Insert 9001 1s.

.
$.`=>$.`;

Replace each 1 with the number of 1s to its left, followed by =>, followed by the same number again, followed by ;.

>|\G\d
$*

Convert > and subsequent digits to unary, thus calculating the digit sum.

M!`\d{4}(?==1{9};)

List all the 4-digit numbers with a digit sum of 9.

G`0

Keep those that contain a zero.

\$\endgroup\$
0
\$\begingroup\$

Red, 109 bytes

repeat n 8000[s: 0 p: 1 foreach t form d: 1000 + n[s: s + r: do t - 48 p: p * r]if all[s = 9 p = 0][print d]]

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @Laikoni Sorry, apparently I've missed that part. Fixed. \$\endgroup\$ Oct 16, 2018 at 6:20
0
\$\begingroup\$

Pyth, 22 19 bytes

f&}\0Tq201sCMT`M^T4

Output is an array of strings. Try it online here.

f&}\0Tq201sCMT`M^T4   
              `M^T4   Array of strings of numbers 0-999
f                     Keep the elements of the above, as T, where:
           CMT          Get the code points of each character in T
          s             Take the sum
      q201              Does the above == 201?
  }\0T                  Does T contain "0" ?
 &                      Logical AND the two previous results

The 201 trick is the same as used in other answers.

Previous answer, without using 201 trick, 22 bytes: iRTf}0Tfq9sTjRTr^T3^T4

\$\endgroup\$
0
\$\begingroup\$

Jelly, 13 bytes

ȷ4ȷrDS9Ƒ>ẠƲƇḌ

Try it online!

-2 (yucky, yucky trivial) bytes thanks to Dennis.

\$\endgroup\$
0
0
\$\begingroup\$

PHP, 114 bytes

<?php
for($i=1e3;$i<=9e3;$i++)
    if(array_sum($a=[$i/1e3%10,$i/100%10,$i/10%10,$i%10])==9&in_array(0,$a))
        echo $i.' ';

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Prolog 117 bytes

b(X):-between(0,9,X). w(X):-write(X). y():-b(A),b(B),b(C),b(D),X is A+B+C+D,X=9,(A=0;B=0;C=0;D=0),w(A),w(B),w(C),w(D).

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Would you consider adding a link to a service like Try it online! - (link is to your code, but I'm not sure if the footer is right since I don't prolog much.)? \$\endgroup\$
    – Οurous
    Oct 18, 2018 at 5:25
  • \$\begingroup\$ 98 \$\endgroup\$
    – ASCII-only
    Nov 14, 2018 at 5:07
  • \$\begingroup\$ 91 \$\endgroup\$
    – ASCII-only
    Nov 14, 2018 at 5:18
0
\$\begingroup\$

J, 35 bytes

echo@>55}.I.(4#10)(*/<9=+/)@#:i.1e4

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C# (.NET Core), 106 bytes

for(int i=999;++i<9999;)if(i.ToString().Sum(c=>c-'0')==9&&i.ToString().Contains("0"))Console.WriteLine(i);

Try it online!

Ungolfed:

for(int i = 999; ++i < 9999;)               // from 1000 to 9999 (fulfills four digit parameter)
    if(i.ToString().Sum(c => c - '0') == 9  // if the sum of the digits is nine
            && i.ToString().Contains("0"))  //   and if the year contains at least one zero
        Console.WriteLine(i);               // write the year to the console
\$\endgroup\$
0
\$\begingroup\$

Scala, 58 bytes

jrook's solution is currently 2 byte shorter

val v=(9 to 9000).filter(x=>(x+"").min<49&(x+"").sum==201)

val v=(9 to 9000).map(_+"").filter(x=>x.min<49&x.sum==201)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Attache, 37 bytes

{{Sum@Ords@_=201and"0"in _}@S\1:9000}

Try it online!

Based on the perl answer.

\$\endgroup\$
0
\$\begingroup\$

Perl 5 -MList::Util=sum, 40 32 bytes

map/0/*9-(sum/./g)||say,1E3..9E3

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 59 bytes

Lambda that returns a List<Int> containing every valid year.

{(1008..9000).filter{"$it".sumBy{it-'0'}==9&&'0' in "$it"}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Deadfish~

{iiiii}dcdcc{i}ddc{d}iiicdciciiiiiicddddddcdciiciiiicdddddcdciiiciicddddcdciiiiccdddcdciiiiicddcddcdciiiiiicddddcdcdc{i}dddcddddddccdc{i}ddc{d}iiciccdc{i}dddcddddddcciiiiiic{d}iiicicicddciiiiiicdddddciciiiicddddddciciicdddciiiiicddddciiciicdddddciciiicddddciiiicdddciiiccddddciciiiicdddddciiicddciiiicddcdddciciiiiicddddddciicdciiiiicddddcddciciiiiiic{d}iiicicciiiiiicddddddcdcic{i}dddc{d}iicciicddcc{i}dddcdddddcddciciiiiicddddcddciiciiicdddcddciiicicddcddciiiicdcdcddciiiiicdddccddciiiiiicdddddcicddc{i}dddc{d}iiiciicdcdciiiiiicddddcdciiiiicddddddciiccddciiiiicdddcciiicdddddciicicdddciiiicddcicicddddciiciicddddciiicdciicdcdddciiciiicdddddciicciiicdddcddciiciiiicddddddciciciiiicdddddcdciiciiiiic{d}iiicciiicdddcciiiiiicdddcdddciciiiicddcdddciiciicdcdddciiicccdddciiiicddcicdddciiiiicddddciicdddciiiiiicddddddciiicddcdciiiiicddcddciiiicdddddciiicdcddciiiicdcdciicddddciiiccdddciiiccccdddciiicicddddciicicicddcddciiiciicdddddciciiciicddddcdciiiciiicddddddcciiiicddddcciiiiicdcddddciciiiccddddciicicicddddciiicdciicddddciiiicdddciiicddddciiiiicdddddciiiicdddcdciiiiccdddciiicddddciiiicddcddciiicicddcicdddciiiicdcdddciiciicdcdcddciiiiccddddciciiiccdddcdciiiicicdddddcciiiiicdddddcciiiicicdddddciciiciicdddddciicciiicdddddciiicddciiiicdddddciiiicddddciiiiicddddcdciiiciicddddciicdddciiiiicdddcddciiciiicdddccddciiiiicddcdddciciiiicddcddcdciiiiicdcddddcciiiiiicddddddcciiiciiicddddddciciciiiicddddddciicdciiiiicddddddciiicdddciiiiiicdddddcdciiciiiicdddddcicddciiiiiicddddcddciciiiiicddddcdcdciiiiiicdddcdddcc{i}dddc{d}iiicciiciiiiic{d}iiicicciiiiiic{d}iiiciicddc{i}dddcddddddcdciciiiiiicddddddccdc{i}dddcdddddcddcc{i}ddc{d}iiccic{i}dddc{d}iicicdc{i}ddc{d}iiicdcc{i}dc{d}iccc

Do I need the spaces?

\$\endgroup\$
2
  • \$\begingroup\$ Yes, there should be some delimiter between the years such that they are easily recognizable. \$\endgroup\$
    – Laikoni
    Mar 22, 2021 at 14:19
  • \$\begingroup\$ Also please add the byte count of your answer to the header and, if you want, add a TIO link: Try it online! \$\endgroup\$
    – Laikoni
    Mar 22, 2021 at 14:19
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.