aboutsummaryrefslogtreecommitdiff
path: root/cpc/cpc1.rb
diff options
context:
space:
mode:
authorNat Lasseter <nat.lasseter@york.ac.uk>2024-01-22 10:23:58 +0000
committerNat Lasseter <nat.lasseter@york.ac.uk>2024-01-22 10:23:58 +0000
commita79414bcf14d17c58e040a7b8524f7bd8f7e5e03 (patch)
tree4f2f232aca568711ecb35caa3444b54f4dfa4361 /cpc/cpc1.rb
Migrate from gists
Diffstat (limited to 'cpc/cpc1.rb')
-rw-r--r--cpc/cpc1.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/cpc/cpc1.rb b/cpc/cpc1.rb
new file mode 100644
index 0000000..1113de0
--- /dev/null
+++ b/cpc/cpc1.rb
@@ -0,0 +1,23 @@
+codewords = []
+
+class Integer
+ def rotate_left(bits, width = bit_width)
+ bits %= width
+ (self << bits | self >> (width - bits)) & ((1 << width) - 1)
+ end
+end
+
+(2**8).times do |candidate|
+ valid_candidate_p = true
+ 8.times do |bits|
+ rotated_candidate = candidate.rotate_left(bits, 8)
+ valid_candidate_p = false if codewords.include?(rotated_candidate)
+ end
+ codewords << candidate if valid_candidate_p
+end
+
+p codewords
+puts codewords.length
+
+#=> [0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 37, 39, 43, 45, 47, 51, 53, 55, 59, 61, 63, 85, 87, 91, 95, 111, 119, 127, 255]
+#=> 36