Skip to content

challenge-312 #11701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions challenge-312/peter-meszaros/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env perl
#
=head1 Task 1: Minimum Time

Submitted by: Mohammad Sajid Anwar

You are given a typewriter with lowercase english letters a to z arranged in a circle.

Typing a character takes 1 sec. You can move pointer one character clockwise or
anti-clockwise.

The pointer initially points at a.

Write a script to return minimum time it takes to print the given string.

=head2 Example 1

Input: $str = "abc"
Output: 5

The pointer is at 'a' initially.
1 sec - type the letter 'a'
1 sec - move pointer clockwise to 'b'
1 sec - type the letter 'b'
1 sec - move pointer clockwise to 'c'
1 sec - type the letter 'c'

=head2 Example 2

Input: $str = "bza"
Output: 7

The pointer is at 'a' initially.
1 sec - move pointer clockwise to 'b'
1 sec - type the letter 'b'
1 sec - move pointer anti-clockwise to 'a'
1 sec - move pointer anti-clockwise to 'z'
1 sec - type the letter 'z'
1 sec - move pointer clockwise to 'a'
1 sec - type the letter 'a'

=head2 Example 3

Input: $str = "zjpc"
Output: 34

=cut

use strict;
use warnings;
use Test2::V0 -no_srand => 1;
use Data::Dumper;

my $cases = [
["abc", 5, "Example 1"],
["bza", 7, "Example 2"],
["zjpc", 34, "Example 3"],
];

sub minimum_time
{
my $str = shift;
my $time = 0;

my $pos = 0;
for my $c (split //, $str) {
my $new_pos = ord($c) - ord('a');
my $diff = abs($new_pos - $pos);
$time += $diff > 13 ? 26 - $diff : $diff;
$pos = $new_pos;
$time++;
}
return $time;
}

for (@$cases) {
is(minimum_time($_->[0]), $_->[1], $_->[2]);
}
done_testing();

exit 0;
78 changes: 78 additions & 0 deletions challenge-312/peter-meszaros/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env perl
#
=head1 Task 2: Balls and Boxes

Submitted by: Mohammad Sajid Anwar

There are $n balls of mixed colors: red, blue or green. They are all
distributed in 10 boxes labelled 0-9.

You are given a string describing the location of balls.

Write a script to find the number of boxes containing all three colors. Return
0 if none found.

=head2 Example 1

Input: $str = "G0B1R2R0B0"
Output: 1

The given string describes there are 5 balls as below:
Box 0: Green(G0), Red(R0), Blue(B0) => 3 balls
Box 1: Blue(B1) => 1 ball
Box 2: Red(R2) => 1 ball

=head2 Example 2

Input: $str = "G1R3R6B3G6B1B6R1G3"
Output: 3

The given string describes there are 9 balls as below:
Box 1: Red(R1), Blue(B1), Green(G1) => 3 balls
Box 3: Red(R3), Blue(B3), Green(G3) => 3 balls
Box 6: Red(R6), Blue(B6), Green(G6) => 3 balls

=head2 Example 3

Input: $str = "B3B2G1B3"
Output: 0

Box 1: Green(G1) => 1 ball
Box 2: Blue(B2) => 1 ball
Box 3: Blue(B3) => 2 balls

=cut

use strict;
use warnings;
use Test2::V0 -no_srand => 1;
use Data::Dumper;

my $cases = [
["G0B1R2R0B0", 1, "Example 1"],
["G1R3R6B3G6B1B6R1G3", 3, "Example 2"],
["B3B2G1B3", 0, "Example 3"],
];

sub balls_and_boxes
{
my $str = shift;

my %boxes;
while ($str =~ /([RGB])(\d)/g) {
++$boxes{$2}{$1};
}

my $count = 0;
for my $box (keys %boxes) {
++$count if keys %{$boxes{$box}} == 3;
}
return $count;
}

for (@$cases) {
is(balls_and_boxes($_->[0]), $_->[1], $_->[2]);
}
done_testing();

