Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving, math and game reading #6

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

Check warning on line 21 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L21

Added line #L21 was not covered by tests
}

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

Check warning on line 27 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L27

Added line #L27 was not covered by tests
}
if err = gz.Close(); err != nil {
panic(err)

Check warning on line 30 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L30

Added line #L30 was not covered by tests
}

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)

Check warning on line 43 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L43

Added line #L43 was not covered by tests
}

gz, err := gzip.NewReader(bytes.NewBuffer(gzData))
if err != nil {
panic(err)

Check warning on line 48 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L48

Added line #L48 was not covered by tests
}
defer func(gz *gzip.Reader) {
err = gz.Close()
if err != nil {
panic(err)

Check warning on line 53 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L53

Added line #L53 was not covered by tests
}
}(gz)

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

Check warning on line 59 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L59

Added line #L59 was not covered by tests
}

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

Check warning on line 64 in data.go

View check run for this annotation

Codecov / codecov/patch

data.go#L64

Added line #L64 was not covered by tests
}

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...)

Check warning on line 19 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L15-L19

Added lines #L15 - L19 were not covered by tests
}

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

Check warning on line 27 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L22-L27

Added lines #L22 - L27 were not covered by tests
}

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

Check warning on line 35 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L30-L35

Added lines #L30 - L35 were not covered by tests
}

func u(a ...any) {

Check warning on line 38 in debug.go

View check run for this annotation

Codecov / codecov/patch

debug.go#L38

Added line #L38 was not covered by tests
}
Loading