From e43f3c64047017a44ccbdacbcac6785c34548a5c Mon Sep 17 00:00:00 2001 From: Jan Date: Thu, 28 Nov 2024 23:08:55 +0100 Subject: [PATCH] some inital dirty go code --- v1/sud.go | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++ v1/sudoku.sud | Bin 0 -> 60 bytes 2 files changed, 128 insertions(+) create mode 100644 v1/sud.go create mode 100644 v1/sudoku.sud diff --git a/v1/sud.go b/v1/sud.go new file mode 100644 index 0000000..7135ff2 --- /dev/null +++ b/v1/sud.go @@ -0,0 +1,128 @@ +package main + +import ( + "fmt" + "os" + "strconv" + "strings" +) + +const Version = "1.1.1" + +type Sudoku struct { + Title string + Author string + Rules string + Board [][]int +} + +func Serialize() { + var serialized []byte + var header []byte + var body []byte + var footer []byte + var length uint16 + var empty byte = 0 + + //HEADER + version := getVersion() + header = append(header, byte(version>>8), byte(version)) + + //BODY + title := stringToBytes("Sudoku") + author := stringToBytes("John Doe") + rules := stringToBytes("Fill the board with numbers from 1 to 9") + + body = append(body, title...) + body = append(body, empty) + body = append(body, author...) + body = append(body, empty) + body = append(body, rules...) + body = append(body, empty) + + //FOOTER + + //LENGTH + length = uint16(len(header) + 2 + len(body) + len(footer)) + header = append(header, byte(length>>8), byte(length)) + + serialized = append(serialized, header...) + serialized = append(serialized, body...) + serialized = append(serialized, footer...) + os.WriteFile("sudoku.sud", serialized, 0644) +} + +func Deserialize() { + serialized, err := os.ReadFile("sudoku.sud") + if err != nil { + panic(err) + } + + printBytes(serialized) + + version := uint16(serialized[0])<<8 | uint16(serialized[1]) + + if version != getVersion() { + printBytes([]byte{byte(version >> 8), byte(version)}) + println("=/") + printBytes([]byte{byte(getVersion() >> 8), byte(getVersion())}) + panic("Invalid version") + } +} + +func getVersion() uint16 { + version := strings.Split(Version, ".") + + majorInt, err := strconv.Atoi(version[0]) + if err != nil || majorInt > 15 { + fmt.Println("Invalid major version, the major version must be an integer between 0 and 15") + panic(err) + } + major := uint16(majorInt) + + minorInt, err := strconv.Atoi(version[1]) + if err != nil || minorInt > 15 { + fmt.Println("Invalid minor version, the minor version must be an integer between 0 and 15") + panic(err) + } + minor := uint16(minorInt) + + patchInt, err := strconv.Atoi(version[2]) + if err != nil || patchInt > 255 { + fmt.Println("Invalid patch version, the patch version must be an integer between 0 and 255") + panic(err) + } + patch := uint16(patchInt) + + versionbyte := major<<12 | minor<<8 | patch + + return versionbyte +} + +func usedBits(n uint16) int { + bits := 0 + for n > 0 { + bits++ + n >>= 1 + } + return bits +} + +func printBytes(bytes []byte) { + for _, n := range bytes { + fmt.Printf("%08b ", n) + fmt.Print("\n") + } +} + +func stringToBytes(s string) []byte { + return []byte(s) +} + +func main() { + Serialize() + Deserialize() + + print(usedBits(5)) + printBytes(stringToBytes("Hello, World!")) +} diff --git a/v1/sudoku.sud b/v1/sudoku.sud new file mode 100644 index 0000000000000000000000000000000000000000..64a2e0f2455d74bb83195941525ee411069032f2 GIT binary patch literal 60 zcmWe+WUvV?P07zLW$?<+$Ww61Pi1h+%*jzG$w*a5%1