27
\$\begingroup\$

Print the dates of all the Sundays in 2017 in the following format : dd.mm.yyyy.
Expected Output:

01.01.2017
08.01.2017
15.01.2017
22.01.2017
29.01.2017
05.02.2017
12.02.2017
19.02.2017
26.02.2017
05.03.2017
12.03.2017
19.03.2017
26.03.2017
02.04.2017
09.04.2017
16.04.2017
23.04.2017
30.04.2017
07.05.2017
14.05.2017
21.05.2017
28.05.2017
04.06.2017
11.06.2017
18.06.2017
25.06.2017
02.07.2017
09.07.2017
16.07.2017
23.07.2017
30.07.2017
06.08.2017
13.08.2017
20.08.2017
27.08.2017
03.09.2017
10.09.2017
17.09.2017
24.09.2017
01.10.2017
08.10.2017
15.10.2017
22.10.2017
29.10.2017
05.11.2017
12.11.2017
19.11.2017
26.11.2017
03.12.2017
10.12.2017
17.12.2017
24.12.2017
31.12.2017
\$\endgroup\$
9
  • 1
    \$\begingroup\$ Possible dupe of Plan your Sundays? This one is a specific year though. \$\endgroup\$
    – xnor
    Apr 13, 2017 at 3:19
  • 1
    \$\begingroup\$ I checked out Plan your Sundays before posting, but it asks to print the Sundays in a given month. @xnor \$\endgroup\$
    – smanna
    Apr 13, 2017 at 3:21
  • 1
    \$\begingroup\$ Any particular reason for that specific output format? You could open in up a bit. \$\endgroup\$
    – Riker
    Apr 13, 2017 at 3:22
  • 4
    \$\begingroup\$ I actually like this as an exact-text kolmogorov challenge. Getting the date formatting right has some interesting optimizations. \$\endgroup\$
    – xnor
    Apr 13, 2017 at 3:23
  • 5
    \$\begingroup\$ Anyways, I think the close votes should be retracted. \$\endgroup\$ Apr 13, 2017 at 8:28

50 Answers 50

16
\$\begingroup\$

Python 2, 81 bytes

x=0
exec"print'%05.2f.2017'%(x%30.99+1.01);x+=7+'0009ANW'.count(chr(x/7+40));"*53

Try it online!

No date libraries, computes the dates directly. The main trick is to treat the dd.mm as a decimal value. For example, 16.04.2017 (April 16) corresponds to the number 16.04. The number is printed formatted as xx.xx with .2017 appended.

The day and month are computed arithmetically. Each week adds 7 days done as x+=7. Taking x modulo 30.99 handles rollover by subtracting 30.99 whenever the day number gets too big. This combines -31 to reset the days with +0.01 to increment the month.

The rollover assumes each month has 31 days. Months with fewer days are adjusted for by nudging x upwards on certain week numbers with +[8,8,8,17,25,38,47].count(x/7). These list is of the week numbers ending these short months, with 8 tripled because February is 3 days short of 31.

This list could is compressed into a string by taking ASCII values plus 40. The shift of +40 could be avoided by using unprintable chars, and could be accessed shorter as a bytes object in Python 3.

\$\endgroup\$
1
  • \$\begingroup\$ What a cool answer! '888z!}+'.count(chr(x%146)) saves one byte. \$\endgroup\$
    – Lynn
    May 11, 2018 at 7:51
10
\$\begingroup\$

PHP, 48 bytes

