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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

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

Posted 05 October 2012 - 05:28 AM

View PostCurtis Rutland, on 05 October 2012 - 12:19 AM, said:

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

And also the letter, as the preferred language is vb.net, so includes the framework / CLR.

And date validation is a requirement.
Was This Post Helpful? 0
  • +
  • -

#17 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4436
  • View blog
  • Posts: 7,707
  • Joined: 08-June 10

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

Posted 05 October 2012 - 06:20 AM

Technically, the language itself doesn't include the framework or the CLR. The language just includes its grammar. That's why Mono can support "VB.NET", because the language itself can be implemented in any way. Of course, it's obvious what you were driving at with the challenge.
Was This Post Helpful? 0
  • +
  • -

#18 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

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

Posted 05 October 2012 - 08:03 AM

Apparently my submissions have a bug.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

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

Posted 05 October 2012 - 08:23 AM

You can shave off some more characters. You don't need this: M > 1 And to bump up the month of February. And since VB.NET doesn't do short circuiting of Boolean expressions anyway (unless you tell it explicitly to), then you aren't saving any CPU cycles by having that check there.
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

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

Posted 05 October 2012 - 08:36 AM

And also possibly shave off even more by changing 0 < M And M < 13 to just check for 0. VB will throw an exception anyway if you try to access s(M) for other values that are out of range so you still get the effect of throwing an exception for invalid parameters. Ugly? Yes, but the point of code golf is minimal characters. :)
Was This Post Helpful? 0
  • +
  • -

#21 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

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

Posted 05 October 2012 - 09:07 AM

Nope it throw an IndexOfRangeException which is confusing without access to the function's sourcecode.

This post has been edited by AdamSpeight2008: 05 October 2012 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#22 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

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

Posted 05 October 2012 - 11:40 AM

:) You didn't specify what kind of exception needed to be thrown, only that an exception be thrown for an invalid date.

Sorry, one of the sports I play allows for very fine distinctions of what is the letter of the law vs. what is the spirit of the law. One of the adages is that "On match day, 'intent' doesn't count." So small nuances about what is allowed vs. not allowed sometimes comes down to little loopholes like this where a stage designer might have intended for competitors to run through a stage a particular way, but if they find a faster/easier way to do it and still comply with what was written, then kudos to their ability to think outside the box and "game the stage" since the sports also rewards creativity.
Was This Post Helpful? 0
  • +
  • -

#23 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

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

Posted 05 October 2012 - 11:47 AM

This is actually a very nice challenge because of how verbose VB.NET can be. Thanks for posting it AdamSpeight2008!

For example, so far the best way I can think off to do the day range validation is using that array for number of days. Since I've already eaten characters to create the table, I might as well use it to compute the Julian date.

There is that interesting approach somebody had of just having an array of deltas to 30 days per month that can possible save 12 characters, but will it be enough to make up for the extra logic need to take advantage of the deltas?
Was This Post Helpful? 0
  • +
  • -

#24 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

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

Posted 05 October 2012 - 11:50 AM

I agree with Skydiver. Exception is thrown, and that was the requirement. He mentioned it was "ugly" solution. For the same reason I provided solution with just Returns...

Quote

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


Was This Post Helpful? 0
  • +
  • -

#25 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

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

Posted 05 October 2012 - 01:33 PM

As an algorithmic improvement, the preferred method seems to be to declare a 13 value array, but you could declare a 12 value array of the following sort:

{3, 0, 3, 2, 3, 2, 3, 3, 2, 3, 2}

Which would save you 14 characters.
Update running sum with + 28 for each month, bringing your character savings down to 11. Would also work with a multiplication of --M * 28, but the savings would be reduced to 8.

Spoiler

This post has been edited by grimpirate: 07 October 2012 - 07:57 PM

Was This Post Helpful? 0
  • +
  • -

#26 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

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

Posted 05 October 2012 - 01:45 PM

grimpirate: I think that your lines 7-8 will incorrectly compute Feb 1, as day 1 of the year. And Mar 1 as day 29 on a non-leap year.
Was This Post Helpful? 0
  • +
  • -

#27 snypeNET  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 06-October 12

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

Posted 12 October 2012 - 10:08 PM

I hope I am not too late in posting this. I saw this posting and it sounded like fun. I broke the code into a main module and another module that checks if the date entered is a valid date.

