day 12a complete
This commit is contained in:
parent
616713e8ae
commit
807fddccc8
3
day12/day 12a/go.mod
Normal file
3
day12/day 12a/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module PWD
|
||||
|
||||
go 1.17
|
89
day12/day 12a/main.go
Normal file
89
day12/day 12a/main.go
Normal file
@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
content := returnContent("../input")
|
||||
//content := returnContent("testInput")
|
||||
|
||||
findEnd("", "start", *content, []string{"start"})
|
||||
fmt.Println(counter)
|
||||
|
||||
}
|
||||
|
||||
//Cave Class
|
||||
type Cave struct {
|
||||
name string
|
||||
next []string
|
||||
isBig bool
|
||||
}
|
||||
|
||||
var counter = 0
|
||||
|
||||
func findEnd(path string, cave string, caves map[string]*Cave, visited []string) {
|
||||
if cave == "end" {
|
||||
// fmt.Println(path + "," + "end")
|
||||
counter++
|
||||
return
|
||||
}
|
||||
for _, c := range (*caves[cave]).next {
|
||||
if !caves[c].isBig {
|
||||
if isIn, _ := isInSlice(c, visited); !isIn {
|
||||
findEnd(path+","+cave, c, caves, append(visited, c))
|
||||
}
|
||||
} else {
|
||||
findEnd(path+","+cave, c, caves, visited)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isInSlice(target string, slice []string) (bool, int) {
|
||||
for i, s := range slice {
|
||||
if s == target {
|
||||
return true, i
|
||||
}
|
||||
}
|
||||
return false, 0
|
||||
}
|
||||
|
||||
func returnContent(path string) *map[string]*Cave {
|
||||
//read file and return it as an array of integers
|
||||
|
||||
file, err := os.Open(path)
|
||||
content := make(map[string]*Cave)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Unlucky, the file didn't open")
|
||||
return &content
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := strings.Split(scanner.Text(), "-")
|
||||
from, to := line[0], line[1]
|
||||
parseCave(from, to, content)
|
||||
parseCave(to, from, content)
|
||||
}
|
||||
|
||||
return &content
|
||||
}
|
||||
|
||||
func parseCave(cave string, to string, caves map[string]*Cave) {
|
||||
if target, ok := caves[cave]; !ok {
|
||||
caves[cave] = &Cave{
|
||||
name: cave,
|
||||
next: []string{to},
|
||||
isBig: strings.ToUpper(cave) == cave,
|
||||
}
|
||||
} else {
|
||||
target.next = append(target.next, to)
|
||||
}
|
||||
|
||||
}
|
10
day12/day 12a/testInput
Normal file
10
day12/day 12a/testInput
Normal file
@ -0,0 +1,10 @@
|
||||
dc-end
|
||||
HN-start
|
||||
start-kj
|
||||
dc-start
|
||||
dc-HN
|
||||
LN-dc
|
||||
HN-end
|
||||
kj-sa
|
||||
kj-HN
|
||||
kj-dc
|
25
day12/input
Normal file
25
day12/input
Normal file
@ -0,0 +1,25 @@
|
||||
bm-XY
|
||||
ol-JS
|
||||
bm-im
|
||||
RD-ol
|
||||
bm-QI
|
||||
JS-ja
|
||||
im-gq
|
||||
end-im
|
||||
ja-ol
|
||||
JS-gq
|
||||
bm-AF
|
||||
RD-start
|
||||
RD-ja
|
||||
start-ol
|
||||
cj-bm
|
||||
start-JS
|
||||
AF-ol
|
||||
end-QI
|
||||
QI-gq
|
||||
ja-gq
|
||||
end-AF
|
||||
im-QI
|
||||
bm-gq
|
||||
ja-QI
|
||||
gq-RD
|
Loading…
Reference in New Issue
Block a user