RogerBW's Blog

The Weekly Challenge 223: Counting Boxes 02 July 2023

I’ve been doing the Weekly Challenges. The latest involved prime counting and list selections. (Note that this ends today.)

Task 1: Count Primes

You are given a positive integer, $n.

Write a script to find the total count of primes less than or equal to the given integer.

It's been a while since we had a prime number problem - I think the last one was #198 part 2, which in fact is nearly the same challenge. The languages in my chosen collection that have prime number generation readily available are Ruby (in core) and Perl (via Math::Prime::Utils); for everything else I've written a lightly optimised sieve of Eratosthenes, which by its nature finds all primes with a given upper bound.

(Yes, including Raku - which has is-prime, but running that on every number is a lot slower.)

Perl:

use Math::Prime::Util qw(primes);

sub countprimes($l) {
  return scalar @{primes($l)};
}

Task 2: Box Coins

You are given an array representing box coins, @box.

Write a script to collect the maximum coins until you took out all boxes. If we pick box[i] then we collect the coins $box[i-1] * $box[i] * $box[i+1]. If $box[i+1] or $box[i-1] is out of bound then treat it as 1 coin.`

A slightly tricky parse, but it made sense with the examples. In each loop, pick one entry in the list. Add to the running total the product of that entry and its two neighbours. Remove that entry (but not the neighbours).

I found this quite reminiscent of 221 part 2 (a search that involves dropping each member in turn from the successor list), though this time I want to be exhaustive, so I use a depth-first search for simplicity. (Also, 221 was the challenge for which I skipped several of my usual languages.)

I'm a bit sloppy about defining extra variables here. Also some languages get Pair and similar types rather than an array with disparate elements.

JavaScript:

function boxcoins(ints) {
    let mx = 0;
    let stack = [ [ ints, 0 ] ];
    while (stack.length > 0) {
        const x = stack.pop();
        const t = x[0];
        const tot = x[1];
        if (tot > mx) {
            mx = tot;
        }
        for (let i = 0; i < t.length; i++) {
            let p = t[i];
            if (i > 0) {
                p *= t[i - 1];
            }
            if (i < t.length - 1) {
                p *= t[i + 1];
            }
            const stot = tot + p;
            let tt = [];
            for (let ix = 0; ix < t.length; ix++) {
                if (i != ix) {
                    tt.push(t[ix]);
                }
            }
            stack.push([tt, stot]);
        }
    }
    return mx;
}

if (boxcoins([3, 1, 5, 8]) == 167) {
  process.stdout.write("Pass");
} else {
  process.stdout.write("FAIL");
}
process.stdout.write(" ");
if (boxcoins([1, 5]) == 10) {
  process.stdout.write("Pass");
} else {
  process.stdout.write("FAIL");
}
process.stdout.write("\n");

Full code on github.

See also:
The Weekly Challenge 198: Count Max

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

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 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 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 existential risk falklands war fandom fanfic fantasy feminism 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 hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2021 hugo 2022 hugo 2023 hugo 2024 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 opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant 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 television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 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