Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions solution/752/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import (
"container/list"
"fmt"
"slices"
"strconv"
)

type State struct {
lock string
time int
}

func openLock(deadends []string, target string) int {
// Your code here.
if slices.Contains(deadends, target) || slices.Contains(deadends, "0000") {
return -1
}

var queue = list.New()

initial_state := State{
"0000",
0,
}
queue.PushBack(initial_state)

var state State
var element *list.Element
var visited = map[string]struct{}{}

visited[initial_state.lock] = struct{}{}
for _, deadend := range deadends {
visited[deadend] = struct{}{}
}

for queue.Len() > 0 {

element = queue.Front() // getting a generic element from list of type list.Element
state = element.Value.(State) // using type assertion
queue.Remove(element)

if state.lock == target {
return state.time
}

locks := nextLockCombination(state.lock, visited)
for _, lock := range locks {
visited[lock] = struct{}{}
queue.PushBack(State{
lock: lock,
time: state.time + 1,
})
}
}

return -1
}

func nextLockCombination(lock string, visited map[string]struct{}) []string {

nextLocks := []string{}

for idx := 0; idx < len([]rune(lock)); idx++ {
elem := lock[idx]
// fmt.Println(elem)
next_elem := (int(elem) - 48 + 1) % 10
prev_elem := (int(elem) - 48 - 1)

if prev_elem < 0 {
prev_elem += 10
}

next_lock := lock[:idx] + strconv.Itoa(next_elem) + lock[idx+1:]
prev_lock := lock[:idx] + strconv.Itoa(prev_elem) + lock[idx+1:]


_, ok := visited[next_lock]
if !ok {
nextLocks = append(nextLocks, next_lock)
}

_, ok = visited[prev_lock]
if !ok {
nextLocks = append(nextLocks, prev_lock)
}

}

return nextLocks

}