From b61107f1bb11bbca80bbda7c0f9a63221eec1bbe Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 28 Jun 2021 15:58:12 -0400 Subject: All but Blogging --- challenge-119/dave-jacoby/node/ch-1.js | 23 +++++++++++++++ challenge-119/dave-jacoby/node/ch-2.js | 51 ++++++++++++++++++++++++++++++++ challenge-119/dave-jacoby/perl/ch-1.pl | 24 +++++++++++++++ challenge-119/dave-jacoby/perl/ch-2.pl | 54 ++++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 challenge-119/dave-jacoby/node/ch-1.js create mode 100644 challenge-119/dave-jacoby/node/ch-2.js create mode 100644 challenge-119/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-119/dave-jacoby/perl/ch-2.pl diff --git a/challenge-119/dave-jacoby/node/ch-1.js b/challenge-119/dave-jacoby/node/ch-1.js new file mode 100644 index 0000000000..6d1075d34d --- /dev/null +++ b/challenge-119/dave-jacoby/node/ch-1.js @@ -0,0 +1,23 @@ +"use strict"; + +for (let i in Array(21).fill("")) { + let v = flopped(i); + console.log(["", i, v].join("\t")); +} + +console.log(["", 86, flopped(86)].join("\t")); +console.log(["", 101, flopped(101)].join("\t")); +console.log(["", 18, flopped(18)].join("\t")); +console.log(["", 33, flopped(33)].join("\t")); + +function flopped(n) { + let b = parseInt(n).toString(2); + while (b.length < 8) { + b = "0" + b; + } + let front = b.substring(0, 4); + let back = b.substring(4); + let r = back + front; + let x = parseInt(r, 2); + return x; +} diff --git a/challenge-119/dave-jacoby/node/ch-2.js b/challenge-119/dave-jacoby/node/ch-2.js new file mode 100644 index 0000000000..7386e2a0db --- /dev/null +++ b/challenge-119/dave-jacoby/node/ch-2.js @@ -0,0 +1,51 @@ +"use strict"; + +// let list = Array(20).fill(); +// for (let i in list) { +// let j = first_pass(i); +// let s = get_sequence(i * 2); +// let v = solve_sequence(i); +// console.log(["", "i", i, "j", j, "v", v].join("\t")); +// } + +let list = [2, 5, 10, 60, 200]; + +for (let i in list) { + let n = list[i]; + let s = solve_sequence(n); + console.log(["", n, s].join("\t")); +} + +function solve_sequence(n) { + let j = n * 2; + let s = get_sequence(1 + j); + while (s[n] == undefined) { + j = j * 2; + s = get_sequence(j); + } + return s[n]; +} + +function get_sequence(n) { + n = parseInt(n); + let sequence = Array(n) + .fill("") + .map((x, i) => first_pass(i + 1)) + .filter((x) => !x.toString().match(/0/)) + .filter((x) => !x.toString().match(/11/)); + sequence.unshift(0); + return sequence; +} + +function first_pass(n) { + if (n == 0) { + return 0; + } + let output = []; + while (n) { + let i = n % 4; + n = parseInt(n / 4); + output.push(i); + } + return output.reverse().join(""); +} diff --git a/challenge-119/dave-jacoby/perl/ch-1.pl b/challenge-119/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..08672aa798 --- /dev/null +++ b/challenge-119/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use feature qw{say state signatures}; +use strict; +use warnings; +use utf8; +no warnings qw{ experimental }; + +for my $n ( 0 .. 20 ) { + say join "\t", '', $n, flopped($n); +} + + say join "\t", '', 86, flopped(86); + say join "\t", '', 101, flopped(101); + say join "\t", '', 18, flopped(18); + say join "\t", '', 33, flopped(33); + +sub flopped ($n) { + my $b = sprintf '%08b', $n; + my $c = join '', substr( $b, 4, 4 ), substr( $b, 0, 4 ); + my $r = oct( '0b' . $c ); + return $r; +} + diff --git a/challenge-119/dave-jacoby/perl/ch-2.pl b/challenge-119/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..702ef4b13a --- /dev/null +++ b/challenge-119/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ postderef say signatures state }; +no warnings qw{ experimental }; + +use Memoize; +memoize('first_pass'); + +my @list = map { int } @ARGV; +@list = ( 2, 5, 10, 60, 200 ) unless scalar @list; + +for my $n (@list) { + say join "\t", '', $n, solve_sequence($n); +} + +# here we get an array such that index $n is in the array +# using increasingly aggressive methods, then +sub solve_sequence( $n ) { + my $j = $n * 2; + my @sequence = get_sequence( 1 .. $j ); + while ( !$sequence[$n] ) { + $j = $j * 2; + @sequence = get_sequence( 1 .. $j ); + } + return $sequence[$n]; +} + +# the next things we want to do are to remove the blocked numbers +# which contain either 0 or 11, and then add another entry to the +# start of the array so that 1 aligns with 1. +sub get_sequence( @arr ) { + my @seq = + grep { !/11/ } + grep { !/0/ } + map { first_pass($_) } @arr; + unshift @seq, ''; + return @seq; +} + +# the numbers will contain only the digits 1, 2 and 3, so to limit +# the amount of numbers we have to come up with, I first make everything +# base 4. This function is memoizable and so I memoized it. +sub first_pass ( $n ) { + return $n if $n == 0; + my @output; + while ($n) { + my $i = $n % 4; + $n = int $n / 4; + push @output, $i; + } + return join '', reverse @output; +} -- cgit