aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-06-29 00:07:16 +0200
committerAbigail <abigail@abigail.be>2021-06-29 00:07:16 +0200
commita6180cd9e375d5f36d77f7a2aac2650d5c642058 (patch)
tree1f83534c8dc823d9dba2cd1b29ec0aa9885398f0
parentcdbc2bb40b6c79ddae6896768daecb3fd70db92f (diff)
downloadperlweeklychallenge-club-a6180cd9e375d5f36d77f7a2aac2650d5c642058.tar.gz
perlweeklychallenge-club-a6180cd9e375d5f36d77f7a2aac2650d5c642058.tar.bz2
perlweeklychallenge-club-a6180cd9e375d5f36d77f7a2aac2650d5c642058.zip
AWK, C, Perl solutions for week 119, part 2.
-rw-r--r--challenge-119/abigail/awk/ch-2.awk43
-rw-r--r--challenge-119/abigail/c/ch-2.c56
-rw-r--r--challenge-119/abigail/perl/ch-2.pl39
3 files changed, 138 insertions, 0 deletions
diff --git a/challenge-119/abigail/awk/ch-2.awk b/challenge-119/abigail/awk/ch-2.awk
new file mode 100644
index 0000000000..c0f502e082
--- /dev/null
+++ b/challenge-119/abigail/awk/ch-2.awk
@@ -0,0 +1,43 @@
+#!/usr/bin/awk
+
+#
+# See ../README.md
+#
+
+#
+# Run as: awk -f ch-2.awk < input-file
+#
+
+function next_num (prev_num, tail) {
+ match (prev_num, /3*$/)
+ tail = substr (prev_num, RSTART)
+ gsub (/3/, 1, tail)
+ if (RLENGTH == length (prev_num)) {
+ prev_num = 1 tail
+ }
+ else {
+ prev_num = substr (prev_num, 1, RSTART - 2) \
+ (substr (prev_num, RSTART - 1, 1) + 1) \
+ tail
+ }
+
+ #
+ # Replace the trailing 1s with 1212...
+ #
+ if (match (prev_num, /1+$/)) {
+ tail = substr (prev_num, RSTART)
+ gsub (/11/, "12", tail)
+ prev_num = substr (prev_num, 1, RSTART - 1) tail
+ }
+
+ return prev_num
+}
+
+
+{
+ n = 0
+ for (i = 0; i < $1; i ++) {
+ n = next_num(n)
+ }
+ print (n)
+}
diff --git a/challenge-119/abigail/c/ch-2.c b/challenge-119/abigail/c/ch-2.c
new file mode 100644
index 0000000000..45effb48c5
--- /dev/null
+++ b/challenge-119/abigail/c/ch-2.c
@@ -0,0 +1,56 @@
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+
+/*
+ * See ../README.md
+ */
+
+/*
+ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file
+ */
+# define BUF_SIZE 32
+int main (void) {
+ int num;
+ char number [BUF_SIZE + 1];
+
+ number [BUF_SIZE] = '\0';
+
+ while (scanf ("%d", &num) == 1) {
+ /*
+ * Set the string to all 0s
+ */
+ for (int i = 0; i < BUF_SIZE; i ++) {
+ number [i] = '0';
+ }
+ while (num --) {
+ int i;
+ /*
+ * All trailing 3s will be turned into 1s
+ */
+ for (i = BUF_SIZE - 1; i > 0 && number [i] == '3'; i --) {
+ number [i] = '1';
+ }
+ /*
+ * Increment the digit before the trailing 3s
+ */
+ number [i] ++;
+ /*
+ * Replace every second 1 into a 2 of the trailing 1s
+ */
+ for (i = BUF_SIZE - 1; i > 0 && number [i] == '1'; i --);
+ i += 2;
+ for (;i < BUF_SIZE; i += 2) {
+ number [i] = '2';
+ }
+ }
+ /*
+ * Print the number, without the leading 0s
+ */
+ int i;
+ for (i = 0; i < BUF_SIZE && number [i] == '0'; i ++);
+ printf ("%s\n", number + i);
+ }
+
+ return (0);
+}
diff --git a/challenge-119/abigail/perl/ch-2.pl b/challenge-119/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..b53a827411
--- /dev/null
+++ b/challenge-119/abigail/perl/ch-2.pl
@@ -0,0 +1,39 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-2.pl < input-file
+#
+
+sub next_num ($prev_num) {
+ #
+ # First, replace any trailing 3's with 1's, incrementing the
+ # digit which comes before.
+ # Then replace any trailing sequence of 1s with 12121... of the same
+ # length.
+ # Note we prepend the incoming number with "00" so we can anchor
+ # against it; we remove any leading 0s at the end.
+ #
+ "00$prev_num" =~
+ s!([012])(3*)$!($1 + 1) . (1 x length $2)!re =~
+ s!([023])((?:11)+)(1?)$!$1 . (12 x ((length $2) / 2)) . $3!re =~
+ s!^0+!!r;
+}
+
+while (<>) {
+ my $n = 0;
+ $n = next_num $n for 1 .. $_;
+ say $n;
+}