This is not an elixir-specific question but this is the best community I know so I thought I’d ask anyway. I am creating an algorithmic scheduling tool for my colleagues. In essence there are 3 team members who will have to be assigned a weekend assignment every 3 weeks. I have set up the order in which each team member will take that role by assigning each of them a cycle “offset” – if offset is 0, they get the first week, if 1, second week, if 2 third week. The start date for this schedule is fixed as a constant which is a Sunday. I want to calculate the assignment for all future weekends based on the start date, the cycle length in days, the “offset”, and the weekend date.
What I’ve done is the following (in pseudocode):
days = current_date - start_date
days_in_current_cycle = mod(days, cycle_length)
week_in_current_cycle = div(days_in_current_cycle, 6)
offset = mod(week_in_current_cycle, 3)
assigned = filter(team_members, fn member -> member_offset(member) == offset end)
I’m using 6 as the divisor in finding which week in the current cycle because the start date is a Sunday and the next Saturday belongs to the next week. I also have a conditional to verify the current_date
is either a Saturday or Sunday before going through the above. Does this seem sound? Is there a better way?
I don’t think dividing by 6 is correct; starting on Sunday makes the whole calculation a little trickier.
Here’s a chart of offset
, calendar-style:
Su |
Mo |
Tu |
We |
Th |
Fr |
Sa |
0 |
0 |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
2 |
2 |
2 |
2 |
2 |
2 |
2 |
3 |
How far in the future do you need this? If not too far I’d be lazy and do something like this. It’s not O(something small), but it doesn’t take parsing some math to understand it.
team = [:mike, :brian, :jim]
x = ~D[2024-01-01]
y = ~D[2024-06-01]
start_date = Date.beginning_of_week(x, :sunday)
Stream.iterate(start_date, &Date.add(&1, 7))
|> Stream.zip(Stream.cycle(team))
|> Stream.drop_while(fn {start_of_week, _} ->
end_of_week = Date.end_of_week(start_of_week, :sunday)
Date.compare(y, end_of_week) == :gt
end)
|> Enum.at(0)
1 Like
The reason I thought using 6 as a divisor was acceptable is that the step before ensures that the dividend is < 21. Because it’s integer division the result can only be 0-3. The error this introduces would maybe lead to Fri or Thu being assigned the wrong offset, but I’m not accepting any dates that are not Sat or Sun.
I guess it would be more logically sound to insist the start date be a Saturday and then use 7. I think that would then eliminate the need to calculate the offset as mod 3 because integer division of a value below 21 by 7 must return 0, 1, or 2.
Very cool solution. Unfortunately this is actually something I have to do in Excel rather than with an Elixir script. It will be used to generate at least quarterly schedules, if not the full year.
As an aside, volunteering for this project made me learn just how Turing complete Excel is. It’s got lambdas and variable assignments within formulas. It even has map and filter for dealing with arrays as input.