diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-04-25 23:40:26 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-04-25 23:40:26 +0800 |
| commit | 201c70e6d8d5374f4cecbc5712eec671266b3702 (patch) | |
| tree | 849abc78113a6e89ad8094ad8dc10c1ee70994f2 /challenge-109 | |
| parent | c4020a60c8b87c299cb4814fb328a475adf930c9 (diff) | |
| download | perlweeklychallenge-club-201c70e6d8d5374f4cecbc5712eec671266b3702.tar.gz perlweeklychallenge-club-201c70e6d8d5374f4cecbc5712eec671266b3702.tar.bz2 perlweeklychallenge-club-201c70e6d8d5374f4cecbc5712eec671266b3702.zip | |
2 Perl scripts ; Java application for Task 2
Diffstat (limited to 'challenge-109')
| -rw-r--r-- | challenge-109/cheok-yin-fung/java/Box.java | 34 | ||||
| -rw-r--r-- | challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java | 201 | ||||
| -rw-r--r-- | challenge-109/cheok-yin-fung/java/Point.java | 19 | ||||
| -rw-r--r-- | challenge-109/cheok-yin-fung/perl/ch-1.pl | 16 | ||||
| -rw-r--r-- | challenge-109/cheok-yin-fung/perl/ch-2.pl | 178 |
5 files changed, 448 insertions, 0 deletions
diff --git a/challenge-109/cheok-yin-fung/java/Box.java b/challenge-109/cheok-yin-fung/java/Box.java new file mode 100644 index 0000000000..4f3b0cdcc6 --- /dev/null +++ b/challenge-109/cheok-yin-fung/java/Box.java @@ -0,0 +1,34 @@ +// The Weekly Challenge - 109 +// Task 2 Four Squares(Rectangles) Puzzle +// Supporting codes + +class Box +{ + private Point tl, br; + + Box(int x1, int y1, int x2, int y2) { + tl = new Point(x1, y1); + br = new Point(x2, y2); + } + + Box(Point p1, Point p2) { + this(p1.x(), p1.y(), p2.x(), p2.y()); + } + + Point topLeft() { + return tl; + } + + int height() { + return br.x()-tl.x(); + } + + int width() { + return br.y()-tl.y(); + } + + boolean isEnclosed(Point p) { + return p.x() > tl.x() && p.x() < br.x() && + p.y() > tl.y() && p.y() < br.y(); + } +} diff --git a/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java new file mode 100644 index 0000000000..ad507def6e --- /dev/null +++ b/challenge-109/cheok-yin-fung/java/NBoxesJavaFX.java @@ -0,0 +1,201 @@ +// The Weekly Challenge - 109 +// Task 2 Four Squares(Rectangles) Puzzle +// Run with Box.java and Point.java +// JavaFX Usage may refer to https://github.com/openjfx/samples/tree/master/HelloFX +// Coding References: +// https://www.tutorialspoint.com/how-to-create-a-rectangle-using-javafx +// https://www.javatpoint.com/javafx-text + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.BitSet; + + +// JavaFX /* + +import javafx.application.Application; +import javafx.scene.Group; +import javafx.scene.Scene; +import javafx.scene.paint.Color; +import javafx.stage.Stage; +import javafx.scene.shape.Rectangle; +import javafx.scene.text.*; + +// */ JavaFX + +public class NBoxesJavaFX extends Application +// public class NBoxesJavaFX // for non-JavaFX +{ + + static Box[] box = new Box[] { + new Box( new Point(9,6), new Point(24,15) ), + new Box( new Point(20,10), new Point(35,19) ), + new Box( new Point(31,6), new Point(46,15) ), + new Box( new Point(42,10), new Point(56,19) ) + }; + + static Point[] point = new Point[] { + new Point(16,8), //a + new Point(22,12), //b + new Point(28,16), //c + new Point(33,12), //d + new Point(38,8), //e + new Point(44,12), //f + new Point(49,16) //g + }; + + public static final int N = box.length; + public static final int M = point.length; + + public static int[] oneOfAns = new int[M]; + public static ArrayList<int[]> finalAnswers = new ArrayList<>(); + +// ========== BEGIN: functions for permutations ================== + public static ArrayList<Integer> copy(ArrayList<Integer> val) + { + ArrayList<Integer> a = new ArrayList<>(); + for (int i=0; i < val.size(); i++) { + a.add(Integer.valueOf(val.get(i))); + } + return a; + } + + public static ArrayList<int[]> permutations(int[] values) + { + int m = values.length; + ArrayList<ArrayList<Integer>> arr = new ArrayList<>(); + ArrayList<Integer> temp = new ArrayList<>(); + temp.add(Integer.valueOf(values[0])); + arr.add(temp); + for (int i = 2; i <= m; i++) { + int cp = arr.size(); + for (int c=0; c < i-1; c++) + for (int d=0; d < cp; d++) { + arr.add( copy( arr.get(d) ) ); + } + int count = 0; + int parameter = arr.size()/i; + for (int k=0; k < i ; k++) { + for (int c = 0; c < parameter; c++) { + ArrayList<Integer> item = arr.get(count); + item.add(k, Integer.valueOf(values[i-1])); + count++; + } + } + + } + + ArrayList<int[]> ans = new ArrayList<>(); + Iterator<ArrayList<Integer>> i_arr = arr.iterator(); + while (i_arr.hasNext()) { + ans.add(toIntArray( i_arr.next() )); + } + return ans; + } + + public static int[] toIntArray (ArrayList<Integer> sth) { + int[] ans = new int[sth.size()]; + for (int i = 0; i < sth.size(); i++ ) + ans[i] = sth.get(i); + return ans; + } + +// =========== END: functions for permutations ================== + +// =========== BEGIN: functions for JavaFX ======================== +// /* + public void start(Stage stage) + { + Group group = new Group(); + + Rectangle[] shape = new Rectangle[N]; + for (int i=0; i<N; i++) { + shape[i] = new Rectangle(); + shape[i].setX(box[i].topLeft().x()*15.0); + shape[i].setY(box[i].topLeft().y()*15.0); + shape[i].setWidth((box[i].width()+5)*15.0); + shape[i].setHeight(box[i].height()*15.0); + shape[i].setFill(Color.TRANSPARENT); + shape[i].setStrokeWidth(1.0); + shape[i].setStroke(Color.DARKSLATEGREY); + group.getChildren().add(shape[i]); + } + + Rectangle shape1 = new Rectangle(); + + Text[] text = new Text[M]; + Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 15); + for (int j=0; j<M; j++) { + text[j] = new Text(); + text[j].setFont(font); + text[j].setX(point[j].x()*15.0); + text[j].setY(point[j].y()*15.0); + text[j].setText(Integer.toString(oneOfAns[j])); + group.getChildren().add(text[j]); + } + + Scene scene = new Scene(group, 1000, 600, Color.WHITE); + stage.setTitle("One of the Solutions for the N-Boxes"); + stage.setScene(scene); + stage.show(); + } + + public static void printing() + { + launch(); + } +// */ +// =========== END: functions for JavaFX ======================== + + public static void main(String... args) + { + + + BitSet[] board = new BitSet[N]; + + for (int i=0; i<N; i++) { + board[i] = new BitSet(M); + for (int j=0; j<M; j++) + board[i].set( j, box[i].isEnclosed(point[j]) ); + } + + ArrayList<int[]> p = permutations(new int[] {1,2,3,4,5,6,7}); + + + + Iterator<int[]> i_p = p.iterator(); + while (i_p.hasNext()) { + int[] now = i_p.next(); + + boolean consistency = true; + int sum = 0; + for (int j=0; j<M; j++) + if (board[0].get(j)) + sum += now[j]; + + for (int i=1; consistency && i<N; i++) { + int currentSum = 0; + for (int j=0; j<M; j++) + if (board[i].get(j)) + currentSum += now[j]; + consistency = sum == currentSum; + } + + if (consistency) { + finalAnswers.add(now); + } + } + + System.out.println("Number of solution(s): " + finalAnswers.size()); + if (finalAnswers.size() != 0) { + System.out.println("|a, b, c, d, e, f, g|\n"); + for (int[] term: finalAnswers ) + { + System.out.println(Arrays.toString(term)); + } + oneOfAns = finalAnswers.get((int)(Math.random()*finalAnswers.size()) ); + printing(); // JavaFX + } + } +} diff --git a/challenge-109/cheok-yin-fung/java/Point.java b/challenge-109/cheok-yin-fung/java/Point.java new file mode 100644 index 0000000000..5f9d14435e --- /dev/null +++ b/challenge-109/cheok-yin-fung/java/Point.java @@ -0,0 +1,19 @@ +// The Weekly Challenge - 109 +// Task 2 Four Squares(Rectangles) Puzzle +// Supporting codes +class Point +{ + private int x, y; + Point(int x, int y) { + this.x = x; + this.y = y; + } + + int x() { + return x; + } + + int y() { + return y; + } +} diff --git a/challenge-109/cheok-yin-fung/perl/ch-1.pl b/challenge-109/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..c42ab429b0 --- /dev/null +++ b/challenge-109/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +# The Weekly Challenge - 109 +# Task 1 Chowla Numbers +use strict; +use warnings; +my $F = $ARGV[0] || 20; +my @chowla_seq = (0,0,0); +for my $n (4..$F) { + my $s = 0; + for my $k (2..$n-1) { + $s += $k unless $n % $k; + } + push @chowla_seq, $s; +} +print join ", ", @chowla_seq; +print "\n"; diff --git a/challenge-109/cheok-yin-fung/perl/ch-2.pl b/challenge-109/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..923d2e666e --- /dev/null +++ b/challenge-109/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,178 @@ +#!/usr/bin/perl +=pod + + + (1) (3) + ╔══════════════╗ ╔══════════════╗ + ║ ║ ║ ║ + ║ a ║ ║ e ║ + ║ ║ (2) ║ ║ (4) + ║ ┌───╫──────╫───┐ ┌───╫─────────┐ + ║ │ ║ ║ │ │ ║ │ + ║ │ b ║ ║ d │ │ f ║ │ + ║ │ ║ ║ │ │ ║ │ + ║ │ ║ ║ │ │ ║ │ + ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │ + │ c │ │ g │ + │ │ │ │ + │ │ │ │ + └──────────────┘ └─────────────┘ + +=cut +# The Weekly Challenge 109 +# Task 2 Boxes +# --- + +use strict; +use warnings; +use Algorithm::Combinatorics qw/permutations/; + +{ + package Point; + use strict; + + sub new { + my ($class) = @_; + bless { + _x => $_[1], + _y => $_[2], + }, $class; + } + + sub x { $_[0]->{_x} } + sub y { $_[0]->{_y} } + +} + +{ + package Box; + use strict; + + sub new { + my ($class) = @_; + bless { + _tl => $_[1], #top left + _br => $_[2], #bottem right + }, $class; + } + + sub tl { $_[0]->{_tl} } + sub br { $_[0]->{_br} } + +} + + +# ================= BEGIN: Parameters input ============== + +my @boxes = ( + Box->new( Point->new(9,6), Point->new(24,15) ), + Box->new( Point->new(20,10), Point->new(35,19) ), + Box->new( Point->new(31,6), Point->new(46,15) ), + Box->new( Point->new(42,10), Point->new(56,19) ), +); + +my @var = ( + Point->new(16,8), #a + Point->new(22,12), #b + Point->new(28,16), #c + Point->new(33,12), #d + Point->new(38,8), #e + Point->new(44,12), #f + Point->new(49,16), #g +); + +my @possible_val = (1,2,3,4,5,6,7); + + +# ================= END: Parameters input ============== + +my $M = scalar @possible_val; +my $N = scalar @boxes; + +my @container; + +for my $i_b (0..$N-1) { + my $b = $boxes[$i_b]; + $container[$i_b] = []; + for my $v_ind (0..$M-1) { + if ( $var[$v_ind]->x < $b->br->x + && $var[$v_ind]->x > $b->tl->x + && $var[$v_ind]->y > $b->tl->y + && $var[$v_ind]->y < $b->br->y) { + push @{$container[$i_b]}, $v_ind; + } + } +} + +my $trial = permutations(\@possible_val); + +my @possible_soln; + +while (my $p = $trial->next) { + # attempt_soln ---> @{$p}; + my $invariant = 0; + for (@{$container[ 0 ]}) { + $invariant += $p->[$_]; } + my $consistency = 1; + for my $i_b (1..$N-1) { + my $this_sum = 0; + for (@{$container[ $i_b ]}) { + $this_sum += $p->[$_]; } + $consistency = ($this_sum == $invariant); + last if !$consistency; + } + push @possible_soln, $p if $consistency; +} + +my $NUM_ANS = scalar @possible_soln; +print "Number of solutions: ", $NUM_ANS, "\n"; + +if ($NUM_ANS != 0) { + print " ",chr(ord('a')+$_) for (0..$M-1); + print "\n"; + for my $temp (0..$#possible_soln) { + for my $number (@{$possible_soln[$temp]}) { + printf("%3d", $number); + } + print "\n"; + } + print "\n"; +} + + + +sub printing { + my @soln = @_; + my $max_x = 0; + my $max_y = 0; + for my $b (@boxes) { + $max_x = $max_x > $b->br->x ? $max_x : $b->br->x; + $max_y = $max_y > $b->br->y ? $max_y : $b->br->y; + } + my @row; + $row[$_] = [(' ') x $max_x] for (0..$max_y-1); + for my $i_b (0..$N-1) { + my $b = $boxes[$i_b]; + for my $i ($b->tl->x-1..$b->br->x-1) {$row[$b->tl->y-1][$i] = '*';} + for my $i ($b->tl->x-1..$b->br->x-1) {$row[$b->br->y-1][$i] = '*';} + for my $i ($b->tl->y-1..$b->br->y-1) {$row[$i][$b->tl->x-1] = '*';} + for my $i ($b->tl->y-1..$b->br->y-1) {$row[$i][$b->br->x-1] = '*';} + } + for my $i_v (0..$M-1) { + $row[$var[$i_v]->y-1]->[$var[$i_v]->x-1] = $soln[$i_v]; + } + + print "One of the possible solution(s):\n"; + + for my $j (0..$max_y-1) { + for my $i (0..$max_x-1) { + print $row[$j]->[$i]; + } + print "\n"; + } + + print "\n" x 3; +} + +printing(@{$possible_soln[ int(rand($NUM_ANS))]}) + if (scalar (map {length $_ == 1} @possible_val) == $M) && $NUM_ANS != 0; |
