RogerBW's Blog

The Weekly Challenge 367: Binary Conflict 05 April 2026

I’ve been doing the Weekly Challenges. The latest involved bit rearrangement and range overlaps. (Note that this ends today.)

Task 1: Max Odd Binary

You are given a binary string that has at least one '1'.

Write a script to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number and return the resulting binary string. The resulting string can have leading zeros.

The highest value string will be the one that shuffles all the 1s into the highest (leftmost) places.

The highest value odd string will be like that, but the lowest value 1 has to be in the last place.

So the output will be

  • "1" × (count of ones, minus 1)
  • "0" × (count of zeroes)
  • "1"

In Raku:

sub maxoddbinary($a) {

Count the 1s and 0s. (We don't care about the order they arrive in.)

    my $ones = 0;
    my $zeroes = 0;
    for $a.comb -> $c {
        if ($c eq '0') {
            $zeroes++;
        } elsif ($c eq '1') {
            $ones++;
        }
    }

If we didn't get at least one 1, bail out.

    if ($ones < 1) {
        return '';
    }

Otherwise, assemble the string:

    my $out = '';

All but one of the ones.

    $out ~= '1' x ($ones - 1);

All the zeroes.

    $out ~= '0' x $zeroes;

The final one.

    $out ~= '1';
    $out;
}

Task 2: Conflict Events

You are given two events start and end time.

Write a script to find out if there is a conflict between the two events. A conflict happens when two events have some non-empty intersection.

I treat this problem as a range intersection: two ranges overlap if the start of each comes before or at the end of the other. The ranges here are quantised into minutes, and ex2 demonstrates that if one end equals the other's start this is not an overlap, so "10:00 to 12:00" becomes 600 to 719 minutes (inclusive).

The fiddly bit is time ranges that span midnight, and I resolve this by making each span a list of lists. If the overall event doesn't span midnight there's just one item, as above; otherwise the first span runs from start to midnight, and the second from midnight to end.

Then I just check each range from the first span against each range from the second span, returning true for any overlap.

Crystal:

Here's a parser to convert a single time ("xx:yy") into a minute count.

def parsetime(t)
  p = t.split(":")
  p[0].to_i * 60 + p[1].to_i
end

def conflictevents(a, b)

r is the list of events, each containing a list of spans, each span itself being a list.

  r = Array(Array(Array(Int32))).new

Look at each event specification.

  [a, b].each do |t|

Get the start and end times out of it.

    st = parsetime(t[0])
    en = parsetime(t[1])

If it doesn't span midnight,

    if st < en

add a single numerical span.

      r.push([[st, en - 1]])

Otherwise, add two separate spans.

    else
      r.push([
               [st, 1440 - 1],
               [0, en - 1]
             ])
    end
  end

Look at each span in the first event

  r[0].each do |ra|

and each span in the second event. (Yeah, O(n²), but we're only making a maximum of four comparisons.)

    r[1].each do |rb|

If there is an overlap, immediately return true.

      if ra[0] <= rb[1] && rb[0] <= ra[1]
        return true
      end
    end
  end

If we got all the way through without an overlap, return fqlse.

  false
end

Full code on codeberg.

Add A Comment

Your Name
Your Email
Your Comment

Note that I will only approve comments that relate to the blog post itself, not ones that relate only to previous comments. This is to ensure that the blog remains outside the scope of the UK's Online Safety Act (2023).

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 2300ad 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech bayern beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime crystal cthulhu eternal cycling dead of winter disaster doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 essen 2024 essen 2025 existential risk falklands war fandom fanfic fantasy feminism filk film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror horrorm science fiction hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 hugo 2025 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow openscad opera parody paul temple perl perl weekly challenge photography podcast poetry politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant review reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense talon television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 typst vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1