mirror of
https://github.com/onyx-and-iris/aoc2024.git
synced 2026-04-08 18:13:36 +00:00
add day-02 + benchmarks
This commit is contained in:
6
day-02/internal/one/benchmark
Normal file
6
day-02/internal/one/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-02/internal/one
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkMain-12 1000000000 0.0003352 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-02/internal/one 0.009s
|
||||
16
day-02/internal/one/check.go
Normal file
16
day-02/internal/one/check.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package one
|
||||
|
||||
import "math"
|
||||
|
||||
func checkWithComparator(nums []int, comparator comparator) bool {
|
||||
for i := 1; i < len(nums); i++ {
|
||||
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func isSafe(m, n int) bool {
|
||||
return int(math.Abs(float64(m-n))) <= 3
|
||||
}
|
||||
69
day-02/internal/one/check_internal_test.go
Normal file
69
day-02/internal/one/check_internal_test.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCheckIncreased(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
input []int
|
||||
want bool
|
||||
}{
|
||||
"nominal": {
|
||||
input: []int{1, 3, 6, 7, 9},
|
||||
want: true,
|
||||
},
|
||||
"unsafe": {
|
||||
input: []int{1, 3, 2, 4, 8},
|
||||
want: false,
|
||||
},
|
||||
"two numbers the same": {
|
||||
input: []int{1, 3, 3, 4, 8},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
cmp := func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckDecreased(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
input []int
|
||||
want bool
|
||||
}{
|
||||
"nominal": {
|
||||
input: []int{7, 6, 4, 2, 1},
|
||||
want: true,
|
||||
},
|
||||
"unsafe": {
|
||||
input: []int{9, 7, 6, 2, 1},
|
||||
want: false,
|
||||
},
|
||||
"two numbers the same": {
|
||||
input: []int{8, 6, 4, 4, 1},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
cmp := func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
40
day-02/internal/one/solve.go
Normal file
40
day-02/internal/one/solve.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
|
||||
)
|
||||
|
||||
type comparator func([]int, int) bool
|
||||
|
||||
func Solve(data []byte) (int, error) {
|
||||
r := bytes.NewReader(data)
|
||||
sum := parseLines(r)
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func parseLines(r io.Reader) int {
|
||||
var sum int
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
nums := util.IntsFromString(scanner.Text())
|
||||
if check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}) || check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
func check(fn func([]int, comparator) bool, nums []int, comparator comparator) bool {
|
||||
return fn(nums, comparator)
|
||||
}
|
||||
15
day-02/internal/one/solve_internal_test.go
Normal file
15
day-02/internal/one/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package one
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testdata/input.txt
|
||||
var data []byte
|
||||
|
||||
func BenchmarkMain(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
6
day-02/internal/two/benchmark
Normal file
6
day-02/internal/two/benchmark
Normal file
@@ -0,0 +1,6 @@
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/onyx-and-iris/aoc2024/day-02/internal/two
|
||||
cpu: Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
|
||||
BenchmarkMain-12 1000000000 0.0005847 ns/op
|
||||
ok github.com/onyx-and-iris/aoc2024/day-02/internal/two 0.011s
|
||||
32
day-02/internal/two/check.go
Normal file
32
day-02/internal/two/check.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"math"
|
||||
"slices"
|
||||
)
|
||||
|
||||
func checkWithComparator(nums []int, comparator comparator, count int) bool {
|
||||
if count > 1 {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := 1; i < len(nums); i++ {
|
||||
if comparator(nums, i) || !isSafe(nums[i-1], nums[i]) {
|
||||
count++
|
||||
removeFirst, removeSecond := nextLevels(nums, i)
|
||||
return checkWithComparator(removeFirst, comparator, count) ||
|
||||
checkWithComparator(removeSecond, comparator, count)
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func isSafe(m, n int) bool {
|
||||
return int(math.Abs(float64(m-n))) <= 3
|
||||
}
|
||||
|
||||
func nextLevels(nums []int, i int) ([]int, []int) {
|
||||
first, second := slices.Clone(nums), slices.Clone(nums)
|
||||
return append(first[:i-1], first[i:]...), append(second[:i], second[i+1:]...)
|
||||
}
|
||||
61
day-02/internal/two/check_internal_test.go
Normal file
61
day-02/internal/two/check_internal_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIncreasedWithDampener(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
input []int
|
||||
want bool
|
||||
}{
|
||||
"one violation: two numbers the same": {
|
||||
input: []int{72, 74, 74, 77, 78},
|
||||
want: true,
|
||||
},
|
||||
"one violation: not in order": {
|
||||
input: []int{72, 74, 75, 77, 63},
|
||||
want: true,
|
||||
},
|
||||
|
||||
"two violations: three numbers the same": {
|
||||
input: []int{74, 74, 74, 77, 78},
|
||||
want: false,
|
||||
},
|
||||
"two violations: two numbers the same and not in order": {
|
||||
input: []int{72, 74, 74, 72, 78},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
cmp := func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp, 0)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecreasedWithDampener(t *testing.T) {
|
||||
tt := map[string]struct {
|
||||
input []int
|
||||
want bool
|
||||
}{}
|
||||
|
||||
cmp := func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}
|
||||
|
||||
for name, tc := range tt {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got := checkWithComparator(tc.input, cmp, 0)
|
||||
assert.Equal(t, tc.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
41
day-02/internal/two/solve.go
Normal file
41
day-02/internal/two/solve.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/onyx-and-iris/aoc2024/day-02/internal/util"
|
||||
)
|
||||
|
||||
type comparator func([]int, int) bool
|
||||
|
||||
func Solve(data []byte) (int, error) {
|
||||
r := bytes.NewReader(data)
|
||||
sum := parseLines(r)
|
||||
|
||||
return sum, nil
|
||||
}
|
||||
|
||||
func parseLines(r io.Reader) int {
|
||||
var sum int
|
||||
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
nums := util.IntsFromString(scanner.Text())
|
||||
if check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||||
return nums[i-1] >= nums[i]
|
||||
}) || check(checkWithComparator, nums, func(nums []int, i int) bool {
|
||||
return nums[i-1] <= nums[i]
|
||||
}) {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
func check(fn func([]int, comparator, int) bool, nums []int, comparator comparator) bool {
|
||||
var count int
|
||||
return fn(nums, comparator, count)
|
||||
}
|
||||
15
day-02/internal/two/solve_internal_test.go
Normal file
15
day-02/internal/two/solve_internal_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package two
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
//go:embed testdata/input.txt
|
||||
var data []byte
|
||||
|
||||
func BenchmarkMain(b *testing.B) {
|
||||
os.Stdout, _ = os.Open(os.DevNull)
|
||||
Solve(data)
|
||||
}
|
||||
22
day-02/internal/util/util.go
Normal file
22
day-02/internal/util/util.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func MustConv(s string) int {
|
||||
n, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func IntsFromString(s string) []int {
|
||||
nums := []int{}
|
||||
for _, r := range strings.Fields(s) {
|
||||
nums = append(nums, MustConv(r))
|
||||
}
|
||||
return nums
|
||||
}
|
||||
Reference in New Issue
Block a user