The validity module:
Module validity
    Function validInput(ByVal userDate As String()) As Boolean
        Dim allowedInput As Boolean = True
        Dim leapYear As Boolean = False
        Dim convertedValue As Integer
        Try
            convertedValue = Integer.Parse(userDate(2))
        Catch ex As Exception
            Return False
        End Try

        leapYear = isItLeapYear(convertedValue)

        If convertedValue < 1 Then
            allowedInput = False
        End If

        convertedValue = Integer.Parse(userDate(0))
        Dim maxDays As Integer
        If convertedValue = 9 Or convertedValue = 11 Or convertedValue = 4 Or convertedValue = 6 Then
            maxDays = 30
        ElseIf convertedValue <> 2 Then
            maxDays = 31
        ElseIf leapYear Then
            maxDays = 29
        Else
            maxDays = 28
        End If
        If convertedValue > maxDays Or convertedValue < 1 Then
            allowedInput = False
        End If

        If convertedValue < 1 Or convertedValue > 12 Then
            allowedInput = False
        End If

        convertedValue = Integer.Parse(userDate(1))

        Return allowedInput
    End Function

    Function isItLeapYear(ByVal anyYear As Integer) As Boolean
        Dim aLeapYear As Boolean = True
        If anyYear Mod 400 = 0 Then
            aLeapYear = True
        ElseIf anyYear Mod 100 = 0 Then
            aLeapYear = False
        ElseIf anyYear Mod 4 = 0 Then
            aLeapYear = True
        Else
            aLeapYear = False
        End If
        Return aLeapYear
    End Function

End Module




The Main module:

Module dayCounter

    Sub Main()
        Dim repeat As Boolean = True
        While repeat
            Console.WriteLine("Please Enter a date of format MM/DD/YYYY:")
            Dim input = Console.ReadLine()
            Dim dateString As String() = input.Split("/")
            Dim totalDays As Integer = 0

            If validInput(dateString) Then

                Select Case Integer.Parse(dateString(0))
                    Case 1 'January
                        totalDays = Integer.Parse(dateString(1))
                    Case 2 'February
                        totalDays = 31 + Integer.Parse(dateString(1)) 
                    Case 3 'March
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 31 + 29 + Integer.Parse(dateString(1)) 
                        Else
                            totalDays = 31 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 4 
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 62 + 29 + Integer.Parse(dateString(1)) 
                        Else
                            totalDays = 62 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 5 
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 92 + 29 + Integer.Parse(dateString(1)) 
                        Else                                          
                            totalDays = 92 + 28 + Integer.Parse(1)
                        End If
                    Case 6 
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 123 + 29 + Integer.Parse(dateString(1)) 
                        Else
                            totalDays = 123 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 7 
                        If isItLeapYear(Integer.Parse(dateString(2))) Then  
                            totalDays = 153 + 29 + Integer.Parse(dateString(1))
                        Else
                            totalDays = 153 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 8 'August
                        If isItLeapYear(Integer.Parse(dateString(2))) Then       
                            totalDays = 184 + 29 + Integer.Parse(dateString(1)) 
                        Else
                            totalDays = 184 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 9 'September
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 215 + 29 + Integer.Parse(dateString(1))
                        Else                                        
                            totalDays = 215 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 10 'October
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 245 + 29 + Integer.Parse(dateString(1)) 
                        Else                                        
                            totalDays = 245 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 11 'November
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 275 + 29 + Integer.Parse(dateString(1))
                        Else                                        
                            totalDays = 275 + 28 + Integer.Parse(dateString(1))
                        End If
                    Case 12 'December
                        If isItLeapYear(Integer.Parse(dateString(2))) Then
                            totalDays = 305 + 29 + Integer.Parse(dateString(1))
                        Else                                       
                            totalDays = 305 + 28 + Integer.Parse(dateString(1))
                        End If
                End Select
                Console.WriteLine(input & "->" & totalDays)
            Else
                Console.WriteLine("You have entered an invalid date please follow the format MM/DD/YYYY.")
            End If
            Console.WriteLine("Would you like to:")
            Console.WriteLine("  1. Try Again")
            Console.WriteLine("  2. Exit")
            Dim userChoice As String = Console.ReadLine()
            If userChoice.Contains("1") Then
                repeat = True
                Console.Clear()
            Else
                repeat = False
            End If
        End While
    End Sub
End Module




I used the pseudocode algorithm from:
http://en.wikipedia.org/wiki/Leap_year

As far as I can tell it works.
Was This Post Helpful? 0
  • +
  • -

#28 nilkanthnaik5  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 16
  • Joined: 22-October 12

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

Posted 19 November 2012 - 11:56 PM

Attached File  daycount.txt (1.03K)
Number of downloads: 35Module Module1

    Sub Main()
        Console.WriteLine("Enter day")
        Dim day As Integer = Console.ReadLine()
        Console.WriteLine("Enter month")
        Dim month As Integer = Console.ReadLine()
        Console.WriteLine("Enter year")
        Dim year As Integer = Console.ReadLine()
        Console.WriteLine("Total days till  " & day & "-" & month & "-" & year & " is " & DaysInYear(day, month, year))
        Console.ReadLine()
    End Sub
    Function DaysInYear(D As Integer,M As Integer,Y As Integer) As Integer
        Try
            Dim MonthsDay As Integer() = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
            If (M > 2 And (Y Mod 4 = 0) And (Y Mod 400) > 0) Then
                MonthsDay(2) = 29
            End If
            If (0 < M And M < 13) And (0 < D And D <= MonthsDay(M)) Then Return MonthsDay.Take(M).Sum + D
            Throw New ArgumentException("Invalid Date")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Function
End Module

This post has been edited by AdamSpeight2008: 20 November 2012 - 01:48 AM
Reason for edit:: Adding in CODE tags

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2