diff --git a/2022/day_12/golang/graph/bfs.go b/2022/day_12/golang/graph/bfs.go index ea040e0..34ca62e 100644 --- a/2022/day_12/golang/graph/bfs.go +++ b/2022/day_12/golang/graph/bfs.go @@ -29,6 +29,39 @@ func GetShortestPath(startVertex *Vertex, endCoords Coords) int { } key = neighbour.Key() + if !visited[key] { + queue = append(queue, BFSNode{vertex: neighbour, distance: node.distance + 1}) + visited[key] = true + } + } + } + + return -1 +} + +func FindShortestPathToValue(startVertex *Vertex, targetValue byte) int { + visited := make(map[Coords]bool) + queue := make([]BFSNode, 0) + + queue = append(queue, BFSNode{vertex: startVertex, distance: 0}) + visited[startVertex.Key()] = true + + for len(queue) > 0 { + node := queue[0] + queue = queue[1:] + + if node.vertex.value == targetValue { + return node.distance + } + + nextNeighbour := node.vertex.Iter() + for { + neighbour := nextNeighbour() + if neighbour == nil { + break + } + + key := neighbour.Key() if !visited[key] { queue = append(queue, BFSNode{vertex: neighbour, distance: node.distance + 1}) diff --git a/2022/day_12/golang/main.go b/2022/day_12/golang/main.go index 20db31a..395dba9 100644 --- a/2022/day_12/golang/main.go +++ b/2022/day_12/golang/main.go @@ -21,7 +21,12 @@ func main() { panic("startVertex is nil") } - shortestPath := graph.GetShortestPath(startVertex, endCoords) + endVertex := g.GetVertex(endCoords) + if endVertex == nil { + panic("endVertex is nil") + } + + shortestPath := graph.FindShortestPathToValue(endVertex, byte('a')-96) fmt.Println(shortestPath) } @@ -92,7 +97,11 @@ func ConnectVertexesByTarget(v1 *graph.Vertex, v2 *graph.Vertex) { return } - if int(v2.Value())-int(v1.Value()) <= 1 { + delta := int(v2.Value()) - int(v1.Value()) + + // for part one with going up -> (v <= 1) + // for part two with going down -> (v >= -1) + if delta >= -1 { v1.AddEdge(v2) } } diff --git a/2022/day_12/map.txt b/2022/day_12/map.txt deleted file mode 100644 index b985a8a..0000000 --- a/2022/day_12/map.txt +++ /dev/null @@ -1,41 +0,0 @@ -...................aaaaaaaa......acccaa..................aaaaaaaaaa...............................aaaaaa..................................aaaaa -....................aaaaa.......aaaaaaa...........aa..aaaaaaaaaaaaa................................aaaaa.................................aaaaaa -..............aa....aaaaaa.......aaaaaaaa......a.aaa..aaaaaaaaaaaaaaa..................aaa.......aaaaaaa................aaa..............aaaaaa -..........a...aa....aaaaaa.......aaaaaaaaa....aaaaaaaaa.aaaaaaaaaaaaa...................aa.......aaaaaaaa...............aaa..............a..aaa -.........aaaaaaa....aaa..a.......aaaaaaaaa....aaaaaaaa....aaaaaaaaaaa................aa.aaaaa....aaaaaaaa...............aaa.................aaa -..........aaaaaaaa...............aaaaaaaa......aaaaaa.....aaaacaaaa..................aaaaaaaa......aaa.a................aaa...aaa............aa -..........aaaaaaaaa..............aaaaaaa.......aaaaa......aaaccaaaa............aa.a...aaaaa........aaa.............a.a.........aa.............. -..........aaaaaaaaa..............a..aaaaa......aaaaaa.....aaacaaa..............aaaa....aaaaa.......................aaaa.........a...aaa..a..... -..........aaaaaaa....................aaaaaaa......aa......aaaaaaaa............aaaaa...aaa.aa.......................aa.....pp........aaaaaa..... -..........aaaaaaa...................aaaaaaaa................aaaaaa............aaaaaa...aa...............................oopppp.......aaaaa..... -..........a..aaa....................aaaaaaaa.................aaaaa............a.aaaa...................................ooopppp.....a.aaaaa..... -..............aaa...................aaaaaaa.................aaaaa...............aaca..................................oooopuppp........aaaa.... -........................aaa...........aaaaaa......aaaaa.....aaaaa............aaacaaa.....aaa.........................ooouuuuupp.........aaa.... -........................aaaa..........aa.aaa......aaaaa......................aaaaaaa.....aaa.a..................oooooooouuuuuppp.........a..... -........................aaaa..........aa.........aaaaaa.......................aaaaaa...aaaaaaaa.................ooooooouuuxuuuppp.............. -........................aaa......................aaaaaa.......................aaaaaaa..aaaaaaaa...................tttuuuuxxxuupppqq............ -...........aaa........................a..........aaaaaa.....................aaaaaaaaaa..aaaaaa...................tttttuuuxxxuuvpqqqq........... -...........aaa.aa....................aaaa.aaa......aa...aaaaa...............aaaaaaaaaa...aaaaa...aaa............ttttxxxxxxxyuvvvvqqqq.......... -...........aaaaaa....................aaaaaaaa...........aaaaa...............aaa.aaa.....aaaaaa..aaaa.....aa.....tttxxxxxxxxyyvvvvvvqqq......... -.........aaaaaaa......................aaaaaaaaa.........aaaaaa..................aaa......aa.....aaaa...aaaa......ttxxxxxxxyyyyyyvvvqqq......... -..........aaaaa........aa.aa........aaaaaaaaaa..........aaaaaa............aaa...aa..............aaaa...aaaaa.....tttxxxxzzzyyyyvvvvqqq......... -...........aaaaa.......aaaaa.......aaaaaaaaaaa..........aaaaaa..........aaaaaa.........................aaaaa......tttxxxyyyyyyvvvvqqq.......... -...........aa.aaa......aaaaaa.......aaaaaaaaaa............aaa...........aaaaaa..........................aaaa.......ttxxyyyyyyvvvvqqq........... -...........aa..........aaaaaa...aa.aa..aaaaaaaaa........................aaaaaa..........................aaaa......tttxwyyyyyywvrqqq............ -.......................aaaaaa...aaaaa..aaaaacaaa........................aaaaa....aaaa....aaa......................ttwwwywwyyywwrrr............. -.........................aaa....aaaaaa.aaaaaaaaa........aaa.............aaaaa....aaaa....aaa......................sswwwwwwwwywwrrr............. -................................aaaaaa...aaaaaa.........aaaaa.....aa.............aaaa.aaaaaaaa....................sswwwwsswwwwwrrr............. -...........aaaa.................aaaaaa....aaaaaaaa.....aaaaaa..aa.aaa.............aa..aaaaaaaa....................ssssssssrwwwwrrr......aa..... -...........aaaa...................aaa....aaaaaaaaa.....aaaaaa..aaaaa....................aaaaa.....................ssssssssrrrwrrr......aaa..... -...........aaaa.........................aaaaaaaaaa......aaaaa...aaaaaa.................aaaaaa.......................sss...rrrrrrr......aaa..... -...........aaa..........................aaa.aaa.........aa.aa.aaaaaaaa.................aaaaaa.aa............................rrrr......aaaaa.... -............................................aaa..aaaa.........aaaaacaa....aa.........a.aaaaaa.aa......................................aaaaa.... -.........aa.................................aaa.aaaaa..........a.aaa......aa.........aacaaaaaaaaaaa.....aa............................aaaaa.... -.........aaa.aa............aa...................aaaaaa....aa.....aaa...aaaaaaaa......aaaaacaaaaaaaa....aaa............................aaa...... -.........aaaaaa....aa.....aaa...................aaaaaa....aaa..........aaaaaaaa.......aaaaaccaaaa....aaaaaaaa.........................aa....... -.......aaaaaaa..aaaaa.aaaaaaa...................aaaaa.....aaaa...aa......aaaa.......aaaaaaaacaaaaa...aaaaaaaa..........aaa..................... -.......aaaaaaa...aaaaaaaaaaaaaaa..................aaa.....aaaa.aaaa.aa...aaaaa......aaaaaaaaaaaaaa....aaaaaa...........aaa.aa...............aaa -.........aaaaaa..aaaaa.aaaaaaaaa............aaa...........aaaaaaaaa.aa..aaa.aa.........aa...aa..aa....aaaaaa..........aaaa..aaa.............aaa -.........aa.aaa.aaaaa...aaaaaaa.............aaaa.........aaaaaaaaaaaaa..aa...a.........aa.............aaaaaa..........aaa...................aaa -.........aa.......aaa..aaaaaa...............aaaa......aaaaaaaaaaaaaaaaaa..............................aa..aa..........aaa................aaaaaa -.......................aaaaaaa..............aaa.......aaaaaaaaaaaaaaaaaa.................................................................aaaaaa \ No newline at end of file