Skip to content

Commit

Permalink
Add meetup exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Dec 25, 2024
1 parent de1c181 commit ece06b7
Show file tree
Hide file tree
Showing 13 changed files with 4,874 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,14 @@
"prerequisites": [],
"difficulty": 7
},
{
"slug": "meetup",
"name": "Meetup",
"uuid": "dd0716b7-09d4-4fb5-95c4-af79983f41f1",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "pythagorean-triplet",
"name": "Pythagorean Triplet",
Expand Down
34 changes: 34 additions & 0 deletions exercises/practice/meetup/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Instructions

Your task is to find the exact date of a meetup, given a month, year, weekday and week.

There are five week values to consider: `first`, `second`, `third`, `fourth`, `last`, `teenth`.

For example, you might be asked to find the date for the meetup on the first Monday in January 2018 (January 1, 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 teenth Saturday of August 1953 (August 15, 1953)

## Teenth

The teenth week refers to the seven days in a month that end in '-teenth' (13th, 14th, 15th, 16th, 17th, 18th and 19th).

If asked to find the teenth Saturday of August, 1953, we check its calendar:

```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
```

From this we find that the teenth Saturday is August 15, 1953.
29 changes: 29 additions & 0 deletions exercises/practice/meetup/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Introduction

Every month, your partner meets up with their best friend.
Both of them have very busy schedules, making it challenging to find a suitable date!
Given your own busy schedule, your partner always double-checks potential meetup dates with you:

- "Can I meet up on the first Friday of next month?"
- "What about the third Wednesday?"
- "Maybe the last Sunday?"

In this month's call, your partner asked you this question:

- "I'd like to meet up on the teenth Thursday; is that okay?"

Confused, you ask what a "teenth" day is.
Your partner explains that a teenth day, a concept they made up, refers to the days in a month that end in '-teenth':

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

As there are also seven weekdays, it is guaranteed that each day of the week has _exactly one_ teenth day each month.

Now that you understand the concept of a teenth day, you check your calendar.
You don't have anything planned on the teenth Thursday, so you happily confirm the date with your partner.
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": [
"keiravillekode"
],
"files": {
"solution": [
"meetup.s"
],
"test": [
"meetup_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Calculate the date of meetups.",
"source": "Jeremy Hinegardner mentioned a Boulder meetup that happens on the Wednesteenth of every month"
}
160 changes: 160 additions & 0 deletions exercises/practice/meetup/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
.equ FIRST, 1
.equ SECOND, 2
.equ THIRD, 3
.equ FOURTH, 4
.equ TEENTH, 5
.equ LAST, 6

.equ MONDAY, 1
.equ TUESDAY, 2
.equ WEDNESDAY, 3
.equ THURSDAY, 4
.equ FRIDAY, 5
.equ SATURDAY, 6
.equ SUNDAY, 7

.data
offset_array:
.hword -1
.hword -1
.hword -1
.hword 2 /* March */
.hword 33
.hword 63
.hword 94
.hword 124
.hword 155
.hword 186
.hword 216
.hword 247
.hword 277
.hword 308
.hword 339 /* February */

week_array:
.byte -1
.byte 7 /* first */
.byte 14
.byte 21
.byte 28
.byte 19 /* teenth */

.text
.globl meetup

/* extern void meetup(char *buffer, int year, int month, week_t week, dayofweek_t dayofweek); */
meetup:
adrp x5, offset_array
add x5, x5, :lo12:offset_array

mov w7, w1 /* adjusted year */
mov w8, w2 /* adjusted month */
cmp w2, #2
bgt .find_offset

sub w7, w7, #1
add w8, w8, #12 /* Consider January and February to be the 13th and 14th months of previous year */

.find_offset:
lsl w8, w8, #1
ldrh w9, [x5, x8] /* offset */
cmp w3, LAST
beq .last

adrp x6, week_array
add x6, x6, :lo12:week_array
ldrb w3, [x6, x3] /* day in month at end of requested week */

.total:
mov w5, w7 /* adjusted year */

lsr w7, w7, #2 /* adjusted year / 4 */
add w5, w5, w7

mov w10, #25
udiv w7, w7, w10 /* adjusted year / 100 */
sub w5, w5, w7

lsr w7, w7, #2 /* adjusted year / 400 */
add w5, w5, w7

add w5, w5, w9 /* add offset */
add w5, w5, w3 /* add day in month at end of requested week */
mov w10, #7
udiv w12, w5, w10 /* quotient */
msub w13, w12, w10, w5 /* remainder */

add w6, w3, w4
sub w6, w6, w13
cmp w6, w3
ble .write

sub w6, w6, #7

.write:
mov w10, #'-'
strb w10, [x0, #4]
strb w10, [x0, #7]
strb wzr, [x0, #10]

mov w10, 10 /* divisor */

mov w11, w1 /* year */
udiv w12, w11, w10 /* quotient */
msub w13, w12, w10, w11 /* remainder */
mov w11, w12
add w13, w13, #'0'
strb w13, [x0, #3]

udiv w12, w11, w10 /* quotient */
msub w13, w12, w10, w11 /* remainder */
mov w11, w12
add w13, w13, #'0'
strb w13, [x0, #2]

udiv w12, w11, w10 /* quotient */
msub w13, w12, w10, w11 /* remainder */
add w13, w13, #'0'
strb w13, [x0, #1]

add w12, w12, #'0'
strb w12, [x0]

mov w11, w2 /* month */
udiv w12, w11, w10 /* quotient */
msub w13, w12, w10, w11 /* remainder */
add w13, w13, #'0'
strb w13, [x0, #6]

add w12, w12, #'0'
strb w12, [x0, #5]

mov w11, w6 /* day in month */
udiv w12, w11, w10 /* quotient */
msub w13, w12, w10, w11 /* remainder */
add w13, w13, #'0'
strb w13, [x0, #9]

add w12, w12, #'0'
strb w12, [x0, #8]
ret

.last:
cmp w2, #2
beq .february

add w8, w8, #2
ldrh w10, [x5, x8] /* offset */
sub w3, w10, w9
b .total

.february:
mov w10, #100 /* divisor */
udiv w12, w1, w10 /* quotient, i.e. century */
msub w13, w12, w10, w1 /* remainder, i.e. year within century */
tst w13, w13 /* check if 0 */
csel w3, w12, w13, eq /* either century, or year within century */
tst w3, #3 /* check if multiple of 4 */
cset w3, eq
add w3, w3, #28
b .total
Loading

0 comments on commit ece06b7

Please sign in to comment.