Hacker News new | ask | show | jobs
by xisukar 1502 days ago
I am still trying to wrap my head around the problem. Here's my attempt at your implementation in Raku:

    sub solutions-filter(@sols, &agg, Bool:D :$flip = False) {
        my @aggregates = @sols.map({ agg($_) });
    
        my %d;
        %d{$_} += %d{$_} ?? %d{$_} !! 1 for @aggregates;
    
        return @sols.grep({ %d{ agg($_) } == 1 }) if $flip;
        return @sols.grep({ %d{ agg($_) } != 1 });
    }
    
    my @candidates = ((1..99) X (1..99)).grep({ $_[0] ≤ $_[1] });
    my @ns = @candidates;
    
    for 1..7 {
        @ns = solutions-filter @ns, { $_[0] * $_[1] };
        @ns = solutions-filter @ns, { $_[0] + $_[1] };
    }
    
    say solutions-filter(@ns, { $_[0] * $_[1] }, :flip);