perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 367 - Task 2: Conflict Events

  1 #!/usr/bin/env perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-367/#TASK2
  3 #
  4 # Task 2: Conflict Events
  5 # =======================
  6 #
  7 # You are given two events start and end time.
  8 #
  9 # Write a script to find out if there is a conflict between the two events. A
 10 # conflict happens when two events have some non-empty intersection.
 11 #
 12 ## Example 1
 13 ##
 14 ## Input: @event1 = ("10:00", "12:00")
 15 ##        @event2 = ("11:00", "13:00")
 16 ## Output: true
 17 ##
 18 ## Both events overlap from "11:00" to "12:00".
 19 #
 20 ## Example 2
 21 ##
 22 ## Input: @event1 = ("09:00", "10:30")
 23 ##        @event2 = ("10:30", "12:00")
 24 ## Output: false
 25 ##
 26 ## Event1 ends exactly at 10:30 when Event2 starts.
 27 ## Since the problem defines intersection as non-empty, exact boundaries touching is not a conflict.
 28 #
 29 ## Example 3
 30 ##
 31 ## Input: @event1 = ("14:00", "15:30")
 32 ##        @event2 = ("14:30", "16:00")
 33 ## Output: true
 34 ##
 35 ## Both events overlap from 14:30 to 15:30.
 36 #
 37 ## Example 4
 38 ##
 39 ## Input: @event1 = ("08:00", "09:00")
 40 ##        @event2 = ("09:01", "10:00")
 41 ## Output: false
 42 ##
 43 ## There is a 1-minute gap from "09:00" to "09:01".
 44 #
 45 ## Example 5
 46 ##
 47 ## Input: @event1 = ("23:30", "00:30")
 48 ##        @event2 = ("00:00", "01:00")
 49 ## Output: true
 50 ##
 51 ## They overlap from "00:00" to "00:30".
 52 #
 53 ############################################################
 54 ##
 55 ## discussion
 56 ##
 57 ############################################################
 58 #
 59 # For easier comparison, we translate all times hh:mm into
 60 # minutes since start of day. Then we jump into checking some
 61 # special cases:
 62 # 1. We check whether any of the dates wrap into the next
 63 #    day, like ["23:30", "00:30"] does. If both dates wrap,
 64 #    we surely have overlap at 00:00, so we can catch that
 65 #    right away.
 66 #    Then we check wether the non-wrapping date overlaps the
 67 #    wrapping one.
 68 # 2. If both dates don't wrap and have the same start time,
 69 #    that will make the dates overlap right away.
 70 # 3. If the first event starts before the second, the two
 71 #    events to overlap once the other one starts before this
 72 #    one ends.
 73 # 4. Now we only need to check the case where the second event
 74 #    starts before the first one in the same manner and we're
 75 #    done.
 76 
 77 use v5.36;
 78 
 79 conflict_events(["10:00", "12:00"], ["11:00", "13:00"]);
 80 conflict_events(["09:00", "10:30"], ["10:30", "12:00"]);
 81 conflict_events(["14:00", "15:30"], ["14:30", "16:00"]);
 82 conflict_events(["08:00", "09:00"], ["09:01", "10:00"]);
 83 conflict_events(["23:30", "00:30"], ["00:00", "01:00"]);
 84 
 85 +-- 27 lines: sub conflict_events($event1, $event2) {-------------------------------------------------------------------------------------------------------------------------------
112 
113 +--  4 lines: sub time_to_minutes($str) {-------------------------------------------------------------------------------------------------------------------------------------------