Skip to content

Commit 1e8c52a

Browse files
committed
Move pkg out from advent-of-code-2020
== Reason December is coming and I'm very confident I'll just `cp` the `pkg` directory over or importing from the 2020 directory. Both of them are pretty bad, so to avoid this, I'll just create a common, shared library for my AoC solutions.
0 parents  commit 1e8c52a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2091
-0
lines changed

.github/workflows/quality-check.yaml

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Quality Check
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
test:
9+
name: Test and coverage
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Set up Go 1.x
13+
uses: actions/setup-go@v2
14+
with:
15+
go-version: ^1.17
16+
id: go
17+
18+
- name: Checkout code
19+
uses: actions/checkout@v2
20+
21+
- name: Get dependencies
22+
run: |
23+
go get -v -t -d ./...
24+
25+
- name: go test
26+
run: go test -v -covermode=count -coverprofile=coverage.out ./...
27+
28+
- name: Convert coverage.out to coverage.lcov
29+
uses: jandelgado/[email protected]
30+
31+
- name: Coveralls
32+
uses: coverallsapp/github-action@master
33+
with:
34+
github-token: ${{ secrets.GITHUB_TOKEN }}
35+
path-to-lcov: coverage.lcov
36+
37+
vet:
38+
name: go vet and lint
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Set up Go 1.x
42+
uses: actions/setup-go@v2
43+
with:
44+
go-version: ^1.17
45+
id: go
46+
47+
- name: Checkout code
48+
uses: actions/checkout@v2
49+
50+
- name: Get dependencies
51+
run: |
52+
go get -v -t -d ./...
53+
go get -u golang.org/x/lint/golint
54+
55+
- name: go vet
56+
run: go vet ./...
57+
58+
- name: go lint
59+
run: golint -set_exit_status ./...
60+
61+
golangci:
62+
name: golangci lint check
63+
runs-on: ubuntu-latest
64+
steps:
65+
- name: Checkout code
66+
uses: actions/checkout@v2
67+
68+
- name: golangci-lint
69+
uses: golangci/golangci-lint-action@v2
70+
with:
71+
version: v1.43.0

.golangci.yml

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
run:
2+
timeout: 10m
3+
tests: false
4+
allow-parallel-runners: true
5+
6+
linters-settings:
7+
staticcheck:
8+
go: "1.17"
9+
stylecheck:
10+
go: "1.17"
11+
cyclop:
12+
skip-tests: true
13+
misspell:
14+
locale: GB
15+
goimports:
16+
local-prefixes: github.com/yitsushi/go-aoc
17+
govet:
18+
check-shadowing: true
19+
nolintlint:
20+
allow-leading-space: false
21+
allow-unused: false
22+
require-explanation: true
23+
require-specific: false
24+
varnamelen:
25+
ignore-names:
26+
- err
27+
- idx
28+
29+
issues:
30+
max-same-issues: 0
31+
max-issues-per-linter: 0
32+
exclude-rules:
33+
- text: "github.com/yitsushi/go-aoc/"
34+
linters:
35+
- wrapcheck
36+
- text: "https://"
37+
linters:
38+
- lll
39+
- path: _test\.go
40+
linters:
41+
- goerr113
42+
- gocyclo
43+
- errcheck
44+
- gosec
45+
- dupl
46+
- funlen
47+
- testpackage
48+
49+
linters:
50+
enable-all: true
51+
disable:
52+
- golint # deprecated
53+
- interfacer # deprecated
54+
- ireturn
55+
- maligned # deprecated
56+
- scopelint # deprecated

bullshit/drop.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package bullshit
2+
3+
// DropErrorBoolean drops the error and returns with the boolean value only.
4+
func DropErrorBoolean(value bool, err error) bool {
5+
return value
6+
}
7+
8+
// DropErrorInt64 drops the error and returns with the Int64 value only.
9+
func DropErrorInt64(value int64, err error) int64 {
10+
return value
11+
}
12+
13+
// DropErrorUint64 drops the error and returns with the Uint64 value only.
14+
func DropErrorUint64(value uint64, err error) uint64 {
15+
return value
16+
}

bullshit/drop_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package bullshit_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/yitsushi/go-aoc/bullshit"
8+
"github.com/yitsushi/go-aoc/puzzle"
9+
)
10+
11+
func TestDropErrorBoolean(t *testing.T) {
12+
assert.True(t, bullshit.DropErrorBoolean(true, puzzle.NoInputError{}))
13+
assert.False(t, bullshit.DropErrorBoolean(false, puzzle.NoInputError{}))
14+
assert.True(t, bullshit.DropErrorBoolean(true, nil))
15+
assert.False(t, bullshit.DropErrorBoolean(false, nil))
16+
}
17+
18+
func TestDropErrorInt64(t *testing.T) {
19+
assert.Equal(t, int64(78), bullshit.DropErrorInt64(78, puzzle.NoInputError{}))
20+
assert.Equal(t, int64(99), bullshit.DropErrorInt64(99, nil))
21+
}
22+
23+
func TestDropErrorUint64(t *testing.T) {
24+
assert.Equal(t, uint64(78), bullshit.DropErrorUint64(78, puzzle.NoInputError{}))
25+
assert.Equal(t, uint64(99), bullshit.DropErrorUint64(99, nil))
26+
}