exit 0;
76 changes: 76 additions & 0 deletions challenge-312/peter-meszaros/tcl/ch-1.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env tclsh
#
# Task 1: Minimum Time
#
# Submitted by: Mohammad Sajid Anwar
#
# You are given a typewriter with lowercase english letters a to z arranged in a circle.
#
# Typing a character takes 1 sec. You can move pointer one character clockwise or
# anti-clockwise.
#
# The pointer initially points at a.
#
# Write a script to return minimum time it takes to print the given string.
#
# Example 1
#
# Input: $str = "abc"
# Output: 5
#
# The pointer is at 'a' initially.
# 1 sec - type the letter 'a'
# 1 sec - move pointer clockwise to 'b'
# 1 sec - type the letter 'b'
# 1 sec - move pointer clockwise to 'c'
# 1 sec - type the letter 'c'
#
# Example 2
#
# Input: $str = "bza"
# Output: 7
#
# The pointer is at 'a' initially.
# 1 sec - move pointer clockwise to 'b'
# 1 sec - type the letter 'b'
# 1 sec - move pointer anti-clockwise to 'a'
# 1 sec - move pointer anti-clockwise to 'z'
# 1 sec - type the letter 'z'
# 1 sec - move pointer clockwise to 'a'
# 1 sec - type the letter 'a'
#
# Example 3
#
# Input: $str = "zjpc"
# Output: 34
#

package require tcltest

set cases {
{"abc" 5 "Example 1"}
{"bza" 7 "Example 2"}
{"zjpc" 34 "Example 3"}
}

proc minimum_time {str} {
set time 0
set pos 0
foreach c [split $str ""] {
set new_pos [expr {[scan $c %c] - 97}]
set diff [expr {abs($new_pos - $pos)}]
incr time [expr {$diff > 13 ? 26 - $diff : $diff}]
set pos $new_pos
incr time
}
return $time
}

tcltest::configure -verbose {pass}
foreach case $cases {
tcltest::test [lindex $case 2] {} {
minimum_time [lindex $case 0]
} [lindex $case 1]
}

exit 0
80 changes: 80 additions & 0 deletions challenge-312/peter-meszaros/tcl/ch-2.tcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env tclsh
#
# Task 2: Balls and Boxes
#
# Submitted by: Mohammad Sajid Anwar
#
# There are $n balls of mixed colors: red, blue or green. They are all
# distributed in 10 boxes labelled 0-9.
#
# You are given a string describing the location of balls.
#
# Write a script to find the number of boxes containing all three colors. Return
# 0 if none found.
#
# Example 1
#
# Input: $str = "G0B1R2R0B0"
# Output: 1
#
# The given string describes there are 5 balls as below:
# Box 0: Green(G0), Red(R0), Blue(B0) => 3 balls
# Box 1: Blue(B1) => 1 ball
# Box 2: Red(R2) => 1 ball
#
# Example 2
#
# Input: $str = "G1R3R6B3G6B1B6R1G3"
# Output: 3
#
# The given string describes there are 9 balls as below:
# Box 1: Red(R1), Blue(B1), Green(G1) => 3 balls
# Box 3: Red(R3), Blue(B3), Green(G3) => 3 balls
# Box 6: Red(R6), Blue(B6), Green(G6) => 3 balls
#
# Example 3
#
# Input: $str = "B3B2G1B3"
# Output: 0
#
# Box 1: Green(G1) => 1 ball
# Box 2: Blue(B2) => 1 ball
# Box 3: Blue(B3) => 2 balls
#

package require tcltest

set cases {
{"G0B1R2R0B0" 1 "Example 1"}
{"G1R3R6B3G6B1B6R1G3" 3 "Example 2"}
{"B3B2G1B3" 0 "Example 3"}
}

proc balls_and_boxes {str} {

set match [regexp -all -inline {[RGB]\d} $str]
set boxes {}
foreach ball $match {
set c [string index $ball 0]
set b [string index $ball 1]
dict set boxes $b $c 1
}

set count 0
foreach box [dict keys $boxes] {
if {[llength [dict get $boxes $box]] == 6} {
incr count
}
}
return $count
}

tcltest::configure -verbose {pass}
foreach case $cases {
tcltest::test [lindex $case 2] {} {
balls_and_boxes [lindex $case 0]
} [lindex $case 1]
}

exit 0