Skip to content

Commit

Permalink
2022_12: golang solution(part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 24, 2022
1 parent 0fb107f commit 87f1c05
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 3 deletions.
10 changes: 7 additions & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Day 12 golang",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}/2022/day_12/golang/main.go"
},
{
"type": "lldb",
"request": "launch",
Expand Down
3 changes: 3 additions & 0 deletions 2022/day_12/golang/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/MellKam/advent_of_code/2022/day_12/golang

go 1.19
41 changes: 41 additions & 0 deletions 2022/day_12/golang/graph/bfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package graph

type BFSNode struct {
vertex *Vertex
distance int
}

func GetShortestPath(startVertex *Vertex, endCoords Coords) 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:]

key := node.vertex.Key()
if endCoords.Equal(&key) {
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})
visited[key] = true
}
}
}

return -1
}
28 changes: 28 additions & 0 deletions 2022/day_12/golang/graph/coords.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package graph

import "fmt"

type Coords struct {
x uint
y uint
}

func (c *Coords) Equal(c2 *Coords) bool {
return c.x == c2.x && c.y == c2.y
}

func (c *Coords) String() string {
return fmt.Sprintf("x: %d, y: %d", c.x, c.y)
}

func NewCoords(x uint, y uint) Coords {
return Coords{x: x, y: y}
}

func (c *Coords) X() uint {
return c.x
}

func (c *Coords) Y() uint {
return c.y
}
69 changes: 69 additions & 0 deletions 2022/day_12/golang/graph/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package graph

import "fmt"

type Graph struct {
veritces []*Vertex
Height uint
Width uint
}

func (g *Graph) AddVertex(vertex Vertex) error {
if g.ContainsKey(&vertex.key) {
return fmt.Errorf("vertex with key {%v} already exists", vertex.key.String())
}

g.veritces = append(g.veritces, &vertex)
return nil
}

func (g *Graph) GetVertex(coords Coords) *Vertex {
for _, v := range g.veritces {
if v.key.Equal(&coords) {
return v
}
}

return nil
}

func (g *Graph) ContainsKey(coords *Coords) bool {
for _, v := range g.veritces {
if v.key.Equal(coords) {
return true
}
}

return false
}

func (g *Graph) String() string {
s := "Graph {\n"
for i, vertex := range g.veritces {
if i > 0 {
s += ",\n"
}
s += fmt.Sprintf(" %v", vertex.String())

}
return s + "\n}"
}

func (g *Graph) Iter() func() *Vertex {
var index = 0
var length = len(g.veritces)

return func() *Vertex {
if index >= length {
return nil
}

v := g.veritces[index]
index++
return v
}
}

func (g *Graph) Len() int {
return len(g.veritces)
}
51 changes: 51 additions & 0 deletions 2022/day_12/golang/graph/vertex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package graph

import "fmt"

type Vertex struct {
key Coords
value byte
adjacents []*Vertex
}

func NewVertex(key Coords, value byte) Vertex {
return Vertex{key: key, value: value}
}

func (v *Vertex) AddEdge(v2 *Vertex) {
v.adjacents = append(v.adjacents, v2)
}

func (v *Vertex) Key() Coords {
return v.key
}

func (v *Vertex) Value() byte {
return v.value
}

func (v *Vertex) String() string {
s := fmt.Sprintf("Vertex[%v] {", v.key.String())
for i, vertex := range v.adjacents {
if i > 0 {
s += ", "
}
s += fmt.Sprintf("{%v}", vertex.key.String())
}
return s + "}"
}

func (v *Vertex) Iter() func() *Vertex {
var index = 0
var length = len(v.adjacents)

return func() *Vertex {
if index >= length {
return nil
}

v := v.adjacents[index]
index++
return v
}
}
98 changes: 98 additions & 0 deletions 2022/day_12/golang/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/MellKam/advent_of_code/2022/day_12/golang/graph"
)

