Skip to content
Open
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
5 changes: 5 additions & 0 deletions codecov.yml → .github/codecov.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
coverage:
range: "70...100"
comment:
layout: "condensed_header, condensed_files, condensed_footer"
hide_project_coverage: true
ignore:
- main.go
- debug.go
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Dmitrii Barsukov
Copyright (c) 2024-present Dmitrii Barsukov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ func (c MockCommand) String() string {

func ExecuteCommands(commands Commands) {
for _, command := range commands {
fmt.Fprintln(commandOutput, command)
_, _ = fmt.Fprintln(commandOutput, command)
}
}
8 changes: 4 additions & 4 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"github.com/stretchr/testify/assert"
)

type mockWriter struct {
type commandMockWriter struct {
data []byte
}

func (w *mockWriter) Write(p []byte) (n int, err error) {
func (w *commandMockWriter) Write(p []byte) (n int, err error) {
w.data = p
return len(p), nil
}
Expand All @@ -21,10 +21,10 @@ func TestMockCommand_String(t *testing.T) {
}

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

want := "1 2\n"
got := commandOutput.(*mockWriter).data
got := commandOutput.(*commandMockWriter).data
assert.Equal(t, want, string(got))
}
46 changes: 8 additions & 38 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,25 @@ import (
// 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)
}

jsonData, _ := json.Marshal(data)
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)
}
_, _ = gz.Write(jsonData)
_ = gz.Close()

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)

gzData, _ := base64.StdEncoding.DecodeString(encodedData)
gz, _ := gzip.NewReader(bytes.NewBuffer(gzData))
_ = gz.Close()
var jsonData bytes.Buffer
if _, err = jsonData.ReadFrom(gz); err != nil {
panic(err)
}

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

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

// Example of game business logic.

type Unit struct {
x, y, z float64
}

type Turn struct {
Power float64
L, R string
}

type Game struct {
Units []Unit
}
16 changes: 14 additions & 2 deletions geometry_line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,13 @@ func TestLine_ClosestPointToLine(t *testing.T) {
want: Point{0, 400},
},
{
name: `diagonal`,
name: `diagonal from`,
line: Line{Point{150, 150}, Point{250, 300}},
point: Point{50, 50},
want: Point{73, 35},
},
{
name: `diagonal to`,
line: Line{Point{0, 0}, Point{100, 50}},
point: Point{400, 0},
want: Point{320, 160},
Expand Down Expand Up @@ -324,7 +330,13 @@ func TestLine_ClosestPointToSegment(t *testing.T) {
want: Point{0, 300},
},
{
name: `diagonal`,
name: `diagonal from`,
line: Line{Point{150, 150}, Point{250, 300}},
point: Point{50, 50},
want: Point{150, 150},
},
{
name: `diagonal to`,
line: Line{Point{0, 0}, Point{100, 50}},
point: Point{400, 0},
want: Point{100, 50},
Expand Down
20 changes: 4 additions & 16 deletions input.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
package main

// Example of how to read input data into game business objects.

import (
"fmt"
)

type Unit struct {
x, y, z float64
}

type Turn struct {
Power float64
L, R string
}

type Game struct {
Units []Unit
}

func InputGame(data []string) Game {
var err error
var game Game
Expand All @@ -25,17 +14,16 @@ func InputGame(data []string) Game {
size = StrToInt(data[0])
data = data[1:]
var unit Unit
units := make([]Unit, 0, size)
game.Units = make([]Unit, 0, size)
for i := 0; i < size; i++ {
_, err = fmt.Sscan(data[i], &unit.x, &unit.y, &unit.z)
if err != nil {
panic(err)
}
units = append(units, unit)
game.Units = append(game.Units, unit)
}

// some additional logic
game.Units = units

return game
}
Expand Down
11 changes: 11 additions & 0 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func TestInputGame(t *testing.T) {
},
}
assert.Equal(t, want, game)

data := []string{
"1",
"1 2 a",
}
assert.Panics(t, func() { InputGame(data) })
}

func TestInputStep(t *testing.T) {
Expand All @@ -26,4 +32,9 @@ func TestInputStep(t *testing.T) {
R: "L",
}
assert.Equal(t, want, turn)

data := []string{
"a a a",
}
assert.Panics(t, func() { InputStep(data) })
}
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

// Example main: Read game state, construct business objects, apply logic, and execute commands.

import (
"bufio"
"math/rand"
Expand All @@ -17,7 +19,6 @@ func init() {
}

func main() {
// example
scanner := bufio.NewScanner(os.Stdin)
scanner.Buffer(make([]byte, 1000000), 1000000)

Expand All @@ -39,5 +40,10 @@ func main() {

// some game logic for the next step
u(game, step)

commands := Commands{
MockCommand{1, 2},
}
ExecuteCommands(commands)
}
}
6 changes: 5 additions & 1 deletion math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ func TestMedian(t *testing.T) {
values := []float64{
0, 2, 4, 6, 8, 10, 1000,
}

assert.EqualValues(t, 6, Median(values))

values = []float64{
0, 2, 4, 6, 8, 10,
}
assert.EqualValues(t, 5, Median(values))
}

func TestExpectedExpDiff(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions reader.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

// Example of reading game state from the standard input stream.

import (
"bufio"
)

// Reading the game state from the standard input stream.

// ReadGame reads the game state from the standard input stream.
func ReadGame(s *bufio.Scanner) []string {
data := make([]string, 0, 32)
Expand Down
2 changes: 2 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func TestStrToInt(t *testing.T) {
assert.Equal(t, -1, StrToInt(s))
s = "0"
assert.Equal(t, 0, StrToInt(s))
s = "a"
assert.Panics(t, func() { StrToInt(s) })
}

func TestBoolToInt(t *testing.T) {
Expand Down