-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsundays.ml
108 lines (103 loc) · 2.43 KB
/
sundays.ml
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(* Any similarities to a Project Euler exercise are purely coincidental. ;)
* (Though for the record, I know that this is a terrible implementation. Just use Integers.)
*)
module Date = struct
type weekday = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday
type month = January | February | March | April | May | June | July | August | September | October | November | December
type date = {
weekday : weekday;
day : int;
month : month;
year : int;
}
let make_date weekday day month year = {
weekday = weekday;
day = day;
month = month;
year = year;
}
let next_day date = (
let week w = (
match w with
| Monday -> Tuesday
| Tuesday -> Wednesday
| Wednesday -> Thursday
| Thursday -> Friday
| Friday -> Saturday
| Saturday -> Sunday
| Sunday -> Monday
) in
let day d = (
let max_day = (
match date.month with
| September
| April
| June
| November -> (
30
)
| February -> (
if (date.year mod 4 = 0 && date.year mod 100 <> 0)
|| (date.year mod 100 = 0 && date.year mod 400 = 0) then
29
else
28
)
| _ -> (
31
)
) in
if (d < max_day) then
d + 1
else
1
) in
let month m = (
if day date.day = 1 then (
match m with
| January -> February
| February -> March
| March -> April
| April -> May
| May -> June
| June -> July
| July -> August
| August -> September
| September -> October
| October -> November
| November -> December
| December -> January
) else (
m
)
) in
let year y = (
if (day date.day) = 1 && (month date.month = January) then
y + 1
else
y
) in {
weekday = (week date.weekday);
day = (day date.day);
month = (month date.month);
year = (year date.year);
}
)
end
open Date
let _ = (
let rec iter date counter = (
if date.year < 2001 then (
let next = next_day date in
if next.year >= 1901 && next.weekday = Sunday && next.day = 1 then (
iter next (counter + 1)
) else (
iter next (counter)
)
) else (
counter
)
) in
iter (make_date Monday 1 January 1900) 0
|> Printf.printf "%d\n"
)