Project Euler – Problem # 19 – Solved with Go

How many Sundays fell on the first of the month during the twentieth century?

You are given the following information, but you may prefer to do some research for yourself.

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

Problem:

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

**********

To solve this problem, I just ported my python solution to Go.

One Possible Solution: Go

package main

import "fmt"

func main() {
	fmt.Println("Hello, playground")

	L := make(map[int]string)

	L[1] = "Sun"
	L[2] = "Mon"
	L[3] = "Tue"
	L[4] = "Wed"
	L[5] = "Thurs"
	L[6] = "Fri"
	L[7] = "Sat"

	// Start at Monday, 1st, 1900
	counter := 1
	tally := 0

	for yr := 1900; yr < 2001; yr++ {	// Year
		fmt.Printf("Year = %d\n", yr)

		for month := 1; month < 13; month++ {	//  Month
			y := find_days(month, yr)
			for month_days := 1; month_days <= y; month_days++ {	// Number of month days
				// Start at Monday, 1st, 1900
				counter += 1
				if month_days == 1 && L[counter] == "Sun" {
					fmt.Printf("month_days = %d L[counter] = %s\n", month_days, L[counter])
					tally += 1
				}
				if counter >= 7 {	// Days of the week
					counter = 0
				}
			}
		}
		fmt.Printf("\n")
	}
	// subtract 2 for the year 1900
	fmt.Println("Tally = ", tally-2)
}

func find_days(month int, yr int) int {
	d := 0
	if (month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12) {
		d = 31
	} else if (month == 4) || (month == 6) || (month == 9) || (month == 11) {
		d = 30
	} else if month == 2 {
		if yr%4 == 0 {
			if yr%100 == 0 {
				if yr%400 == 0 {
					d = 29
				} else {
					d = 28
				}
			} else {
				d = 29
			}
		} else {
			d = 28
		}
	} else {
		fmt.Println("There has been an error")
	}
	return d
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s