From 614efeb1256d39938f8fad487cdd02a5aa0e7b27 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Sat, 29 Aug 2020 12:49:15 +0200 Subject: ash 075 --- challenge-075/ash/blog.txt | 1 + challenge-075/ash/blog1.txt | 1 + challenge-075/ash/cpp/ch-2.cpp | 41 +++++++++++++++++++++++++++++++++++++ challenge-075/ash/raku/ch-1.raku | 26 ++++++++++++++++++++++++ challenge-075/ash/raku/ch-2.raku | 44 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+) create mode 100644 challenge-075/ash/blog.txt create mode 100644 challenge-075/ash/blog1.txt create mode 100644 challenge-075/ash/cpp/ch-2.cpp create mode 100644 challenge-075/ash/raku/ch-1.raku create mode 100644 challenge-075/ash/raku/ch-2.raku diff --git a/challenge-075/ash/blog.txt b/challenge-075/ash/blog.txt new file mode 100644 index 0000000000..804c56654c --- /dev/null +++ b/challenge-075/ash/blog.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2020/08/29/coins-sum-the-raku-challenge-week-75-task-1/ diff --git a/challenge-075/ash/blog1.txt b/challenge-075/ash/blog1.txt new file mode 100644 index 0000000000..e1998ba852 --- /dev/null +++ b/challenge-075/ash/blog1.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2020/08/29/largest-rectangle-histogram-the-raku-challenge-week-75-task-2/ diff --git a/challenge-075/ash/cpp/ch-2.cpp b/challenge-075/ash/cpp/ch-2.cpp new file mode 100644 index 0000000000..130f41839e --- /dev/null +++ b/challenge-075/ash/cpp/ch-2.cpp @@ -0,0 +1,41 @@ +/* + Task 2 from https://perlweeklychallenge.org/blog/perl-weekly-challenge-075/ + + Comments: https://andrewshitov.com/2020/08/29/largest-rectangle-histogram-the-raku-challenge-week-75-task-2/ + + How to compile: + $ g++ --std=c++17 ch-2.cpp +*/ + +#include +#include + +using namespace std; + +int main() { + vector hist = {3, 2, 3, 5, 7, 5}; + + int max_area = 0; + int max_start; + int max_end; + + for (auto start = 0; start <= hist.size(); start++) { + for (auto end = start + 1; end < hist.size(); end++) { + auto min_height = INT_MAX; + for (auto i = start; i != end; i++) { + if (hist[i] < min_height) min_height = hist[i]; + } + + auto area = min_height * (1 + end - start); + + if (area > max_area) { + max_area = area; + max_start = start; + max_end = end; + } + } + } + + cout << "The biggest rectangle is between the columns " << max_start << " and " << max_end + << " and its area is " << max_area << ".\n"; +} diff --git a/challenge-075/ash/raku/ch-1.raku b/challenge-075/ash/raku/ch-1.raku new file mode 100644 index 0000000000..681643dc6a --- /dev/null +++ b/challenge-075/ash/raku/ch-1.raku @@ -0,0 +1,26 @@ +#!/usr/bin/env raku + +# Task 1 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-075/ + +# Comments: https://andrewshitov.com/2020/08/29/the-raku-challenge-week-75/ + +my @coins = 1, 2, 4; +my $sum = 6; + +my @wallet; +@wallet.append: $_ xx ($sum div $_) for @coins; + +# say @wallet; + +.say for @wallet.combinations.unique(:as(*.Str)).grep({$sum == [+] $_}); + +# Output: +# +# $ raku ch-1.raku +# (2 4) +# (1 1 4) +# (2 2 2) +# (1 1 2 2) +# (1 1 1 1 2) +# (1 1 1 1 1 1) diff --git a/challenge-075/ash/raku/ch-2.raku b/challenge-075/ash/raku/ch-2.raku new file mode 100644 index 0000000000..7253841f3f --- /dev/null +++ b/challenge-075/ash/raku/ch-2.raku @@ -0,0 +1,44 @@ +#!/usr/bin/env raku + +# Task 2 from +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-075/ + +# Comments: https://andrewshitov.com/2020/08/29/largest-rectangle-histogram-the-raku-challenge-week-75-task-2/ + +# my @hist = 2, 1, 4, 5, 3, 7; +my @hist = 3, 2, 3, 5, 7, 5; + +my $max = 0; +my @max; + +for ^@hist -> $start { + for $start ^..^ @hist -> $end { + my $area = min(@hist[$start .. $end]) * (1 + $end - $start); + # say "$start..$end = $area"; + if $area > $max { + $max = $area; + @max = $start, $end; + } + } +} + +say "The biggest rectangle is between the columns @max[0] and @max[1] and its area is $max."; + +# Output: +# $ raku ch-2.raku +# 0..1 = 4 +# 0..2 = 6 +# 0..3 = 8 +# 0..4 = 10 +# 0..5 = 12 +# 1..2 = 4 +# 1..3 = 6 +# 1..4 = 8 +# 1..5 = 10 +# 2..3 = 6 +# 2..4 = 9 +# 2..5 = 12 +# 3..4 = 10 +# 3..5 = 15 +# 4..5 = 10 +# The biggest rectangle is between the columns 3 and 5 and its area is 15. -- cgit