func main() {
data, err := os.ReadFile("./2022/day_12/input.txt")
if err != nil {
panic(err)
}

g, startCoords, endCoords := FillGraph(string(data))

startVertex := g.GetVertex(startCoords)
if startVertex == nil {
panic("startVertex is nil")
}

shortestPath := graph.GetShortestPath(startVertex, endCoords)

fmt.Println(shortestPath)
}

func FillGraph(data string) (graph.Graph, graph.Coords, graph.Coords) {
var startCoords graph.Coords
var endCoords graph.Coords

lines := strings.Split(data, "\n")
height := len(lines)
width := len(lines[0])

g := graph.Graph{Width: uint(width), Height: uint(height)}

for y, line := range lines {
for x, char := range line {
var vertex graph.Vertex
coords := graph.NewCoords(uint(x), uint(y))
var value byte

if char == 'S' {
// "S" => start point (elevation 'a')
value = byte('a')
startCoords = coords
} else if char == 'E' {
// "E" => end point (elevation 'z')
value = byte('z')
endCoords = coords
} else {
value = byte(char)
}

vertex = graph.NewVertex(coords, value-96)
err := g.AddVertex(vertex)
if err != nil {
panic(err)
}
}
}

nextVertex := g.Iter()

for {
vertex := nextVertex()

if vertex == nil {
break
}

coords := vertex.Key()

left := g.GetVertex(graph.NewCoords(coords.X()-1, coords.Y()))
right := g.GetVertex(graph.NewCoords(coords.X()+1, coords.Y()))
top := g.GetVertex(graph.NewCoords(coords.X(), coords.Y()-1))
bottom := g.GetVertex(graph.NewCoords(coords.X(), coords.Y()+1))

ConnectVertexesByTarget(vertex, left)
ConnectVertexesByTarget(vertex, right)
ConnectVertexesByTarget(vertex, top)
ConnectVertexesByTarget(vertex, bottom)
}

return g, startCoords, endCoords
}

