From 4f7da2d3e9f1043768f9663685cc3cd7a0bb0bff Mon Sep 17 00:00:00 2001 From: andrezgz Date: Fri, 20 Aug 2021 21:30:08 -0300 Subject: challenge-126 andrezgz solution --- challenge-126/andrezgz/perl/ch-1.pl | 35 +++++++++++++++++++++ challenge-126/andrezgz/perl/ch-1.sh | 1 + challenge-126/andrezgz/perl/ch-2.pl | 62 +++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 challenge-126/andrezgz/perl/ch-1.pl create mode 100644 challenge-126/andrezgz/perl/ch-1.sh create mode 100644 challenge-126/andrezgz/perl/ch-2.pl diff --git a/challenge-126/andrezgz/perl/ch-1.pl b/challenge-126/andrezgz/perl/ch-1.pl new file mode 100644 index 0000000000..9d25fdd3af --- /dev/null +++ b/challenge-126/andrezgz/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-126/ +# Task #1 +# +# Count Numbers +# You are given a positive integer $N. +# +# Write a script to print count of numbers from 1 to $N that don't contain digit 1. +# +# Example +# Input: $N = 15 +# Output: 8 +# +# There are 8 numbers between 1 and 15 that don't contain digit 1. +# 2, 3, 4, 5, 6, 7, 8, 9. +# +# Input: $N = 25 +# Output: 13 +# +# There are 13 numbers between 1 and 25 that don't contain digit 1. +# 2, 3, 4, 5, 6, 7, 8, 9, 20, 22, 23, 24, 25. + +use strict; +use warnings; + +print scalar grep {!/1/} 1..shift||0; + +__END__ + +$ ./ch-1.pl 25 +13 + +$./ch-1.pl 15 +8 diff --git a/challenge-126/andrezgz/perl/ch-1.sh b/challenge-126/andrezgz/perl/ch-1.sh new file mode 100644 index 0000000000..b966be4dc3 --- /dev/null +++ b/challenge-126/andrezgz/perl/ch-1.sh @@ -0,0 +1 @@ +perl -se 'print scalar grep {!/1/} 1..$N' -- -N=$1 diff --git a/challenge-126/andrezgz/perl/ch-2.pl b/challenge-126/andrezgz/perl/ch-2.pl new file mode 100644 index 0000000000..1128f8d020 --- /dev/null +++ b/challenge-126/andrezgz/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +# https://theweeklychallenge.org/blog/perl-weekly-challenge-126/ +# Task #2 +# +# Minesweeper Game +# You are given a rectangle with points marked with either x or *. Please consider the x as a land mine. +# +# Write a script to print a rectangle with numbers and x as in the Minesweeper game. +# +# A number in a square of the minesweeper game indicates the number of mines within the neighbouring squares (usually 8), also implies that there are no bombs on that square. +# +# Example +# Input: +# x * * * x * x x x x +# * * * * * * * * * x +# * * * * x * x * x * +# * * * x x * * * * * +# x * * * x * * * * x +# +# Output: +# x 1 0 1 x 2 x x x x +# 1 1 0 2 2 4 3 5 5 x +# 0 0 1 3 x 3 x 2 x 2 +# 1 1 1 x x 4 1 2 2 2 +# x 1 1 3 x 2 0 0 1 x + +use strict; +use warnings; +use feature qw(postderef); + +my $rows = [ + [qw/x * * * x * x x x x/], + [qw/* * * * * * * * * x/], + [qw/* * * * x * x * x */], + [qw/* * * x x * * * * */], + [qw/x * * * x * * * * x/] +]; + +my $max_row = $rows->@* - 1; +my $max_col = $rows->[0]->@* - 1; + +for my $r (0 .. $max_row) { + for my $c (0 .. $max_col) { + my $output = $rows->[$r]->[$c]; + if ($output eq '*') { + $output = 0; + for (-1,0,1) { + my $x = $r + $_; + next if $x < 0 || $x > $max_row; + for (-1,0,1) { + my $y = $c + $_; + next if $y < 0 || $y > $max_col; + $output++ if $rows->[$x]->[$y] eq 'x'; + } + } + + } + print $output,' '; + } + print "\n"; +} -- cgit