# Mini (Code Golf) Challenge: How Many Days Into Year?

• (2 Pages)
• 1
• 2

## 27 Replies - 21746 Views - Last Post: 19 November 2012 - 11:56 PM

• MrCupOfT

Reputation: 2290
• Posts: 9,529
• Joined: 29-May 08

# Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 10:14 AM

Mini Challenge: How Many Days Into Year

Challenge:

Create a function the returns how many days into year a user specified date.
Throw exception for an invalid date.

```Function DaysInToYear(D As Integer,
M As Integer,
Y As Integer) As Integer
Return  ' To be filled in '
End Function

```

Example Test Cases
```01/01/2012 => 1
31/12/2012 => 366
30/12/2011 => 364

```

Restrictions
Use of the programming language's built-in date methods and functions, is prohibited.

Score
Least number of non whitespace characters, excluding the following allowances.

Allowances
The function definition signature and structure and the required return keyword.
What is provided above excluding the comments.

This post has been edited by AdamSpeight2008: 04 October 2012 - 10:17 AM

Is This A Good Question/Topic? 3

## Replies To: Mini (Code Golf) Challenge: How Many Days Into Year?

### #2 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4803
• Posts: 8,542
• Joined: 08-June 10

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 10:55 AM

Quick question: do we have to determine if the year is a leap year without using System.DateTime.IsLeapYear?

• MrCupOfT

Reputation: 2290
• Posts: 9,529
• Joined: 29-May 08

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:04 AM

Is that one of the built-in date functions?

### #4 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4803
• Posts: 8,542
• Joined: 08-June 10

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:17 AM

Just double checking.

### #5 Skydiver

• Code herder

Reputation: 4177
• Posts: 13,320
• Joined: 05-May 12

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:24 AM

Here's my entry at a hefty 127 characters.

It also doesn't throw an exception for D being out of range, M being out of range, or negative years. It also doesn't check for special years when the number of days in a month were changed because some Roman emperor or another was feeling feisty, or when the shift from Julian to Gregorian calendar happened for a particular country or the world for that matter.

Spoiler

(Forgive me if I've murdered the VB.NET language since I usually just read VB.NET, not write in it.)

• MrCupOfT

Reputation: 2290
• Posts: 9,529
• Joined: 29-May 08

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:35 AM

My effort which I'll count later.
Spoiler

This post has been edited by AdamSpeight2008: 04 October 2012 - 12:09 PM
Reason for edit:: Missing New Keyword. So now 186

### #7 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4803
• Posts: 8,542
• Joined: 08-June 10

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 11:49 AM

My attempt in C#, like Skydiver's it doesn't check for invalids. I'll add that later. (118 chars):

Spoiler

Edit 1: By my count Adam, yours is 183. And damn, we had similar solutions again.

Edit 2: I realized mine is actually incorrect. Stand by.

Fixed, in at 109 chars:

Spoiler

Last edit, changed my methodology slightly to get 102. The code looks more like yours, but I had always intended to try both ways to see which used more characters, a delta or a month list.

Spoiler

This post has been edited by Curtis Rutland: 04 October 2012 - 02:53 PM

### #8 grimpirate

• Pirate King

Reputation: 149
• Posts: 717
• Joined: 03-August 06

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 12:41 PM

Well, I didn't write this in VB.NET (as I haven't used it in a really long while), but I liked the challenge and I wanted to see what it would look like so I wrote it up in Java.

Spoiler

I have no idea what the spoiler tag is, but since it's in Java I figured it wouldn't be a problem.

This post has been edited by AdamSpeight2008: 04 October 2012 - 01:37 PM
Reason for edit:: Adding in the spoiler tags

### #9 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 01:01 PM

My entry. Didn't count number of characters, and check for input date validity.
Spoiler

Edit: Hmmmm... I understood it should have just Return.....
Edit2: Missed the date validity check requirement. Will fix that. Fixed >=28 bug.

This post has been edited by lucky3: 04 October 2012 - 01:32 PM

• MrCupOfT

Reputation: 2290
• Posts: 9,529
• Joined: 29-May 08

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 01:50 PM

I counted my original at 256 - (78 of allowances) => 178
Spoiler

### #11 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 03:20 PM

Added date validation. It checks for leap year first, so it doesn't throw exception for 29th of February on leap year.
Spoiler

This post has been edited by lucky3: 04 October 2012 - 03:31 PM

### #12 Skydiver

• Code herder

Reputation: 4177
• Posts: 13,320
• Joined: 05-May 12

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 04:18 PM

I've got it down to 46 characters including throwing exceptions for invalid dates:
Spoiler

This post has been edited by Skydiver: 04 October 2012 - 04:18 PM

### #13 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 4803
• Posts: 8,542
• Joined: 08-June 10

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 04:19 PM

Clever, but obviously violating the spirit of the challenge, if not the letter.

### #14 Skydiver

• Code herder

Reputation: 4177
• Posts: 13,320
• Joined: 05-May 12

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 04 October 2012 - 06:15 PM

80 Characters with no parameter checks and sticking to the spirit and letter of the restrictions:
Spoiler

### #15 lucky3

• Friend lucky3 As IHelpable

Reputation: 233
• Posts: 770
• Joined: 19-October 11

## Re: Mini (Code Golf) Challenge: How Many Days Into Year?

Posted 05 October 2012 - 02:34 AM

If I may point out: the code should check if 29/2/2010 (non leap year) is valid first, and then do the magic.