func ConnectVertexesByTarget(v1 *graph.Vertex, v2 *graph.Vertex) {
if v2 == nil {
return
}

if int(v2.Value())-int(v1.Value()) <= 1 {
v1.AddEdge(v2)
}
}
41 changes: 41 additions & 0 deletions 2022/day_12/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
abcccccccccccccccccaaaaaaaaccccccacccaaccccccccccccccccccaaaaaaaaaacccccccccccccccccccccccccccccccaaaaaaccccccccccccccccccccccccccccccccccaaaaa
abccccccccccccccccccaaaaacccccccaaaaaaacccccccccccaaccaaaaaaaaaaaaaccccccccccccccccccccccccccccccccaaaaacccccccccccccccccccccccccccccccccaaaaaa
abccccccccccccaaccccaaaaaacccccccaaaaaaaaccccccacaaaccaaaaaaaaaaaaaaaccccccccccccccccccaaacccccccaaaaaaaccccccccccccccccaaaccccccccccccccaaaaaa
abccccccccacccaaccccaaaaaacccccccaaaaaaaaaccccaaaaaaaaacaaaaaaaaaaaaacccccccccccccccccccaacccccccaaaaaaaacccccccccccccccaaaccccccccccccccaccaaa
abaacccccaaaaaaaccccaaaccacccccccaaaaaaaaaccccaaaaaaaaccccaaaaaaaaaaaccccccccccccccccaacaaaaaccccaaaaaaaacccccccccccccccaaacccccccccccccccccaaa
abaaccccccaaaaaaaacccccccccccccccaaaaaaaaccccccaaaaaacccccaaaacaaaaccccccccccccccccccaaaaaaaaccccccaaacaccccccccccccccccaaakccaaaccccccccccccaa
abaaacccccaaaaaaaaaccccccccccccccaaaaaaacccccccaaaaaccccccaaaccaaaaccccccccccccaacacccaaaaaccccccccaaacccccccccccccacacckkkkkkkaacccccccccccccc
abaaacccccaaaaaaaaaccccccccccccccaccaaaaaccccccaaaaaacccccaaacaaaccccccccccccccaaaaccccaaaaacccccccccccccccccccccccaaaakkkkkkkkkacccaaaccaccccc
abacacccccaaaaaaaccccccccccccccccccccaaaaaaaccccccaaccccccaaaaaaaaccccccccccccaaaaacccaaacaacccccccccccccccccccccccaajkkkkppkkkkccccaaaaaaccccc
abacccccccaaaaaaacccccccccccccccccccaaaaaaaaccccccccccccccccaaaaaaccccccccccccaaaaaacccaacccccccccccccccccccccccccccjjkkooppppkllccccaaaaaccccc
abccccccccaccaaaccccccccccccccccccccaaaaaaaacccccccccccccccccaaaaaccccccccccccacaaaacccccccccccccccccccccccccccccjjjjjjoooppppklllcacaaaaaccccc
abcccaacccccccaaacccccccccccccccccccaaaaaaacccccccccccccccccaaaaacccccccccccccccaacaccccccccccccccccccccccccccjjjjjjjjoooopuppplllcccccaaaacccc
abcccaacccccccccccccccccaaacccccccccccaaaaaaccccccaaaaacccccaaaaaccccccccccccaaacaaacccccaaaccccccccccccccccijjjjjjjjooouuuuuppllllcccccaaacccc
abaaaaaaaaccccccccccccccaaaaccccccccccaacaaaccccccaaaaaccccccccccccccccccccccaaaaaaacccccaaacacccccccccccccciijjoooooooouuuuuppplllllccccaccccc
abaaaaaaaaccccccccccccccaaaaccccccccccaacccccccccaaaaaacccccccccccccccccccccccaaaaaacccaaaaaaaacccccccccccciiiqqooooooouuuxuuuppplllllccccccccc
abccaaaaccccccccccccccccaaaccccccccccccccccccccccaaaaaacccccccccccccccccccccccaaaaaaaccaaaaaaaacccccccccccciiiqqqqtttuuuuxxxuupppqqllllmccccccc
abcaaaaacccaaaccccccccccccccccccccccccaccccccccccaaaaaacccccccccccccccccccccaaaaaaaaaaccaaaaaaccccccccccccciiiqqqtttttuuuxxxuuvpqqqqmmmmccccccc
abcaacaaaccaaacaaccccccccccccccccccccaaaacaaaccccccaacccaaaaacccccccccccccccaaaaaaaaaacccaaaaacccaaaccccccciiiqqttttxxxxxxxyuvvvvqqqqmmmmcccccc
abcacccaaccaaaaaaccccccccccccccccccccaaaaaaaacccccccccccaaaaacccccccccccccccaaacaaacccccaaaaaaccaaaacccccaaiiiqqtttxxxxxxxxyyvvvvvvqqqmmmdddccc
abcccccccaaaaaaaccccccccccccccccccccccaaaaaaaaacccccccccaaaaaaccccccccccccccccccaaaccccccaacccccaaaacccaaaaiiiqqqttxxxxxxxyyyyyyvvvqqqmmmdddccc
SbccccccccaaaaaccccccccaacaaccccccccaaaaaaaaaaccccccccccaaaaaaccccccccccccaaacccaaccccccccccccccaaaacccaaaaaiiiqqtttxxxxEzzyyyyvvvvqqqmmmdddccc
abaccccccccaaaaacccccccaaaaacccccccaaaaaaaaaaaccccccccccaaaaaaccccccccccaaaaaacccccccccccccccccccccccccaaaaaiiiqqqtttxxxyyyyyyvvvvqqqmmmdddcccc
abaacccccccaacaaaccccccaaaaaacccccccaaaaaaaaaaccccccccccccaaacccccccccccaaaaaaccccccccccccccccccccccccccaaaahhhqqqqttxxyyyyyyvvvvqqqmmmddddcccc
abaccccccccaaccccccccccaaaaaacccaacaaccaaaaaaaaaccccccccccccccccccccccccaaaaaaccccccccccccccccccccccccccaaaachhhqqtttxwyyyyyywvrqqqmmmmdddccccc
abaaaccccccccccccccccccaaaaaacccaaaaaccaaaaacaaaccccccccccccccccccccccccaaaaaccccaaaaccccaaaccccccccccccccccchhhppttwwwywwyyywwrrrnmmmdddcccccc
abaaaccccccccccccccccccccaaaccccaaaaaacaaaaaaaaaccccccccaaacccccccccccccaaaaaccccaaaaccccaaaccccccccccccccccchhpppsswwwwwwwwywwrrrnnndddccccccc
abaaacccccccccccccccccccccccccccaaaaaacccaaaaaacccccccccaaaaacccccaacccccccccccccaaaacaaaaaaaaccccccccccccccchhpppsswwwwsswwwwwrrrnneeddccccccc
abaccccccccaaaacccccccccccccccccaaaaaaccccaaaaaaaacccccaaaaaaccaacaaacccccccccccccaaccaaaaaaaaccccccccccccccchhpppssssssssrwwwwrrrnneeecaaccccc
abaccccccccaaaacccccccccccccccccccaaaccccaaaaaaaaacccccaaaaaaccaaaaaccccccccccccccccccccaaaaacccccccccccccccchhpppssssssssrrrwrrrnnneeeaaaccccc
abcccccccccaaaacccccccccccccccccccccccccaaaaaaaaaaccccccaaaaacccaaaaaacccccccccccccccccaaaaaacccccccccccccccchhpppppsssooorrrrrrrnnneeeaaaccccc
abcccccccccaaaccccccccccccccccccccccccccaaacaaacccccccccaacaacaaaaaaaacccccccccccccccccaaaaaacaaccccccccccccchhhppppppoooooorrrrnnneeeaaaaacccc
abccccccccccccccccccccccccccccccccccccccccccaaaccaaaacccccccccaaaaacaaccccaacccccccccacaaaaaacaaccccccccccccchhhgpppppoooooooonnnnneeeaaaaacccc
abcccccccaacccccccccccccccccccccccccccccccccaaacaaaaaccccccccccacaaaccccccaacccccccccaacaaaaaaaaaaacccccaaccccgggggggggggfooooonnneeeeaaaaacccc
abcccccccaaacaaccccccccccccaacccccccccccccccccccaaaaaaccccaacccccaaacccaaaaaaaaccccccaaaaacaaaaaaaaccccaaacccccggggggggggfffooonneeeecaaacccccc
abcccccccaaaaaaccccaacccccaaacccccccccccccccccccaaaaaaccccaaaccccccccccaaaaaaaacccccccaaaaaccaaaaccccaaaaaaaacccggggggggfffffffffeeeecaaccccccc
abcccccaaaaaaaccaaaaacaaaaaaacccccccccccccccccccaaaaacccccaaaacccaaccccccaaaacccccccaaaaaaaacaaaaacccaaaaaaaaccccccccccaaaffffffffecccccccccccc
abcaaacaaaaaaacccaaaaaaaaaaaaaaaccccccccccccccccccaaacccccaaaacaaaacaacccaaaaaccccccaaaaaaaaaaaaaaccccaaaaaacccccccccccaaacaafffffccccccccccaaa
abaaaacccaaaaaaccaaaaacaaaaaaaaaccccccccccccaaacccccccccccaaaaaaaaacaaccaaacaacccccccccaacccaaccaaccccaaaaaaccccccccccaaaaccaaacccccccccccccaaa
abaaaacccaacaaacaaaaacccaaaaaaacccccccccccccaaaacccccccccaaaaaaaaaaaaaccaacccacccccccccaacccccccccccccaaaaaaccccccccccaaacccccccccccccccccccaaa
abcaaacccaacccccccaaaccaaaaaacccccccccccccccaaaaccccccaaaaaaaaaaaaaaaaaaccccccccccccccccccccccccccccccaaccaaccccccccccaaaccccccccccccccccaaaaaa
abcccccccccccccccccccccaaaaaaaccccccccccccccaaacccccccaaaaaaaaaaaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa
41 changes: 41 additions & 0 deletions 2022/day_12/map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
...................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
Loading

0 comments on commit 87f1c05

Please sign in to comment.