Skip to content

Commit 3342d6a

Browse files
Add meetup (#482)
1 parent 2c16285 commit 3342d6a

File tree

8 files changed

+1121
-0
lines changed

8 files changed

+1121
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,14 @@
953953
"prerequisites": [],
954954
"difficulty": 5
955955
},
956+
{
957+
"slug": "meetup",
958+
"name": "Meetup",
959+
"uuid": "a625f21c-b468-478a-b23b-1b3fd02fa2a7",
960+
"practices": [],
961+
"prerequisites": [],
962+
"difficulty": 5
963+
},
956964
{
957965
"slug": "pythagorean-triplet",
958966
"name": "Pythagorean Triplet",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Instructions
2+
3+
Your task is to find the exact date of a meetup, given a month, year, weekday and week.
4+
5+
There are six week values to consider: `first`, `second`, `third`, `fourth`, `last`, `teenth`.
6+
7+
For example, you might be asked to find the date for the meetup on the first Monday in January 2018 (January 1, 2018).
8+
9+
Similarly, you might be asked to find:
10+
11+
- the third Tuesday of August 2019 (August 20, 2019)
12+
- the teenth Wednesday of May 2020 (May 13, 2020)
13+
- the fourth Sunday of July 2021 (July 25, 2021)
14+
- the last Thursday of November 2022 (November 24, 2022)
15+
- the teenth Saturday of August 1953 (August 15, 1953)
16+
17+
## Teenth
18+
19+
The teenth week refers to the seven days in a month that end in '-teenth' (13th, 14th, 15th, 16th, 17th, 18th and 19th).
20+
21+
If asked to find the teenth Saturday of August, 1953, we check its calendar:
22+
23+
```plaintext
24+
August 1953
25+
Su Mo Tu We Th Fr Sa
26+
1
27+
2 3 4 5 6 7 8
28+
9 10 11 12 13 14 15
29+
16 17 18 19 20 21 22
30+
23 24 25 26 27 28 29
31+
30 31
32+
```
33+
34+
From this we find that the teenth Saturday is August 15, 1953.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Introduction
2+
3+
Every month, your partner meets up with their best friend.
4+
Both of them have very busy schedules, making it challenging to find a suitable date!
5+
Given your own busy schedule, your partner always double-checks potential meetup dates with you:
6+
7+
- "Can I meet up on the first Friday of next month?"
8+
- "What about the third Wednesday?"
9+
- "Maybe the last Sunday?"
10+
11+
In this month's call, your partner asked you this question:
12+
13+
- "I'd like to meet up on the teenth Thursday; is that okay?"
14+
15+
Confused, you ask what a "teenth" day is.
16+
Your partner explains that a teenth day, a concept they made up, refers to the days in a month that end in '-teenth':
17+
18+
- 13th (thirteenth)
19+
- 14th (fourteenth)
20+
- 15th (fifteenth)
21+
- 16th (sixteenth)
22+
- 17th (seventeenth)
23+
- 18th (eighteenth)
24+
- 19th (nineteenth)
25+
26+
As there are also seven weekdays, it is guaranteed that each day of the week has _exactly one_ teenth day each month.
27+
28+
Now that you understand the concept of a teenth day, you check your calendar.
29+
You don't have anything planned on the teenth Thursday, so you happily confirm the date with your partner.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"meetup.zig"
8+
],
9+
"test": [
10+
"test_meetup.zig"
11+
],
12+
"example": [
13+
".meta/example.zig"
14+
]
15+
},
16+
"blurb": "Calculate the date of meetups.",
17+
"source": "Jeremy Hinegardner mentioned a Boulder meetup that happens on the Wednesteenth of every month"
18+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
const std = @import("std");
2+
3+
pub const Month = enum {
4+
january,
5+
february,
6+
march,
7+
april,
8+
may,
9+
june,
10+
july,
11+
august,
12+
september,
13+
october,
14+
november,
15+
december,
16+
};
17+
18+
pub const Week = enum {
19+
first,
20+
second,
21+
third,
22+
fourth,
23+
teenth,
24+
last,
25+
};
26+
27+
pub const DayOfWeek = enum {
28+
monday,
29+
tuesday,
30+
wednesday,
31+
thursday,
32+
friday,
33+
saturday,
34+
sunday,
35+
};
36+
37+
fn isLeapYear(year: u12) bool {
38+
return (year % 4 == 0) and ((year % 100 != 0) or (year % 400 == 0));
39+
}
40+
41+
fn daysInMonth(year: u12, month: Month) u6 {
42+
return switch (month) {
43+
.january => 31,
44+
.february => if (isLeapYear(year)) 29 else 28,
45+
.march => 31,
46+
.april => 30,
47+
.may => 31,
48+
.june => 30,
49+
.july => 31,
50+
.august => 31,
51+
.september => 30,
52+
.october => 31,
53+
.november => 30,
54+
.december => 31,
55+
};
56+
}
57+
58+
fn weekConcludes(year: u12, month: Month, week: Week) u6 {
59+
return switch (week) {
60+
.first => 7,
61+
.second => 14,
62+
.third => 21,
63+
.fourth => 28,
64+
.teenth => 19,
65+
.last => daysInMonth(year, month),
66+
};
67+
}
68+
69+
fn monthOffset(month: Month) u12 {
70+
return switch (month) {
71+
.january => 307, // offset from the end of February of previous year
72+
.february => 338,
73+
.march => 1,
74+
.april => 32,
75+
.may => 62,
76+
.june => 93,
77+
.july => 123,
78+
.august => 154,
79+
.september => 185,
80+
.october => 215,
81+
.november => 246,
82+
.december => 276,
83+
};
84+
}
85+
86+
fn concludingDay(year: u12, month: Month, day_of_month: u6) u12 {
87+
const y = if (month == .january or month == .february) (year - 1) else year;
88+
return (y + (y / 4) - (y / 100) + (y / 400) + monthOffset(month) + day_of_month) % 7;
89+
}
90+
91+
pub fn meetupDayOfMonth(year: u12, month: Month, week: Week, day_of_week: DayOfWeek) u12 {
92+
const day = weekConcludes(year, month, week);
93+
const concluding = concludingDay(year, month, day);
94+
const required = @intFromEnum(day_of_week);
95+
const adjustment: u12 = if (concluding < required) 7 else 0;
96+
return day + required - (concluding + adjustment);
97+
}
98+
99+
pub fn meetup(year: u12, month: Month, week: Week, day_of_week: DayOfWeek) [10]u8 {
100+
const day_of_month = meetupDayOfMonth(year, month, week, day_of_week);
101+
102+
var buffer: [10]u8 = undefined;
103+
_ = std.fmt.bufPrint(&buffer, "{d:0>4}-{d:0>2}-{d:0>2}", .{ year, 1 + @intFromEnum(month), day_of_month }) catch unreachable;
104+
return buffer;
105+
}

0 commit comments

Comments
 (0)