Skip to content

Commit

Permalink
Merge pull request #6 from mrsombre/moving
Browse files Browse the repository at this point in the history
Moving, math and game reading
  • Loading branch information
mrsombre authored Jan 16, 2024
2 parents 4a38b6b + 7e752e5 commit 941fd8e
Show file tree
Hide file tree
Showing 27 changed files with 1,123 additions and 240 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

[![codecov](https://codecov.io/gh/mrsombre/codingame-framework/graph/badge.svg?token=I8RYIUSN6Q)](https://codecov.io/gh/mrsombre/codingame-framework)

A set of algorithms and data structures for solving puzzles.

Feel free to follow me on [Codingame Profile](https://www.codingame.com/profile/9dd9f9f38412d78eaf21718bf6e87ca0626964).
A collection of algorithms and data structures designed for solving programming puzzles.

### Golang Version

Expand All @@ -21,3 +19,6 @@ go test -cover ./...
```shell
go test -bench=. -benchmem -run=^$ > bench.out
```

---
You are welcome to follow my [Codingame profile](https://www.codingame.com/profile/9dd9f9f38412d78eaf21718bf6e87ca0626964)
2 changes: 0 additions & 2 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ var (
GlobalB bool
GlobalI int
GlobalF float64

GlobalPoint Point
)
9 changes: 5 additions & 4 deletions bench.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ goos: linux
goarch: amd64
pkg: github.com/mrsombre/codingame-framework
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkLine_IsCollision-8 271175239 4.403 ns/op 0 B/op 0 allocs/op
BenchmarkLine_LinesIntersection-8 351207843 3.375 ns/op 0 B/op 0 allocs/op
BenchmarkLine_IsPointOnLine-8 515632399 2.412 ns/op 0 B/op 0 allocs/op
BenchmarkIsPointOnLine 531822363 2.250 ns/op 0 B/op 0 allocs/op
BenchmarkClosestPoint 100000000 10.82 ns/op 0 B/op 0 allocs/op
BenchmarkLinesIntersection 353300368 3.435 ns/op 0 B/op 0 allocs/op
BenchmarkLine_IsCollision 273297457 4.427 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/mrsombre/codingame-framework 4.666s
ok github.com/mrsombre/codingame-framework 5.733s
32 changes: 32 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"io"
"os"
)

// Command is an interface for game commands.

var commandOutput io.Writer = os.Stdout

type Command interface {
String() string
}

type Commands []Command

type MockCommand struct {
Param1 float64
Param2 float64
}

func (c MockCommand) String() string {
return fmt.Sprintf("%.f %.f", c.Param1, c.Param2)
}

func ExecuteCommands(commands Commands) {
for _, command := range commands {
fmt.Fprintln(commandOutput, command)
}
}
30 changes: 30 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

type mockWriter struct {
data []byte
}

func (w *mockWriter) Write(p []byte) (n int, err error) {
w.data = p
return len(p), nil
}

func TestMockCommand_String(t *testing.T) {
cmd := MockCommand{1, 2}
assert.Equal(t, "1 2", cmd.String())
}

func TestExecuteCommand(t *testing.T) {
commandOutput = &mockWriter{}
ExecuteCommands(Commands{MockCommand{1, 2}})

want := "1 2\n"
got := commandOutput.(*mockWriter).data
assert.Equal(t, want, string(got))
}
68 changes: 68 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

// Export/Import of data in the form of string arrays.
// This can be used to unload the conditions of a problem (input)
// in a compressed form into the debug console and unpack it in the IDE.

import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
)

// DataExport serializes and compresses a slice of strings,
// returning a base64 encoded string.
func DataExport(data []string) string {
var err error

jsonData, err := json.Marshal(data)
if err != nil {
panic(err)
}

var gzBuf bytes.Buffer
gz := gzip.NewWriter(&gzBuf)
if _, err = gz.Write(jsonData); err != nil {
panic(err)
}
if err = gz.Close(); err != nil {
panic(err)
}

return base64.StdEncoding.EncodeToString(gzBuf.Bytes())
}

// DataImport decodes a base64 string, decompresses it,
// and deserializes the JSON data into a slice of strings.
func DataImport(encodedData string) []string {
var err error

gzData, err := base64.StdEncoding.DecodeString(encodedData)
if err != nil {
panic(err)
}

gz, err := gzip.NewReader(bytes.NewBuffer(gzData))
if err != nil {
panic(err)
}
defer func(gz *gzip.Reader) {
err = gz.Close()
if err != nil {
panic(err)
}
}(gz)

var jsonData bytes.Buffer
if _, err = jsonData.ReadFrom(gz); err != nil {
panic(err)
}

var data []string
if err = json.Unmarshal(jsonData.Bytes(), &data); err != nil {
panic(err)
}

return data
}
24 changes: 24 additions & 0 deletions data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

var dataExportTests = []string{
"123",
"abc",
}

func TestDataExport(t *testing.T) {
data := DataExport(dataExportTests)
assert.Equal(t, dataImportTests, data)
}

var dataImportTests = `H4sIAAAAAAAA/4pWMjQyVtJRSkxKVooFBAAA//9iXM2zDQAAAA==`

func TestDataImport(t *testing.T) {
data := DataImport(dataImportTests)
assert.Equal(t, dataExportTests, data)
}
39 changes: 39 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

// A set of helper methods for outputting debug information
// to the Stderr stream in text or JSON format.

import (
"encoding/json"
"fmt"
"os"
)

var debug = true
var debugOutput = os.Stderr

func asText(a ...any) {
if !debug {
return
}
fmt.Fprintln(debugOutput, a...)
}

func asJson(a any) {
if !debug {
return
}
b, _ := json.Marshal(a)
asText(string(b))
}

func asJsonPretty(a any) {
if !debug {
return
}
b, _ := json.MarshalIndent(a, ``, ` `)
asText(string(b))
}

func u(a ...any) {
}
Loading

0 comments on commit 941fd8e

Please sign in to comment.