aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-31 21:16:57 +0100
committerGitHub <noreply@github.com>2021-07-31 21:16:57 +0100
commit847f60bfd75d6c9be4599bbdc5921fc926682af7 (patch)
treed3f62667afdcef4c0efbc1846a2698636e45065f
parente085ac67c613bb3c258715dd344b6c0c86c83626 (diff)
parent860d54daef008ec53ab7f49045835d1ebcbb24e3 (diff)
downloadperlweeklychallenge-club-847f60bfd75d6c9be4599bbdc5921fc926682af7.tar.gz
perlweeklychallenge-club-847f60bfd75d6c9be4599bbdc5921fc926682af7.tar.bz2
perlweeklychallenge-club-847f60bfd75d6c9be4599bbdc5921fc926682af7.zip
Merge pull request #4633 from luc65r/123
Challenge 123 in C3
-rw-r--r--challenge-123/luc65r/c3/ch-1.c338
-rw-r--r--challenge-123/luc65r/c3/ch-2.c368
2 files changed, 106 insertions, 0 deletions
diff --git a/challenge-123/luc65r/c3/ch-1.c3 b/challenge-123/luc65r/c3/ch-1.c3
new file mode 100644
index 0000000000..44dea486b3
--- /dev/null
+++ b/challenge-123/luc65r/c3/ch-1.c3
@@ -0,0 +1,38 @@
+import std::io;
+
+extern func ulong strtoull(char* str, char** str_end, int base);
+
+/**
+ * @require n > 0
+ * @pure
+ **/
+func bool ugly(ulong n) {
+ while (n != 1) {
+ /* That's ugly, but compile-time $foreach isn't implemented yet,
+ and a labelled continue segfaults the compiler… */
+ if (n % 2 == 0) {
+ n /= 2;
+ } else if (n % 3 == 0) {
+ n /= 3;
+ } else if (n % 5 == 0) {
+ n /= 5;
+ } else {
+ return false;
+ }
+ }
+ return true;
+}
+
+func int main(int argc, char** argv) {
+ if (argc != 2) return 1;
+
+ ulong n = strtoull(argv[1], null, 10);
+ ulong i;
+ while (n > 0) {
+ i++;
+ if (ugly(i)) n--;
+ }
+ io::printf("%u\n", i);
+
+ return 0;
+}
diff --git a/challenge-123/luc65r/c3/ch-2.c3 b/challenge-123/luc65r/c3/ch-2.c3
new file mode 100644
index 0000000000..05ee05296f
--- /dev/null
+++ b/challenge-123/luc65r/c3/ch-2.c3
@@ -0,0 +1,68 @@
+import std::io;
+
+extern func float strtof(char* str, char** str_end);
+
+struct Point {
+ float x, y;
+}
+
+func void Point.add(Point *a, Point b) {
+ a.x += b.x;
+ a.y += b.y;
+}
+
+func void Point.sub(Point *a, Point b) {
+ a.x -= b.x;
+ a.y -= b.y;
+}
+
+macro abs(x) {
+ return x < 0 ? -x : x;
+}
+
+const float EPSILON = 0.00001;
+
+macro bool eq(a, b) {
+ return @abs(a - b) < EPSILON;
+}
+
+func bool Point.eq(Point *a, Point b) {
+ return @eq(a.x, b.x) && @eq(a.y, b.y);
+}
+
+func int main(int argc, char** argv) {
+ if (argc != 9) return 1;
+
+ Point[4] pts = void;
+ for (int i; i < 8; i++) {
+ (*(float[8]*)(&pts))[i] = strtof(argv[i + 1], null);
+ }
+
+ Point center;
+ foreach (p : pts) center.add(p);
+ center.x /= 4;
+ center.y /= 4;
+
+ Point[4] v = void;
+ v[0] = pts[0];
+ v[0].sub(center);
+ v[1] = { v[0].x, -v[0].y };
+ v[2] = { -v[0].x, v[0].y };
+ v[3] = { -v[0].x, -v[0].y };
+
+ foreach (&x : v) x.add(center);
+
+ bool square = {|
+ for FOUND: (int i; i < 4; i++) {
+ for (int j; j < 4; j++) {
+ if (v[i].eq(pts[j])) continue FOUND;
+ }
+ return false;
+ }
+ return true;
+ |};
+
+ io::printf("%d\n", square);
+
+ return 0;
+}