Browse Source

Solved part1

Piotr Czajkowski 2 years ago
parent
commit
692a0e61ca
2 changed files with 65 additions and 5 deletions
  1. 39 5
      12/code.go
  2. 26 0
      12/input

+ 39 - 5
12/code.go

@@ -6,16 +6,17 @@ import (
 	"log"
 	"os"
 	"strings"
+	"unicode"
 )
 
-func readInput(file string) [][]string {
+func readInput(file string) map[string][]string {
 	content, err := ioutil.ReadFile(file)
 	if err != nil {
 		log.Fatal(err)
 	}
 
 	lines := strings.Split(string(content), "\n")
-	var input [][]string
+	input := make(map[string][]string)
 	for _, line := range lines {
 		if line == "" {
 			continue
@@ -26,18 +27,51 @@ func readInput(file string) [][]string {
 			log.Fatal("Invalid input")
 		}
 
-		input = append(input, []string{points[0], points[1]})
-		input = append(input, []string{points[1], points[0]})
+		input[points[0]] = append(input[points[0]], points[1])
+		input[points[1]] = append(input[points[1]], points[0])
 	}
 
 	return input
 }
 
+var paths [][]string
+
+func findAllPaths(start string, end string, visited map[string]bool, input map[string][]string, localPath []string) {
+	if start == end {
+		paths = append(paths, localPath)
+		return
+	}
+
+	if unicode.IsLower(rune(start[0])) {
+		visited[start] = true
+	}
+
+	for _, next := range input[start] {
+		if !visited[next] {
+			localPath = append(localPath, next)
+			findAllPaths(next, end, visited, input, localPath)
+			localPath = localPath[:len(localPath)-1]
+		}
+	}
+
+	if visited[start] {
+		visited[start] = false
+	}
+}
+
+func part1(input map[string][]string) int {
+	visited := make(map[string]bool)
+	localPath := []string{"start"}
+	findAllPaths("start", "end", visited, input, localPath)
+
+	return len(paths)
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("Please provide a file name as argument")
 	}
 
 	input := readInput(os.Args[1])
-	fmt.Println(input)
+	fmt.Println("Part 1:", part1(input))
 }

+ 26 - 0
12/input

@@ -0,0 +1,26 @@
+RT-start
+bp-sq
+em-bp
+end-em
+to-MW
+to-VK
+RT-bp
+start-MW
+to-hr
+sq-AR
+RT-hr
+bp-to
+hr-VK
+st-VK
+sq-end
+MW-sq
+to-RT
+em-er
+bp-hr
+MW-em
+st-bp
+to-start
+em-st
+st-end
+VK-sq
+hr-st