Skip to content

Commit 3ba1724

Browse files
committed
Adding stats
1 parent 55b2ca3 commit 3ba1724

File tree

6 files changed

+91
-5
lines changed

6 files changed

+91
-5
lines changed

2023/day22-go/main_test.go

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
6+
"runtime"
57
"testing"
68

79
"github.com/stretchr/testify/assert"
@@ -27,7 +29,37 @@ func TestFs2Test(t *testing.T) {
2729
}
2830

2931
func TestFs2Input(t *testing.T) {
30-
f, err := os.Open("input.txt")
31-
require.NoError(t, err)
32-
assert.Equal(t, 95059, fs2(f))
32+
fmt.Println("Memory usage before allocating nil slices:")
33+
PrintMemUsage()
34+
35+
AllocNilSlices(1000000)
36+
37+
fmt.Println("Memory usage after allocating nil slices:")
38+
PrintMemUsage()
39+
40+
AllocEmptySlices(1000000)
41+
42+
fmt.Println("Memory usage after allocating empty slices:")
43+
PrintMemUsage()
44+
}
45+
46+
func AllocNilSlices(n int) {
47+
for i := 0; i < n; i++ {
48+
var s []int
49+
_ = s
50+
}
51+
}
52+
53+
func AllocEmptySlices(n int) {
54+
for i := 0; i < n; i++ {
55+
s := make([]int, 0)
56+
_ = s
57+
}
58+
}
59+
60+
func PrintMemUsage() {
61+
var m runtime.MemStats
62+
runtime.ReadMemStats(&m)
63+
fmt.Printf("Alloc = %v MiB", m.Alloc)
64+
fmt.Println()
3365
}

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ Stats: Rust (56%), Go (44%)
7575
* [Day 22](https://adventofcode.com/2022/day/22): [Go](2022/day22-go/main.go)
7676
* [Day 21](https://adventofcode.com/2022/day/21): [Go](2022/day21-go/main.go)
7777
* [Day 20](https://adventofcode.com/2022/day/20): [Go](2022/day20-go/main.go)
78-
* [Day 19](https://adventofcode.com/2022/day/19): [Rust (part 1)](2022/day19-rust/src/lib.rs), [Go (part 1 and 2)](2022/day19-go/main.go)
79-
* [Day 18](https://adventofcode.com/2022/day/16): [Rust (part 1)](2022/day18-rust/src/lib.rs), [Go (part 1 and 2)](2022/day18-go/main.go)
78+
* [Day 19](https://adventofcode.com/2022/day/19): [Rust (part 1)](2022/day19-rust/src/lib.rs), [Go](2022/day19-go/main.go)
79+
* [Day 18](https://adventofcode.com/2022/day/16): [Rust (part 1)](2022/day18-rust/src/lib.rs), [Go](2022/day18-go/main.go)
8080
* [Day 17](https://adventofcode.com/2022/day/16): [Go](2022/day17-go/main.go)
8181
* [Day 16](https://adventofcode.com/2022/day/16): [Go](2022/day16-go/main.go)
8282
* [Day 15](https://adventofcode.com/2022/day/15): [Go](2022/day15-go/main.go)

cmd/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/teivah/aoc/stats
2+
3+
go 1.21.4
4+
5+
require github.com/teivah/advent-of-code v1.9.1 // indirect

cmd/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/teivah/advent-of-code v1.9.1 h1:m2U+T1q2x24a37r44cP11nOPQlgNi/M73zmTI1RUCps=
2+
github.com/teivah/advent-of-code v1.9.1/go.mod h1:p1BrdH9bL5eXxtTxuLfUSW+jYtq9fYzKhzcpTePbpsw=

cmd/stats.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strings"
8+
)
9+
10+
const totalStars = 450
11+
12+
func main() {
13+
if err := run(); err != nil {
14+
panic(err)
15+
}
16+
}
17+
18+
func run() error {
19+
f, err := os.Open("README.md")
20+
if err != nil {
21+
return err
22+
}
23+
24+
stats := map[string]int{
25+
"Go": 0,
26+
"Python": 0,
27+
"Rust": 0,
28+
}
29+
scanner := bufio.NewScanner(f)
30+
for scanner.Scan() {
31+
line := scanner.Text()
32+
33+
for language := range stats {
34+
if strings.Contains(line, fmt.Sprintf("[%s]", language)) {
35+
stats[language] += 2
36+
} else if strings.Contains(line, fmt.Sprintf("[%s ", language)) {
37+
stats[language] += 1
38+
}
39+
}
40+
}
41+
42+
for language, count := range stats {
43+
fmt.Printf("%s: %.2f%%\n", language, float64(count)/float64(totalStars)*100)
44+
}
45+
return nil
46+
}

justfile

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ gen LANGUAGE YEAR DAY:
4040
idea {{YEAR}}/day{{DAY}}-{{LANGUAGE}}
4141

4242
stats:
43+
go run cmd/stats.go
4344
echo "Go lines: $(find . -name '*.go' -exec cat {} + | wc -l)"
4445
echo "Go lines without tests: $(find . -name '*.go' ! -name '*_test.go' -exec cat {} + | wc -l)"
4546
echo "Rust lines: $(find . -name '*.rs' ! -name '*_test.go' -exec cat {} + | wc -l)"

0 commit comments

Comments
 (0)