1
0
Fork 0

Compare commits

..

2 Commits

Author SHA1 Message Date
Bill Ewanick ab90e9c219 Solve Project Euler 20 2023-11-15 17:05:40 -05:00
Bill Ewanick 02693c7419 L33t code 841, by David Schindler 2023-11-15 17:05:27 -05:00
2 changed files with 15 additions and 18 deletions

View File

@ -80,7 +80,6 @@
clean
go
gofumpt
]);
shellHook = ''

View File

@ -3,22 +3,22 @@ package main
import "fmt"
func main() {
res := canVisitAllRooms([][]int{{1, 3}, {3, 0, 1}, {2}, {0}})
res := canVisitAllRooms([][]int{{1}, {2}, {}})
fmt.Printf("%+v\n", res)
}
func canVisitAllRooms(rooms [][]int) bool {
if len(rooms) > 0 && len(rooms[0]) == 0 {
return false
return false
}
keys := getAllKeys(0, make(map[int]struct{}), rooms)
keys[0] = struct{}{}
keys[0] = struct{}{}
for i := 0; i < len(rooms); i++ {
if _, ok := keys[i]; !ok {
return false
}
if _, ok := keys[i]; !ok {
return false
}
}
return true
}
@ -27,17 +27,15 @@ func getAllKeys(index int, seen map[int]struct{}, rooms [][]int) map[int]struct{
keys := map[int]struct{}{}
for j := 0; j < len(rooms[index]); j++ {
if _, ok := seen[rooms[index][j]]; ok {
continue
} else {
seen[rooms[index][j]] = struct{}{}
}
keys[rooms[index][j]] = struct{}{}
insideKeys := getAllKeys(rooms[index][j], seen, rooms)
for k := range insideKeys {
keys[k] = struct{}{}
}
if _, ok := keys[rooms[index][j]]; ok {
continue
}
keys[rooms[index][j]] = struct{}{}
insideKeys := getAllKeys(j, keys, rooms)
for k := range insideKeys {
keys[k] = struct{}{}
}
}
return keys
}