commit eef434e314ef6b71370c426e7e443f19d29b2d2d
parent 290e6e438afe5f422392452da03129afa2eae724
Author: david cochran <about.trout@gmail.com>
Date: Mon, 31 Jan 2022 02:47:14 +0000
add layer1 solution
Diffstat:
3 files changed, 70 insertions(+), 0 deletions(-)
diff --git a/bitwise.go b/bitwise.go
@@ -0,0 +1,14 @@
+package tdo
+
+import (
+ "log"
+ "math/bits"
+)
+
+func FlipSecondBits(b byte) byte {
+ return (b ^ 0b01010101) | (b & 0b10101010)
+}
+
+func CycleRight(b byte) byte {
+ return (b << 7) | (b >> 1)
+}
diff --git a/bitwise_test.go b/bitwise_test.go
@@ -0,0 +1,35 @@
+package tdo
+
+import (
+ "bytes"
+ "testing"
+)
+
+func TestCycleRight(t *testing.T) {
+ tests := []struct{ input, want byte }{
+ {0b00000000, 0b00000000},
+ {0b10101010, 0b01010101},
+ {0b01010101, 0b10101010},
+ {0b10000001, 0b11000000},
+ {0b11111111, 0b11111111},
+ }
+ for _, test := range tests {
+ if got := CycleRight(test.input); got != test.want {
+ t.Errorf("CycleRight(%08b) = %08b; want %08b", test.input, got, test.want)
+ }
+ }
+}
+
+func TestFlipSecondBits(t *testing.T) {
+ tests := []struct{ input, want byte }{
+ {0b00000000, 0b01010101},
+ {0b10101010, 0b11111111},
+ {0b10110100, 0b11100001},
+ {0b11111111, 0b10101010},
+ }
+ for _, test := range tests {
+ if got := FlipSecondBits(test.input); got != test.want {
+ t.Errorf("FlipSecondBits(%08b) = %08b; want %08b", test.input, got, test.want)
+ }
+ }
+}
diff --git a/cmd/layer1/decode.go b/cmd/layer1/decode.go
@@ -0,0 +1,21 @@
+package main
+
+import (
+ "log"
+ "os"
+
+ "github.com/abtrout/tdo"
+)
+
+func main() {
+ bs, err := tdo.DecodePipedInput()
+ if err != nil {
+ log.Fatalf("Failed to read layer input: %v", err)
+ }
+ for i, b := range bs {
+ bs[i] = tdo.CycleRight(tdo.FlipSecondBits(b))
+ }
+ if _, err := os.Stdout.Write(bs); err != nil {
+ log.Fatalf("Failed to write decoded output: %v", err)
+ }
+}