Added peak amount of goroutines as final stat for day 6

This commit is contained in:
Melody Becker 2024-12-06 22:48:18 +01:00
parent 1014cbb42c
commit b7325b93f6

View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"runtime"
"strings" "strings"
"sync" "sync"
@ -27,6 +28,8 @@ const (
MapStateGuardRight MapStateGuardRight
) )
var amountOfGoroutines = 1
func parseStringIntoMapLine(line string) []MapState { func parseStringIntoMapLine(line string) []MapState {
mapLine := []MapState{} mapLine := []MapState{}
for _, r := range line { for _, r := range line {
@ -229,6 +232,10 @@ func runCheck(area Map, addChan chan any, wg *sync.WaitGroup, isRoot bool) {
checkedStates := []State{} checkedStates := []State{}
for area.isGuardInMap() { for area.isGuardInMap() {
if isRoot { if isRoot {
activeRoutines := runtime.NumGoroutine()
if activeRoutines > amountOfGoroutines {
amountOfGoroutines = activeRoutines
}
nextElem, nextX, nextY := area.getFieldGuardIsFacing() nextElem, nextX, nextY := area.getFieldGuardIsFacing()
// fmt.Printf("Guard in root is facing %d:%d of type %d\n", nextX, nextY, nextElem) // fmt.Printf("Guard in root is facing %d:%d of type %d\n", nextX, nextY, nextElem)
// If is root and next field is empty, launch a new goroutine with that field being an obstacle // If is root and next field is empty, launch a new goroutine with that field being an obstacle
@ -294,4 +301,5 @@ func main() {
wg.Wait() wg.Wait()
close(addChan) close(addChan)
fmt.Printf("Task 2: %d\n", acc) fmt.Printf("Task 2: %d\n", acc)
fmt.Printf("Peak nr of goroutines: %d\n", amountOfGoroutines)
} }