-
Notifications
You must be signed in to change notification settings - Fork 0
/
019.go
66 lines (54 loc) · 1.22 KB
/
019.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import "fmt"
func getDaysSince1900(d int, m int, y int) int {
n := d
if m > 1 {
n += getDaysSince1900(0, m - 1, 0)
n += getDaysInMonth(m - 1, y)
}
if y > 1900 {
n += getDaysSince1900(0, 0, y - 1)
n += getDaysInYear(y - 1)
}
return n
}
func getDaysInMonth(m int, y int) int {
switch m {
case 2:
if isLeapYear(y) {
return 29
}
return 28
case 4: case 6: case 9: case 11:
// 30 days in April, June, September and November
return 30
default:
return 31
}
return 0
}
func getDaysInYear(y int) int {
if isLeapYear(y) {
return 366;
}
return 365;
}
func isLeapYear(y int) bool {
return y % 4 == 0 && y % 400 != 0
}
func isSunday(d int, m int, y int) bool {
// 1900-01-01 was a Monday, which we calculate as 1 day after 1900, so
// any date with the number of days divisible by 7 will be a Sunday
return getDaysSince1900(d, m, y) % 7 == 0
}
func main() {
c := 0
for y := 1901; y <= 2000; y++ {
for m := 1; m <= 12; m++ {
if (isSunday(1, m, y)) {
c++
}
}
}
fmt.Printf("Result: %d\n", c)
}