Skip to content

Commit

Permalink
Add meetup exercise (#358)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Mar 14, 2024
1 parent e7e522b commit 4720043
Show file tree
Hide file tree
Showing 10 changed files with 848 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
swi-prolog-branch: stable
swi-prolog-version: latest

- name: Install packages
run: swipl pack install date_time -y

- name: Verify all exercises
run: bin/verify-exercises.sh

Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "meetup",
"name": "Meetup",
"uuid": "2590ef6c-3643-4d20-8f81-224d430ad3eb",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
Expand Down
9 changes: 9 additions & 0 deletions exercises/practice/meetup/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Hints

## General

- Use the [date_time package][date_time_package] to make working with dates a lot easier.
- Check the [source of the date_time package][https://www.swi-prolog.org/pack/file_details/date_time/prolog/date_time.pl?show=src] for detailed information on how to use the package.

[date_time_package]: https://www.swi-prolog.org/pack/list?p=date_time
[date_time-package-source]: https://www.swi-prolog.org/pack/file_details/date_time/prolog/date_time.pl?show=src
40 changes: 40 additions & 0 deletions exercises/practice/meetup/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Instructions append

## Working with Dates in Prolog

Prolog's built-in support for dates is [fairly rudimentary][built-in-dates-support].
A much nicer way to work with dates is via the [date_time package][date_time-package].

### Installing the date_time package

If your installed Swipl version is fairly up-to-date, you can install the package by running:

```bash
swipl pack install date_time
```

If that doesn't work, you can run:

```shell
swipl -g 'pack_install(date_time)'
```

~~~~exercism/note
If you're using the online editor, you don't need to manually install anything.
~~~~

### Using the date_time package

Ad the following code to the top of your solution file to use the `date_time` package:

```prolog
:- use_module(library(date_time)).
```

~~~~exercism/note
Tip: the [source of the date_time package][https://www.swi-prolog.org/pack/file_details/date_time/prolog/date_time.pl?show=src] is well documented and be a great help!
~~~~

[built-in-dates-support]: https://www.swi-prolog.org/pldoc/man?section=timedate
[date_time_package]: https://www.swi-prolog.org/pack/list?p=date_time
[date_time-package-source]: https://www.swi-prolog.org/pack/file_details/date_time/prolog/date_time.pl?show=src
51 changes: 51 additions & 0 deletions exercises/practice/meetup/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Instructions

Recurring monthly meetups are generally scheduled on the given weekday of a given week each month.
In this exercise you will be given the recurring schedule, along with a month and year, and then asked to find the exact date of the meetup.

For example a meetup might be scheduled on the _first Monday_ of every month.
You might then be asked to find the date that this meetup will happen in January 2018.
In other words, you need to determine the date of the first Monday of January 2018.

Similarly, you might be asked to find:

- the third Tuesday of August 2019 (August 20, 2019)
- the teenth Wednesday of May 2020 (May 13, 2020)
- the fourth Sunday of July 2021 (July 25, 2021)
- the last Thursday of November 2022 (November 24, 2022)

The descriptors you are expected to process are: `first`, `second`, `third`, `fourth`, `last`, `teenth`.

Note that descriptor `teenth` is a made-up word.

It refers to the seven numbers that end in '-teen' in English: 13, 14, 15, 16, 17, 18, and 19.
But general descriptions of dates use ordinal numbers, e.g. the _first_ Monday, the _third_ Tuesday.

For the numbers ending in '-teen', that becomes:

- 13th (thirteenth)
- 14th (fourteenth)
- 15th (fifteenth)
- 16th (sixteenth)
- 17th (seventeenth)
- 18th (eighteenth)
- 19th (nineteenth)

So there are seven numbers ending in '-teen'.
And there are also seven weekdays (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday).
Therefore, it is guaranteed that each day of the week (Monday, Tuesday, ...) will have exactly one numbered day ending with "teen" each month.

If asked to find the teenth Saturday of August, 1953 (or, alternately the "Saturteenth" of August, 1953), we need to look at the calendar for August 1953:

```plaintext
August 1953
Su Mo Tu We Th Fr Sa
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
```

The Saturday that has a number ending in '-teen' is August 15, 1953.
18 changes: 18 additions & 0 deletions exercises/practice/meetup/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"meetup.pl"
],
"test": [
"meetup_tests.plt"
],
"example": [
".meta/meetup.example.pl"
]
},
"blurb": "Calculate the date of meetups.",
"source": "Jeremy Hinegardner mentioned a Boulder meetup that happens on the Wednesteenth of every month"
}
34 changes: 34 additions & 0 deletions exercises/practice/meetup/.meta/meetup.example.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
:- use_module(library(date_time)).
:- use_module(library(clpfd)).

days_helper(Month, Date, WeekDay, Dates) :-
date_extract(Date, months(Month)),
date_add(Date, 1 days, NextDay),
days_helper(Month, NextDay, WeekDay, NextDates),
(week_day(Date, WeekDay) -> Dates = [Date|NextDates]; Dates = NextDates), !.

days_helper(_, _, _, []).

days(Year, Month, WeekDay, Dates) :-
date_create(Year, Month, 1, Date),
days_helper(Month, Date, WeekDay, Dates).

nth1_day(Year, Month, WeekDay, Index, Date) :-
days(Year, Month, WeekDay, Dates),
nth1(Index, Dates, Date).

meetup(Year, Month, first, WeekDay, Date) :- nth1_day(Year, Month, WeekDay, 1, Date), !.
meetup(Year, Month, second, WeekDay, Date) :- nth1_day(Year, Month, WeekDay, 2, Date), !.
meetup(Year, Month, third, WeekDay, Date) :- nth1_day(Year, Month, WeekDay, 3, Date), !.
meetup(Year, Month, fourth, WeekDay, Date) :- nth1_day(Year, Month, WeekDay, 4, Date), !.

meetup(Year, Month, last, WeekDay, Date) :-
days(Year, Month, WeekDay, Dates),
last(Dates, Date), !.

meetup(Year, Month, teenth, WeekDay, Date) :-
days(Year, Month, WeekDay, Dates),
member(Date, Dates),
date_extract(Date, days(Day)),
Day #>= 13,
!.
Loading

0 comments on commit 4720043

Please sign in to comment.