empty struct instead of bool for better memory usage

This commit is contained in:
David 2021-12-12 13:29:41 -05:00
parent 137cfd379b
commit c09e706d42

View File

@ -63,10 +63,10 @@ func doubleSmallAlready(visitedRooms []string) bool {
return false return false
} }
func descend(rooms map[string]room, currentRoom string, path []string, success chan bool, canVisitSmallRoomTwice bool) { func descend(rooms map[string]room, currentRoom string, path []string, success chan struct{}, canVisitSmallRoomTwice bool) {
var wg sync.WaitGroup var wg sync.WaitGroup
if currentRoom == "end" { if currentRoom == "end" {
success <- true success <- struct{}{}
return return
} }
for _, link := range rooms[currentRoom].links { for _, link := range rooms[currentRoom].links {
@ -107,10 +107,11 @@ func partOne() {
rooms[p[0]] = a rooms[p[0]] = a
rooms[p[1]] = b rooms[p[1]] = b
} }
successes := make(chan bool, 10000) successes := make(chan struct{}, 10000)
descend(rooms, "start", []string{}, successes, false) descend(rooms, "start", []string{}, successes, false)
fmt.Printf("%+v\n", len(successes)) fmt.Printf("%+v\n", len(successes))
} }
func partTwo() { func partTwo() {
scanner := makeScanner(false) scanner := makeScanner(false)
@ -133,7 +134,7 @@ func partTwo() {
rooms[p[0]] = a rooms[p[0]] = a
rooms[p[1]] = b rooms[p[1]] = b
} }
successes := make(chan bool, 200000) successes := make(chan struct{}, 200000)
descend(rooms, "start", []string{}, successes, true) descend(rooms, "start", []string{}, successes, true)
fmt.Printf("%+v\n", len(successes)) fmt.Printf("%+v\n", len(successes))
} }