From ee7fd0942cf32ed5cfc66870ee5503a8ca934d62 Mon Sep 17 00:00:00 2001 From: BarrOff <58253563+BarrOff@users.noreply.github.com> Date: Sun, 26 May 2024 22:18:06 +0200 Subject: feat: add solutions for challenge 270 from BarrOff --- challenge-270/barroff/julia/ch-1.jl | 30 ++++++++++++++++++++++++++++++ challenge-270/barroff/raku/ch-1.p6 | 27 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 challenge-270/barroff/julia/ch-1.jl create mode 100644 challenge-270/barroff/raku/ch-1.p6 diff --git a/challenge-270/barroff/julia/ch-1.jl b/challenge-270/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..67e4b509c2 --- /dev/null +++ b/challenge-270/barroff/julia/ch-1.jl @@ -0,0 +1,30 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function special_positions(m::Matrix{T})::Int where {T<:Integer} + special_columns = filter(x -> sum(m[:, x]) == 1, 1:size(m, 2)) + return length( + filter( + x -> sum(m[x, :]) == 1, + map( + x -> findfirst( + y -> y == 1, + m[:, x] + ), special_columns) + ) + ) +end + +@testset "special positions" begin + @test special_positions( + [1 0 0; + 0 0 1; + 1 0 0 + ]) == 1 + @test special_positions( + [1 0 0; + 0 1 0; + 0 0 1 + ]) == 3 +end diff --git a/challenge-270/barroff/raku/ch-1.p6 b/challenge-270/barroff/raku/ch-1.p6 new file mode 100644 index 0000000000..e4beb0f2b4 --- /dev/null +++ b/challenge-270/barroff/raku/ch-1.p6 @@ -0,0 +1,27 @@ +#!/usr/bin/env raku + +use v6.d; + +sub special-positions(@ints --> Int) { + my @special-rows = grep({ sum(@ints[$_]) == 1 }, 0..@ints.elems - 1); + my &special-columns = sub ($i) { + my $column = @ints[$i].first: * == 1, :k; + sum(map({ @ints[$_][$column]}, 0..@ints.elems - 1)); + } + grep({ &special-columns($_) == 1 }, @special-rows).elems; +} + +#| Run test cases +multi sub MAIN('test') { + use Test; + plan 2; + + is special-positions([ [1, 0, 0], + [0, 0, 1], + [1, 0, 0], + ]), 1, 'works for first matrix'; + is special-positions([ [1, 0, 0], + [0, 1, 0], + [0, 0, 1], + ]), 3, 'works for second matrix'; +} -- cgit