-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounting_sundays.rs
45 lines (42 loc) · 1.08 KB
/
counting_sundays.rs
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
/// Check whether the given year is a leap year.
///
/// * `year`
fn is_leap(year: i64) -> bool {
year % 4 == 0 && year % 100 != 0 || year % 400 == 0
}
/// Determine the number of days in the given month of the given year.
///
/// * `year`
/// * `month` Number from 1 to 12.
///
/// Returns 0 if the month is invalid. Returns the number of days otherwise.
fn days_in(year: i64, month: i64) -> i64 {
match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
4 | 6 | 9 | 11 => 30,
2 => {
if is_leap(year) {
29
} else {
28
}
}
_ => 0,
}
}
pub fn solve() -> i64 {
// If 1 January 1900 was a Monday, then 1 January 1901 was a Tuesday.
// Represent each day as an offset from Sunday. (e.g. Tuesday is 2.)
let mut day = 2;
let mut sundays = 0;
for year in 1901..=2000 {
for month in 1..=12 {
if day == 0 {
sundays += 1;
}
day = (day + days_in(year, month)) % 7;
}
}
assert_eq!(sundays, 171);
sundays
}