-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 2023 directory, setup utils and update action
- Loading branch information
1 parent
4367f80
commit cc6524d
Showing
7 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module aoc2023 | ||
|
||
go 1.21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters