Camel
Peter
Peter Campbell Smith

Bits of conflicts

Weekly challenge 367 — 30 March 2026

Week 367: 30 Mar 2026

Task 2

Task — Conflict events

You are given two events' start and end times. Write a script to find out if there is a conflict between the two events. A conflict happens when two events have a non-empty intersection.

Examples


Example 1
Input: @event1 = ('10:00', '12:00')
       @event2 = ('11:00', '13:00')
Output: true
Both events overlap from '11:00' to '12:00'.

Example 2
Input: @event1 = ('09:00', '10:30')
       @event2 = ('10:30', '12:00')
Output: false
Event1 ends exactly at 10:30 when Event2 starts.
Since the problem defines intersection as non-empty,
   exact boundaries touching is not a conflict.

Example 3
Input: @event1 = ('14:00', '15:30')
       @event2 = ('14:30', '16:00')
Output: true
Both events overlap from 14:30 to 15:30.

Example 4
Input: @event1 = ('08:00', '09:00')
       @event2 = ('09:01', '10:00')
Output: false
There is a 1-minute gap from '09:00' to '09:01'.

Example 5
Input: @event1 = ('23:30', '00:30')
       @event2 = ('00:00', '01:00')
Output: true
They overlap from '00:00' to '00:30'.

Analysis

This is somewhat harder than it looks because of ambiguity in the input. Specifically, consider the following data:

Event 1: 20:00 to 04:00, event 2: 03:00 to 19:00

Event 1 starts on (say) Monday and ends on Tuesday.

But does event 2 start at 03:00 on Monday, in which case it doesn't overlap event 1, or at 03:00 on Tuesday in which case it overlaps event 1 from 03:00 to 04:00? There's no way to tell.

To resolve this, I have assumed that if event 2 could be interpreted to start more than 12 hours before event 1, then it starts 24 later than that.

I have also assumed that if either event apparently finishes before, or at the same time as, it starts, then I assume it is running into the following day. For example, I interpret 09:00-08:00 to be a 23-hour event and 09:00-09:00 to be a 24-hour marathon.

Having dealt with that, it's neccesary to cope with four cases of overlap:

  1. E1 starts before E2 and ends before E2 ends.
  2. E1 starts before E2 and ends after E2 ends.
  3. E1 starts after E2 and ends after E2 ends.
  4. E1 starts after E2 and ends before E2 ends

and two of non-overlap:

  1. E1 starts after E2 ends.
  2. E1 ends before E2 starts.

Try it 

Try running the script with any input:



example: 10:00, 13:00



example: 12:00, 14:00

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2026-03-30
use utf8;     # Week 367 - task 2 - Conflict events
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

conflict_events('09:00', '12:00', '10:00', '13:00');
conflict_events('09:00', '12:00', '10:00', '11:00');
conflict_events('09:00', '12:00', '08:00', '13:00');
conflict_events('09:00', '12:00', '08:00', '11:00');

conflict_events('22:00', '02:00', '23:00', '03:00');
conflict_events('22:00', '02:00', '23:00', '01:00');
conflict_events('22:00', '02:00', '21:00', '00:00');
conflict_events('22:00', '02:00', '00:00', '03:00');

conflict_events('09:00', '12:00', '12:00', '15:00');
conflict_events('12:00', '15:00', '09:00', '12:00');

sub conflict_events {
    
    my (@events, $e, @times, $overlap_starts, $overlap_ends);
    
    # initialise
    @events = @_;
    say qq[\nInput:  $events[0]-$events[1] and $events[2]-$events[3]];
    
    # convers times to mins past midnight
    for $e (0 .. 3) {
        if ($events[$e] =~ m|(\d\d):(\d\d)|) {
            @times[$e] = $1 * 60 + $2;
        } else {
            say qq[Output: malformed time $events[$e]];
            return;
        }
    }
    
    # if end times precede start times assume the next day
    $times[1] += 1440 if $times[1] < $times[0];
    $times[2] += 1440 if $times[0] - $times[2] >= 720;
    $times[3] += 1440 if $times[3] < $times[2];
    
    # overlap is from the latest start to the earliest finish
    $overlap_starts = $times[0] > $times[2] ? $events[0] : $events[2];
    $overlap_ends = $times[1] > $times[3] ? $events[3] : $events[1];
    
    # report
    if ($times[2] >= $times[1] or $times[0] >= $times[3]) {
        say qq[Output: false - events do not overlap];
    } else {
        say qq[Output: true - events overlap from $overlap_starts until $overlap_ends]; 
    }
}       

last updated 2026-03-30 — 19 lines of code

Output


Input:  09:00-12:00 and 10:00-13:00
Output: true - events overlap from 10:00 until 12:00

Input:  09:00-12:00 and 10:00-11:00
Output: true - events overlap from 10:00 until 11:00

Input:  09:00-12:00 and 08:00-13:00
Output: true - events overlap from 09:00 until 12:00

Input:  09:00-12:00 and 08:00-11:00
Output: true - events overlap from 09:00 until 11:00

Input:  22:00-02:00 and 23:00-03:00
Output: true - events overlap from 23:00 until 02:00

Input:  22:00-02:00 and 23:00-01:00
Output: true - events overlap from 23:00 until 01:00

Input:  22:00-02:00 and 21:00-00:00
Output: true - events overlap from 22:00 until 00:00

Input:  22:00-02:00 and 00:00-03:00
Output: true - events overlap from 00:00 until 02:00

Input:  09:00-12:00 and 12:00-15:00
Output: false - events do not overlap

Input:  12:00-15:00 and 09:00-12:00
Output: false - events do not overlap

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain