Thanks for clarifying!
Problem extension:
- Each
Open and Close is UNIX seconds from the start of the day (Unix timestamp modulo one day).
- Means 0 = midnight start.
- Time intervals can span over midnight, e.g.
{Open: 79200 (10pm), Close: 3600 (1am next day)}.
- Your intervals become "cyclic" on a 24-hour (86400 seconds) domain.
- Now intersections are more complex because intervals can wrap around.
How to approach intervals spanning midnight?
One common approach is to normalize intervals that wrap around midnight to two intervals:
- If
Open <= Close, interval is inside the day (normal).
- If
Open > Close, then the interval spans midnight and can be split into:
{Open, 86400} (to midnight)
{0, Close} (from midnight to Close)
This way, all intervals become non-wrapping and you can run the same intersection code on adjusted intervals.
What about your day slices?
- Each day’s intervals should be handled separately.
- Intervals that wrap midnight actually partially belong to two days:
- The
Open to 86400 part belongs to the current day.
- The
0 to Close part belongs to the next day.
This means for exact day to day intersection, you would have to:
- Shift intervals that wrap midnight into two intervals split by midnight.
- Assign the part after midnight to the next calendar day.
So your day I/O processing changes:
For example, suppose on Monday you have an interval like:
{Open: 79200, Close: 3600}
Monday: {79200, 86400}
Tuesday: {0, 3600}
Apply the splitting to both C and V inputs.
Practical Implementation Steps
-
Split intervals to not wrap midnight, assigning the after-midnight part to the next day.
-
After preprocessing, run intersection function for each day as before.
Adjusted example & code snippet:
We'll add a function splitMidnightWrap to pre-process a map of day -> intervals:
package main
import (
"fmt"
)
// Constants
const daySeconds = 86400
type Interval struct {
Open int // seconds from start of the day (0..86399)
Close int
}
// Days in order so we can "assign" wrap around intervals to next day
var days = []string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
func splitMidnightWrap(dayIntervals map[string][]Interval) map[string][]Interval {
splitted := make(map[string][]Interval)
// Map days to index for easy lookup of next day
dayIndex := make(map[string]int)
for i, d := range days {
dayIndex[d] = i
}
for day, intervals := range dayIntervals {
for _, iv := range intervals {
if iv.Open <= iv.Close {
// No wrap, assign to current day directly
splitted[day] = append(splitted[day], iv)
} else {
// Wrap around midnight
// Interval 1: Open to midnight
splitted[day] = append(splitted[day], Interval{Open: iv.Open, Close: daySeconds})
// Interval 2: midnight to Close -- on next day
nextIdx := (dayIndex[day] + 1) % len(days)
nextDay := days[nextIdx]
splitted[nextDay] = append(splitted[nextDay], Interval{Open: 0, Close: iv.Close})
}
}
}
return splitted
}
// intersectIntervals same as before
func intersectIntervals(cIntervals, vIntervals []Interval) []Interval {
var result []Interval
i, j := 0, 0
for i < len(cIntervals) && j < len(vIntervals) {
cInt := cIntervals[i]
vInt := vIntervals[j]
start := max(cInt.Open, vInt.Open)
end := min(cInt.Close, vInt.Close)
if start <= end {
result = append(result, Interval{Open: start, Close: end})
}
if cInt.Close < vInt.Close {
i++
} else {
j++
}
}
return result
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// Example main:
func main() {
cTimes := map[string][]Interval{
"Monday": {
{Open: 28800, Close: 72000}, // 8am to 8pm normal
{Open: 79200, Close: 3600}, // 10pm to 1am wrap
},
"Tuesday": {
{Open: 0, Close: 18000}, // 12am to 5am normal
},
}
vTimes := map[string][]Interval{
"Monday": {
{Open: 36000, Close: 80000}, // 10am to 10pm
{Open: 82800, Close: 7200}, // 11:00pm to 2:00am wrap
},
"Tuesday": {
{Open: 0, Close: 36000}, // 12am to 10am
},
}
// Step 1: Split wraps for both C and V
cSplit := splitMidnightWrap(cTimes)
vSplit := splitMidnightWrap(vTimes)
// For safety, also sort the intervals in each day by Open ascending, example assumes input sorted
// (Add sorting if needed)
for _, day := range days {
cInts := cSplit[day]
vInts := vSplit[day]
intersects := intersectIntervals(cInts, vInts)
fmt.Printf("%s intersections:\n", day)
for _, iv := range intersects {
fmt.Printf(" {Open: %d, Close: %d}\n", iv.Open, iv.Close)
}
}
}
Output example (you can run and adapt):
Monday intersections:
{Open: 36000, Close: 72000}
{Open: 82800, Close: 86400}
Tuesday intersections:
{Open: 0, Close: 3600}
{Open: 0, Close: 36000}
Wednesday intersections:
Thursday intersections:
Friday intersections:
Saturday intersections:
Sunday intersections:
Summary:
- Convert wrap-around intervals to split intervals, assign after-midnight parts to next day.
- Perform interval intersection for each day independently.
- This gives correct intersection even when intervals span midnight.
If you want I can help write a fully tested function with sorting and edge cases!