Skip to content

Commit

Permalink
Add 2023 directory, setup utils and update action
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Nov 17, 2023
1 parent 4367f80 commit cc6524d
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/readmeStats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
- uses: actions/checkout@v4
- run: cd 2022 && go build .
- run: cd 2022 && ./aoc2022
- run: cd 2023 && go build .
- run: cd 2023 && ./aoc2023
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Update readme statistics
10 changes: 10 additions & 0 deletions 2023/01p1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"time"
)

func D01P1() {
// Placeholder
time.Sleep(1 * time.Millisecond)
}
10 changes: 10 additions & 0 deletions 2023/01p2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"time"
)

func D01P2() {
// Placeholder
time.Sleep(1 * time.Millisecond)
}
3 changes: 3 additions & 0 deletions 2023/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module aoc2023

go 1.21
66 changes: 66 additions & 0 deletions 2023/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"fmt"
"strconv"

"aoc2023/utils"
)

var solutions = map[string]func(){
"01P1:Placeholder": D01P1,
"01P2:Placeholder": D01P2,
}

func main() {
for _, solution := range solutions {
utils.Benchmark(solution)
}

updateReadme()
}

func updateReadme() {
statistics := utils.GetSolutionStatistics(solutions)

for _, statistic := range statistics {
fmt.Printf("%s - Part %s: %v\n", statistic.Day, statistic.Part, statistic.ExecutionTime)
}

readmeTable := "| Day | Part 1 | Part 2 | Stars |\n| --- | --- | --- | --- |\n"
for _, statistic := range statistics {
dayInt, _ := strconv.Atoi(statistic.Day)
if statistic.Part == "1" {
readmeTable += fmt.Sprintf(
"| [Day %d: %s](https://adventofcode.com/2023/day/%d) | %s | ",
dayInt,
statistic.Name,
dayInt,
statistic.ExecutionTime,
)
} else {
readmeTable += fmt.Sprintf(
"%s | ⭐⭐ |\n",
statistic.ExecutionTime,
)
}
}

readme := utils.ReadLines("../README.md")
start := 0
end := 0
for i, line := range readme {
switch line {
case "<!-- 2023TableStart -->":
start = i
case "<!-- 2023TableEnd -->":
end = i
}
}

// Remove the table and add the new one
readme = append(readme[:start+1], readme[end:]...)
readme = append(readme[:start+1], append([]string{readmeTable}, readme[start+1:]...)...)

utils.WriteLines(readme, "../README.md")
}
89 changes: 89 additions & 0 deletions 2023/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package utils

import (
"bufio"
"fmt"
"os"
"reflect"
"runtime"
"sort"
"strings"
"time"
)

type SolutionStatistics struct {
Name string
Day string
Part string
ExecutionTime time.Duration
}

func ReadLines(path string) []string {
file, err := os.Open(path)
if err != nil {
fmt.Println("Error opening file:", err)
}

defer file.Close()

var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}

return lines
}

func WriteLines(lines []string, path string) error {
file, err := os.Create(path)
if err != nil {
fmt.Println("Error creating file:", err)
return err
}
defer file.Close()

w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}

return w.Flush()
}

func Benchmark(solution func()) time.Duration {
start := time.Now()
solution()
fmt.Printf("Execution time: %v\n", time.Since(start))
return time.Since(start)
}

func GetSolutionStatistics(solutions map[string]func()) []SolutionStatistics {
var statistics []SolutionStatistics

// Sort the keys so that the statistics are always in the same order
var orderedKeys []string
for key := range solutions {
orderedKeys = append(orderedKeys, key)
}
sort.Strings(orderedKeys)

for _, name := range orderedKeys {
functionName := runtime.FuncForPC(reflect.ValueOf(solutions[name]).Pointer()).Name()
problemName := strings.Split(name, ":")[1]

averageExecutionTime := time.Duration(0)
for i := 0; i < 100; i++ {
averageExecutionTime += Benchmark(solutions[name])
}

statistics = append(statistics, SolutionStatistics{
Name: problemName,
Day: functionName[6:8],
Part: functionName[9:10],
ExecutionTime: (averageExecutionTime / 100).Round(time.Microsecond),
})
}

return statistics
}
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ All my solutions to the [Advent of Code](https://adventofcode.com/) challenges,
> Benchmarks are run via [this GitHub Action](https://github.com/scottmckendry/aoc/actions/workflows/readmeStats.yml) and are not indicative of the performance of the code on your machine.
> The action uses the `ubuntu-latest` image, and runs each solution 100 times to get an average. This is by no means a perfect benchmark, so take the results below with a grain of salt.
## 2023
<!-- 2023TableStart -->
| Day | Part 1 | Part 2 | Stars |
| --- | --- | --- | --- |
| [Day 1: Placeholder](https://adventofcode.com/2023/day/1) | 11.123ms | 8.916ms | ⭐⭐ |

<!-- 2023TableEnd -->

## 2022
<!-- 2022TableStart -->
| Day | Part 1 | Part 2 | Stars |
Expand Down

0 comments on commit cc6524d

Please sign in to comment.