while($t<53)echo gmdate("d.m.2017
",605e3*$t++);

PHP, 46 bytes (for non-negative UTC offsets)

while($t<53)echo date("d.m.2017
",605e3*$t++);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Shouldn´t that be gmdate for timezone safety? It fails on onlinephpfunctions.com. Great work in all other aspects! \$\endgroup\$
    – Titus
    Apr 14, 2017 at 17:42
9
\$\begingroup\$

Python 2, 90 79 bytes

-5 bytes with the help of xnor (avoid counting the weeks themselves)
-1 byte thanks to xnor (add back in e for 605000 as 605e3)

from time import*
i=0
exec"print strftime('%d.%m.2017',gmtime(i));i+=605e3;"*53

Try it online!

0 seconds since epoch is 00:00:00 on the 1st of January 1970, which, like 2017 was not a leap year. 605000 seconds is 1 week, 3 minutes, 20 seconds. Adding 52 of these "weeks" does not take us beyond midnight.

\$\endgroup\$
4
  • \$\begingroup\$ Here's a shorter way to generate the arithmetic progression. \$\endgroup\$
    – xnor
    Apr 13, 2017 at 5:37
  • \$\begingroup\$ @xnor Thanks, I was working on the same kind of thing and was trying to go lower, but 81 seems to much to go for with the library approach. \$\endgroup\$ Apr 13, 2017 at 6:00
  • \$\begingroup\$ @xnor ...or not. \$\endgroup\$ Apr 13, 2017 at 6:21
  • 1
    \$\begingroup\$ Nice! The number can be 605e3. I have some ideas though :) \$\endgroup\$
    – xnor
    Apr 13, 2017 at 6:23
7
\$\begingroup\$

Bash + coreutils, 44 bytes

seq -f@%f 0 605e3 32e6|date -uf- +%d.%m.2017

may save 2 bytes -u if GMT is assumed

  • Thanks Digital Trauma point out -f paramter for date which saves 10 bytes;
  • And use 2017 in format string saves more bytes which idea is from the answer given by user63956

  • @0 is 1970-1-1
  • 605000 is one week (604800) plus 200 sec
    • 200 sec. should just work since there are only 52 weeks in a year
  • @32000000 is just a little more than a year
\$\endgroup\$
1
7
\$\begingroup\$

PowerShell, 51 47

0..52|%{date((date 2017-1-1)+7.*$_)-u %d.%m.%Y}

Fairly straightforward. 2017-01-01 is a Sunday, so is every following seven days. We can save two bytes if we only need the script to be working in my lifetime:

0..52|%{date((date 17-1-1)+7.*$_)-u %d.%m.%Y}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can save a few bytes if you, instead of a string, add a double representing number of days, i.e. (date 2017-1-1)+7.*$_ . See this answer \$\endgroup\$ Apr 13, 2017 at 14:00
  • \$\begingroup\$ @DankoDurbić: Oh, wow. I only knew of adding int for ticks and strings for days so far. Nice to know. \$\endgroup\$
    – Joey
    Apr 13, 2017 at 18:18
5
\$\begingroup\$

Excel VBA 106 91 79 bytes

Sub p()
For i = #1/1/2017# To #12/31/2017#
If Weekday(i) = 1 Then MsgBox i
Next
End Sub

saved 15 bytes thanks to @Radhato

Assuming 1/1/2017 is Sunday it will save 12 more bytes.

Sub p()
For i = #1/1/2017# To #12/31/2017#
MsgBox i
i = i + 6
Next
End Sub

Thanks @Toothbrush 66 bytes

Sub p:For i=#1/1/2017# To #12/31/2017#:MsgBox i:i=i+6:Next:End Sub

Edit: (Sub and End Sub is not necessary) 52 bytes

For i=#1/1/2017# To #12/31/2017#:MsgBox i:i=i+6:Next
\$\endgroup\$
8
  • \$\begingroup\$ Can be improved by chanching For i = 42736 To 43100 to For i = #1/1/2017# To #12/31/2017# and then removing Format(...) replacing with just i \$\endgroup\$
    – Radhato
    Apr 13, 2017 at 6:43
  • \$\begingroup\$ @Radhato but wouldn't that increase byte size? \$\endgroup\$ Apr 13, 2017 at 6:44
  • \$\begingroup\$ I think it reduces it to 96.. at least thats what I counted lol \$\endgroup\$
    – Radhato
    Apr 13, 2017 at 6:48
  • \$\begingroup\$ Yes it did. Thanks \$\endgroup\$ Apr 13, 2017 at 6:48
  • 1
    \$\begingroup\$ @newguy Sub p:For i=#1/1/2017# To #12/31/2017#:MsgBox i:i=i+6:Next:End Sub is perfectly valid VBA code and is only 66 bytes. The fact that the VBA Editor adds in extra spaces is irrelevant. \$\endgroup\$
    – Toothbrush
    Apr 13, 2017 at 20:39
4
\$\begingroup\$

PHP, 67 bytes

Using the fact the PHP automatically assign value 1 to undeclared loop variables, and using Linux epoch times,

<?php for(;54>$t+=1;)echo date("d.m.Y\n",604800*($t)+1482624000);?>
\$\endgroup\$
6
  • \$\begingroup\$ Welcome to PPCG! I believe this can be counted as 59 bytes (although I don't golf in PHP myself). \$\endgroup\$ Apr 13, 2017 at 5:04
  • 4
    \$\begingroup\$ <?for(;54>++$t;)echo date("d.m.Y\n",605e3*$t+148263e4); should just be ok \$\endgroup\$
    – tsh
    Apr 13, 2017 at 6:03
  • 2
    \$\begingroup\$ Always nice to see some new PHP golfers ! You're allowed to use php -r so you don't need <?php and ?> and therefore can count this as 59 bytes like @JonathanAllan correctly said. You don't need the brackets around $t. A few other golfs and you end up with for(;++$t<54;)echo date("d.m.Y\n",605e3*$t+14827e5); for 51 bytes (in my time zone). You can replace "\n" with a real line break which is only 1 byte therefore it's 51 byte. \$\endgroup\$
    – Christoph
    Apr 13, 2017 at 12:05
  • 1
    \$\begingroup\$ Thanks @Christoph I'm trying out another approach, will update soon \$\endgroup\$
    – smanna
    Apr 13, 2017 at 15:55
  • \$\begingroup\$ Apart from all the other hints, you might want to use gmdate instead of date for timezone safety. \$\endgroup\$
    – Titus
    Apr 14, 2017 at 17:47
4
\$\begingroup\$

k6, 32 bytes

`0:("."/|"."\)'$2017.01.01+7*!53

Brief explanation:

                2017.01.01+7*!53 /add 0, 7, 14, ..., 364 to January 1st
   ("."/|"."\)'$                 /convert to string, turn Y.m.d into d.m.Y
                                 /   split by ".", reverse, join by "."
`0:                              /output to stdout (or stderr), line by line

Alas, this seems to only work in the closed-source, on-request-only interpreter.

Running the command in the closed-source interpreter.

\$\endgroup\$
4
\$\begingroup\$

Pyke, 26 24 bytes

53 Fy17y"RVs6)c"%d.%m.%Y

Try it online!

53 F                     - for i in range(53):, printing a newline between each
    y17y"                -  Create a time object with the year 2017. (Month and days are initialised to 1.)
         RV  )           -  Repeat i times:
           s6            -   Add 1 week
              c"%d.%m.%Y -  Format in "dd.mm.yyyy" time

Or 11 bytes

If allowed to ignore output format

y17y"52VDs6

Try it online!

y17y"       - Create a time object with the year 2017. (Month and days are initialised to 1.)
     52V    - Repeat 52 times:
        D   -  Duplicate the old time
         s6 -  Add 1 week
\$\endgroup\$
3
\$\begingroup\$

R, 79 67 58 bytes

cat(format(seq(as.Date("2017/01/01"),,7,53),"\n%d.%m.%Y"))

First of January being a sunday, this snippet creates a sequence of days, every 7 days starting from the 01-01-2017 to the 31-12-2017, format them to the desired format and print them.

\$\endgroup\$
2
  • \$\begingroup\$ This should bring it down to 41 bytes print(as.Date("2017-01-01")+seq(7,365,7)) \$\endgroup\$
    – count
    Apr 22, 2017 at 22:17
  • \$\begingroup\$ @count Thanks but it wouldn't print the required output (i. e. "2017.01.01" instead of "2017/01/01") \$\endgroup\$
    – plannapus
    Apr 23, 2017 at 5:43
3
\$\begingroup\$

Befunge-98 (PyFunge), 99 95 93 85 bytes, Leaves trailing newline

All the optimizations were made by @JoKing many thanks to them

s :1g2/10g\%:d1p10g\`+:b`#@_:1\0d1g#;1+:a/'0+,a%'0+,'.,j;a"7102"4k,d1g7+
>8><><>><><>

Try it online!

I felt like we were missing out on some esostericity here so I made a solution in my favourite Esosteric language.

Explanation:
>8><><>><><> Encodes the length of the 12 months
s Store the old day in the blank space
:1g2/ Get an ASCII value from the bottom row and divide it by two this gives us the length of a given month Ex: 8 = 56 in ASCII => 56/2 = 28 => The month (February) has 28 days
10g\% Get the previously saved day and modulo it by the length of the month which allows us to transition the date into the next month
:d1p Save a copy of the new updated day
10g\`+ Test if old date > new date => we transitioned into the next month => add 1 to the month counter
:b` Test if month counter > 11 that means we reached the end of the year (using 0 indexing)
#@_ Based on the previous if terminate the program
:1\0d1g Reorder the stack so it looks like this: Month, 1, Month, 0, Day
# skip the next insctruction (duh)
1+:a/'0+,a%'0+,'., Convert the number to 1 indexing, print, add a . at the end
j; Use the 0 from the stack to not jump and use the ; to go to to the print schedule again then use the 1 to jump over the ; next time
a"7102"4k, Print 2017\n
d1g Get the day value again 7+ Add a week before repeating

\$\endgroup\$
4
  • \$\begingroup\$ @JoKing Omc such an obvious optimization! How could've I missed that one? Thank you. \$\endgroup\$
    – IQuick 143
    May 12, 2018 at 11:51
  • 1
    \$\begingroup\$ A few more bytes off. Try it online! One from changing the -17 jump to a comment instead, one from using 0 based indexing for the month counter and one from changing it to a one-liner \$\endgroup\$
    – Jo King
    May 12, 2018 at 12:54
  • \$\begingroup\$ @JoKing Wow that's a lot of golfing you did there. Lemme add it to the answer. \$\endgroup\$
    – IQuick 143
    May 12, 2018 at 12:58
  • 1
    \$\begingroup\$ Hell, why not make the days 0 indexed as well, and save having to do any of the initialisation at all! 85 bytes \$\endgroup\$
    – Jo King
    May 12, 2018 at 13:38
3
\$\begingroup\$

JavaScript, 111 106 bytes

for(i=14833e8;i<1515e9;i+=605e6)console.log(new Date(i).toJSON().replace(/(....).(..).(..).*/,'$3.$2.$1'))

Note: Stack Exchange's console isn't long enough to display the entire list, so here's the first half as a separate snippet:

for(i=14833e8;i<15e11;i+=605e6)console.log(new Date(i).toJSON().replace(/(....).(..).(..).*/,'$3.$2.$1'))

The custom format costs me 40 bytes...

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 64 bytes

use POSIX;print strftime"%d.%m.%Y\n",0,0,0,7*$_+1,0,117for 0..52

Try it online!

The task given was 2017, not any year, so I hardcoded in:

  • 117 (which is perlish for year 2017, 1900+117)
  • +1 because 1st of January is a sunday in 2017
  • 0..52 because 2017 have 53 sundays

POSIX is a core module and is always installed with Perl5. Doing the same without using modules in 101 bytes, removing whitespace:

$$_[5]==117&&printf"%02d.%02d.%d\n",$$_[3],$$_[4]+1,$$_[5]+1900
  for map[gmtime(($_*7+3)*86400)],0..1e4
\$\endgroup\$
2
\$\begingroup\$

Ruby, 75 bytes

Straightforward solution to figure out the dates with Time.

t=Time.new 2017
365.times{puts t.strftime("%d.%m.%Y")if t.sunday?
t+=86400}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ If you add a whole week(604800 seconds), then you don't need to check for a sunday, just repeat 53 times. \$\endgroup\$
    – G B
    Apr 13, 2017 at 13:38
  • 1
    \$\begingroup\$ If you do the trick suggested by @GB and no longer check for Sunday, you can also initialize with t=Time.new 1 and then do t.strftime"%d.%m.2017" for -1 byte. \$\endgroup\$
    – Value Ink
    Apr 13, 2017 at 22:01
2
\$\begingroup\$

SAS, 52 50 bytes

Saved 2 bytes thanks to @user3490.

data;do i=20820to 21184 by 7;put i ddmmyyp10.;end;
\$\endgroup\$
3
  • \$\begingroup\$ You don't need to specify a dataset - just use data; instead of data c; and that saves 2 bytes. I think you do need a run; though. \$\endgroup\$
    – user3490
    Apr 13, 2017 at 15:31
  • \$\begingroup\$ @user3490 Thanks, I wasn't aware of that. I guess that's the equivalent of data _null_? Also, the run statement is implied if it's missing. \$\endgroup\$
    – J_Lard
    Apr 13, 2017 at 16:33
  • \$\begingroup\$ Not quite equivalent - you end up with an output dataset following the datan naming convention. \$\endgroup\$
    – user3490
    Apr 18, 2017 at 22:17
2
\$\begingroup\$

Mathematica 90 84 bytes

Fairly wordy. numbermaniac and Scott Milner saved 5 and 1 bytes, respectively.

Column[#~DateString~{"Day",".","Month",".","Year"}&/@DayRange["2017","2018",Sunday]]
\$\endgroup\$
2
  • \$\begingroup\$ Do you need the Most@? The output seems to be identical without it. \$\endgroup\$ Apr 22, 2017 at 11:51
  • 1
    \$\begingroup\$ @numbermaniac, Thanks. Most was in there to avoid the first Sunday in 2018. I had originally tested the code, without Most, for years, 2011, 2012, in which case the first Sunday in 2012 is included in the output. (That's why I included it into the code.) Strangely, Most is not needed for 2017-18. Nor does Most have any apparent effect on the result. Mysterious! \$\endgroup\$
    – DavidC
    Apr 22, 2017 at 15:18
1
\$\begingroup\$

VBA, 81 bytes (maybe 64)

Sub p()
For i = 0 To 52
MsgBox format(42736 + i * 7, "dd.mm.yyyy")
Next i
End Sub

My first post. Building on newguy's solution by removing the check for weekdays and just specifying every 7th day. Removing the dates saves 12 bytes a piece. 42736 is 1/1/2017. Output date format depends on system setting. Is that allowed? If so, it's 64 bytes because you don't need the format method.

MsgBox #1/1/2017# + i * 7
\$\endgroup\$
1
  • \$\begingroup\$ You can also remove a lot of the white space that is autoformatted. For instance, and For i=0To 52 and Format(42736+i*7,"dd.mm.yyyy"). Also, you can just use Next instead of Next i. \$\endgroup\$ Apr 13, 2017 at 12:33
1
\$\begingroup\$

AHK, 67 bytes

d=20170101
Loop,52{
FormatTime,p,%d%,dd.MM.yyyy
Send,%p%`n
d+=7,d
}

Nothing magical happens here. I tried to find a shorter means than FormatTime but I failed.

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

Java 8+, 104 100 99 bytes

()->{for(int t=0;t<53;)System.out.printf("%1$td.%1$tm.2017%n",new java.util.Date(t++*604800000L));}

Java 5+, 109 105 104 bytes

void f(){for(int t=0;t<53;)System.out.printf("%1$td.%1$tm.2017%n",new java.util.Date(t++*604800000L));}

Uses the date-capabilities of the printf format.

Test it yourself!

Savings

  1. 104 -> 100: changed the loop values and the multiplicand.
  2. 100 -> 99: golfed the loop
\$\endgroup\$
1
\$\begingroup\$

T-SQL, 94 Bytes

DECLARE @ INT=0,@_ DATETIME='2017'W:SET @+=1SET @_+=7PRINT FORMAT(@_,'dd.MM.yyy')IF @<52GOTO W

if you don't like SQL GOTO or WHILE, here is a 122 bytes CTE solution

WITH C AS(SELECT CAST('2017'AS DATETIME)x UNION ALL SELECT x+7FROM C WHERE X<'12-31-17')SELECT FORMAT(x,'dd.MM.yyy')FROM C
\$\endgroup\$
2
  • \$\begingroup\$ your first solution starts at 08.01.2017 not 01.01.2017 \$\endgroup\$ Apr 14, 2017 at 9:10
  • 1
    \$\begingroup\$ Very good tricks in your solution. Good job. I couldn't resist to borrow them. ;) \$\endgroup\$
    – AXMIM
    Apr 14, 2017 at 16:11
1
\$\begingroup\$

Ruby, 60+7 = 67 bytes

Uses the -rdate flag.

(d=Date.new 1).step(d+365,7){|d|puts d.strftime"%d.%m.2017"}
\$\endgroup\$
1
\$\begingroup\$

Groovy, 81 77 63 60 56 bytes

d=new Date(0);53.times{printf('%td.%<tm.2017%n',d);d+=7}

The above can be run as groovy script.

My first code golf entry. Fortunately, the year 1970 was not a leap year, thus can use it as a base.

Thanks to Dennis, here's a: Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! In case you're interested, here's a permalink: Try it online! \$\endgroup\$
    – Dennis
    Apr 14, 2017 at 5:35
  • \$\begingroup\$ Thank you @Dennis I didn't know this existed :) \$\endgroup\$
    – Krystian
    Apr 14, 2017 at 6:05
  • \$\begingroup\$ You can save four bytes by moving the date inside the times block, 53.times{printf('%td.%<tm.2017%n',new Date(0)+it*7)}, 52 bytes. Defending groovy's honor here... \$\endgroup\$ May 12, 2018 at 10:36
1
\$\begingroup\$

C#, 138 111 102 bytes

Saved 9 more bytes thanks to Johan du Toit!

Saved 27 bytes thanks to Kevin Cruijssen's suggestions!

()=>{for(int i=0;i<53;)Console.Write(new DateTime(2017,1,1).AddDays(7*i++).ToString("dd.MM.yyyy\n"));}

Anonymous function which prints all Sundays in 2017.

Full program with ungolfed method:

using System;

class P
{
    static void Main()
    {
        Action f =
        ()=>
        {
            for (int i = 0; i < 53; )
                Console.Write(new DateTime(2017, 1, 1).AddDays(7 * i++).ToString("dd.MM.yyyy\n"));
        };

        
        
        f();
    }
}
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Isn't it easier to just use .AddDays(7*i++)? Then there is no need for the .DayOfWeek<1 check. We know 01-01-2017 is a Sunday, and from there we can just keep adding 7 days. \$\endgroup\$ Apr 13, 2017 at 13:04
  • 2
    \$\begingroup\$ Not sure about this -- in all code golfs I partake, I always used the complete namespace -- but aren't you missing some Systems in there -- System.DateTime and System.Console.Write? \$\endgroup\$
    – auhmaan
    Apr 13, 2017 at 13:32
  • \$\begingroup\$ Cool but you can save a couple of bytes: ()=>{for(int i=0;i<53;)Console.Write(new DateTime(2017,1,1).AddDays(7*i++).ToString("dd.MM.yyyy\n"));}; \$\endgroup\$
    – jdt
    Apr 13, 2017 at 20:39
  • \$\begingroup\$ @auhmaan: Yes, either full namespaces should be used, or the using statements should be included in the byte count. \$\endgroup\$
    – raznagul
    Apr 14, 2017 at 8:14
  • 3
    \$\begingroup\$ @adrianmp: Necessary using statements to run the code must be counted. See this Meta question: Do I need to use imports or can I call a class explicity? \$\endgroup\$
    – raznagul
    Apr 14, 2017 at 10:12
1
\$\begingroup\$

C#, 110 109 bytes

using System;()=>{for(int i=42729;i<43100;Console.Write(DateTime.FromOADate(i+=7).ToString("dd.MM.yyy\n")));}

You can enjoy this program online here

In this soluion we :

  • Use OLE Automation Date (OADate) to avoid "AddDay()" from datetime.
    FromOADate() seem big but it's equal to new DateTime(2017,1,1)

  • Start the loop on the last sunday of 2016. to allow us to increment with operator += only. This operator return the value after the increment is done.

  • Increment by 7 days to jump from sunday to sunday before printing the date.

  • We stop once the last sunday of 2017 has been hit.

  • Use Debug instead of Console to save two characters

  • Avoid having explicit variable declaration and assignment

\$\endgroup\$
6
  • \$\begingroup\$ Unless otherwise specified, our defaults say you should provide a function or program (i.e. not a snippet), and you need to qualify Debug and DateTime: I'd recommend adding using System; and switching to Console from Debug (which is a tad dodgy itself, but I can't find any commentary on meta regarding it). \$\endgroup\$ Apr 14, 2017 at 8:08
  • 1
    \$\begingroup\$ @VisualMelon I've complied with "provide a function" and I have also replace Debug for Console since Debug required a specific include. However, I didn't comply with the 'use system' because my competitor in this language doesn't do it. Also, you can't code outside a class which itself required to be defined within a namespace. I'm pretty sure most c# answer on this site doesn't include that. So what now, do we throw all these answers to the garbage? Do we downvote them all to force them to comply? If so, then the community might as well ban c# from codegolf altogether. \$\endgroup\$
    – AXMIM
    Apr 14, 2017 at 14:20
  • \$\begingroup\$ These 'rules' are just what the community has over time settled on (and documented on meta). We don't downvote answers, we comment and don't upvote them either. When they fix them, then we are free to upvote. Both other C# answers have comments suggesting they should add the using directive or fully qualify the methods and types, please don't take any of this personally. I'm a long time C# golfer, and I appreciate how arbitrary the rules seem, so I like to nudge people in the right direction when I can. I specifically 'target' C# answers because I'm relatively well informed on the matter. \$\endgroup\$ Apr 14, 2017 at 15:26
  • 1
    \$\begingroup\$ @VisualMelon Fair enough, I'm just a passerby here anyway. So I shall accept your rules while I'm here. Therefore, I've attempted to make my answer comply this time. \$\endgroup\$
    – AXMIM
    Apr 14, 2017 at 17:13
  • 1
    \$\begingroup\$ Looks good to me! +1 for the original approach. And you don't need the space after using System; (I assume that wasn't intentional) so that's 109 bytes. \$\endgroup\$ Apr 14, 2017 at 18:59
1
\$\begingroup\$

TSQL, 112 105 bytes

SELECT CONVERT(VARCHAR,DATEADD(d,number*7,42734),104)FROM master..spt_values WHERE type='p' AND number<53

Demo

T-SQL Convert Syntax

\$\endgroup\$
2
  • \$\begingroup\$ You can save 2 bytes by replacing DAY by d. It does the same. \$\endgroup\$
    – AXMIM
    Apr 14, 2017 at 14:53
  • \$\begingroup\$ +1 You can save another 3 bytes by replacing '20170101' with 42734. Dates are number. Time is the decimals part of the number. \$\endgroup\$
    – AXMIM
    Apr 14, 2017 at 15:08
1
\$\begingroup\$

JavaScript (ES6), 123 bytes

It's my first post here, hello!

a=x=>`0${x}.`.slice(-3);[].map.call('155274263153',(x,i)=>{for(j=0;j<4+(2633>>i&1);j++)console.log(a(+x+j*7)+a(i+1)+2017)})

This solution uses hardcoded data and is designed to work specifically for the year 2017. It relies on no date/time APIs.

As for the digits in the string 155274263153, each digit is a number of its own and denotes the first Sunday of each consecutive month. Output for the entire year can be generated by successively adding 7 to those.

What about the magic number, 2633, used in the loop?
Well... 2633 (decimal) is 101001001001 in binary. Now what could those 1s mean? Starting from the right, the 1st, 4th, 7th, 10th and 12th bit are set. This corresponds to months which happen to have five Sundays, as opposed to those that have only four. Golfed down to this neat expression, it initially looked like this: for(j=0;j<4+ +[0,3,6,9,11].includes(i);j++).

I guess the remaining parts are fairly self-explanatory.

\$\endgroup\$
3
  • \$\begingroup\$ @SIGSEGV: I don't mind changing ECMAScript 2015 to Javascript (ES6), but... you broke my code and I had to revert it. \$\endgroup\$
    – rhino
    Apr 15, 2017 at 4:41
  • \$\begingroup\$ Oh, that's the community's consensus, having only the lambda part is allowed. \$\endgroup\$ Apr 15, 2017 at 4:45
  • \$\begingroup\$ @SIGSEGV This is not the case here. This lambda contains only a small portion of the code, and I need that identifier to be able to use it elsewhere. Without the a= the code is actually broken. \$\endgroup\$
    – rhino
    Apr 15, 2017 at 4:51
1
\$\begingroup\$

T-SQL, 79 77 Bytes

After helping Salman A improve his answer. I decided to write my own using a loop and PRINT.

I ended with this 90 bytes solution.

DECLARE @d DATETIME=42734 WHILE @d<43100BEGIN PRINT CONVERT(VARCHAR,@d,104)SET @d=@d+7 END

Then I looked at the current leader in T-SQL which was 94 bytes from WORNG ALL with this answer. This guy had found very good tricks.

  1. Name the variable only @
  2. Loop with GOTO instead of actual LOOP
  3. Save one character using FORMAT instead of CONVERT. (SSMS2012+ only)

Using these tricks, this solution was trimmed down to the solution below which is 79 bytes.

DECLARE @ DATETIME=42734G:PRINT FORMAT(@,'dd.MM.yyy')SET @+=7IF @<43100GOTO G
\$\endgroup\$
3
  • \$\begingroup\$ You can save 2 bytes by changing @=@+7 to @+=7 and by removing the space between the 7 and the IF. \$\endgroup\$
    – WORNG ALL
    May 9, 2017 at 20:42
  • \$\begingroup\$ In this post is where I got all the tricks, may help you too. \$\endgroup\$
    – WORNG ALL
    May 9, 2017 at 20:43
  • 1
    \$\begingroup\$ @WORNGALL wonderfull, I was unaware we could do that. Thanks a lot. \$\endgroup\$
    – AXMIM
    May 10, 2017 at 14:42
1
\$\begingroup\$

Octave, 37 bytes

A lot shorter than all other non-golfing languages, and it's even tied with Jelly! Way to go Octave! :)

disp(datestr(367:7:737,'DD.mm.2017'))

Try it online!

Luckily, year 2 AD looks exactly the same as year 2017 AD. Both starts and ends at a Sunday, and neither is a leap year. This saves a lot of bytes, since 367:7:737 is quite a bit shorter than 736696:7:737060.

This converts the number of days since 01.01.0001, to a string on the format DD.mm, with a trailing .2017.

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

Haskell, 133 130 bytes

import Data.Time.Calendar
((\[f,g,h,i,j]->i:j:'.':f:g:".2017\n").drop 5.show)=<<take 53(iterate(addDays 7)$fromGregorian 2017 1 1)

Try it online!

Without a calendar library, 148 144 140 bytes

(\l->last l!l++length l!l++"2017\n").fst.span(>0).(<$>scanl((+).(+28))0[3,0,3,2,3,2,3,3,2,3,2]).(-)=<<[1,8..365]
a!_=['0'|a<=9]++show a++"."

This is funny since using an operator for the padding function saves two bytes even though the second argument is unused, because less parentheses are needed – p(last l) is longer than last l!l. Works by calculating day/month pairs by subtracting the cumulative month start dates from the day of the year. The month start dates are compressed as (scanl((+).(+28))0[3,0,3,2,3,2,3,3,2,3,2]). Month number is the number of positive elements and day number is the last positive element.

Try it online!

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

C#, 116 114 113 bytes

for(long i=(long)636188256e9;i<636502857e9;i+=(long)605e10)Out.Write(‌​new DateTime(i).ToString("dd.MM.yyyy\n"));

Can be run in the interactive windows of Visual Studio (or any other C# REPL based on Roslyn)

Down to 113 bytes: thanks to Kevin Cruijssen.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Could you add a TryItNow link? Also, you can golf it a bit by using a for-loop instead of a while-loop: for(long i=(long)636188256e9;i<636502857e9;i+=(long)605e10)Out.Write(new DateTime(i).ToString("dd.MM.yyyy\n")); \$\endgroup\$ Apr 13, 2017 at 11:57
  • \$\begingroup\$ @KevinCruijssen Unfortunaly I am not able to find an applicable compiler on the given page. Here is a link to the used technology if you want to get in touch with it: link. Anyway thanks for the new loop. :) \$\endgroup\$
    – rmrm
    Apr 13, 2017 at 12:33
  • 1
    \$\begingroup\$ Unless otherwise specified, our defaults say you should provide a function or program (i.e. not a snippet), and you need to qualify Debug and DateTime: I'd recommend adding using System; and using Console.WriteLine(string) \$\endgroup\$ Apr 14, 2017 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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