debug/io.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package debug
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
// WaitForKeypress a simple debugger tool. It waits for a keypress.
9+
func WaitForKeypress() {
10+
_, _ = bufio.NewReader(os.Stdin).ReadBytes('\n')
11+
}

generic/hashable.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package generic
2+
3+
// Hashable object. The hash can be used for cache.
4+
type Hashable interface {
5+
Hash() interface{}
6+
}

generic/queue.go

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package generic
2+
3+
import (
4+
"sync"
5+
)
6+
7+
// Queue is a simple queue with unique items.
8+
type Queue struct {
9+
queue []Hashable
10+
keyCache map[interface{}]bool
11+
lock sync.RWMutex
12+
}
13+
14+
// NewQueue create a new Queue.
15+
func NewQueue() Queue {
16+
return Queue{
17+
queue: []Hashable{},
18+
keyCache: map[interface{}]bool{},
19+
lock: sync.RWMutex{},
20+
}
21+
}
22+
23+
// Push items into the queue.
24+
// The queue contains only unique items.
25+
func (q *Queue) Push(items ...Hashable) {
26+
q.lock.Lock()
27+
defer q.lock.Unlock()
28+
29+
for _, item := range items {
30+
if v, found := q.keyCache[item.Hash()]; found && v {
31+
continue
32+
}
33+
34+
q.keyCache[item.Hash()] = true
35+
q.queue = append(q.queue, item)
36+
}
37+
}
38+
39+
// Pull an item from the Queue.
40+
func (q *Queue) Pull() Hashable {
41+
if len(q.queue) > 0 {
42+
q.lock.Lock()
43+
defer q.lock.Unlock()
44+
45+
item := q.queue[0]
46+
q.queue = q.queue[1:]
47+
48+
q.keyCache[item.Hash()] = false
49+
50+
return item
51+
}
52+
53+
return nil
54+
}
55+
56+
// Size of the queue.
57+
func (q *Queue) Size() int {
58+
return len(q.queue)
59+
}
60+
61+
// Empty or not?
62+
func (q *Queue) Empty() bool {
63+
return q.Size() == 0
64+
}

go.mod

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module github.com/yitsushi/go-aoc
2+
3+
go 1.17
4+
5+
require (
6+
github.com/sirupsen/logrus v1.8.1
7+
github.com/stretchr/testify v1.6.1
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245 // indirect
15+
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
16+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
17+
)

go.sum

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
5+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
6+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
7+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
8+
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
9+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
10+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
12+
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
13+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
14+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
15+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
16+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
17+
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
18+
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245 h1:GGQcbpn3KsnwsPvzzr1mDsTriyvGNKi9eo2lG3N8YdM=
19+
golang.org/x/sys v0.0.0-20201024132449-ef9fd89ba245/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
20+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21+
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
22+
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
23+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
24+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

math/const.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package math
2+
3+
import "math"
4+
5+
const rad = math.Pi / 180

math/division.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package math
2+
3+
// DivMod is a simple div and mod together.
4+
func DivMod(numerator, denominator int64) (int64, int64) {
5+
return numerator / denominator, numerator % denominator
6+
}

math/division_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package math_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/yitsushi/go-aoc/math"
7+
)
8+
9+
func TestDivMod(t *testing.T) {
10+
type args struct {
11+
numerator int64
12+
denominator int64
13+
}
14+
15+
tests := []struct {
16+
name string
17+
args args
18+
quotient int64
19+
remainder int64
20+
}{
21+
{
22+
name: "9 divmod 3",
23+
args: args{numerator: 9, denominator: 3},
24+
quotient: 3,
25+
remainder: 0,
26+
},
27+
{
28+
name: "9 divmod 4",
29+
args: args{numerator: 9, denominator: 4},
30+
quotient: 2,
31+
remainder: 1,
32+
},
33+
{
34+
name: "9 divmod 13",
35+
args: args{numerator: 9, denominator: 13},
36+
quotient: 0,
37+
remainder: 9,
38+
},
39+
}
40+
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
q, r := math.DivMod(tt.args.numerator, tt.args.denominator)
44+
if q != tt.quotient || r != tt.remainder {
45+
t.Errorf("DivMod() got = (%v, %v), want (%v, %v)", q, r, tt.quotient, tt.remainder)
46+
}
47+
})
48+
}
49+
}

0 commit comments

Comments
 (0)