Browse Source

Not there

Piotr Czajkowski 2 years ago
parent
commit
497357f47f
2 changed files with 42 additions and 0 deletions
  1. 35 0
      12/code.go
  2. 7 0
      12/small

+ 35 - 0
12/code.go

@@ -67,6 +67,40 @@ func part1(input map[string][]string) int {
 	return len(paths)
 }
 
+func findAllPathsPart2(start string, end string, visited map[string]int, input map[string][]string, localPath []string) {
+	if start == end {
+		paths = append(paths, localPath)
+		fmt.Println(localPath)
+		return
+	}
+
+	if unicode.IsLower(rune(start[0])) {
+		visited[start]++
+	}
+
+	for _, next := range input[start] {
+		if next != "start" && visited[next] < 2 {
+			localPath = append(localPath, next)
+			findAllPathsPart2(next, end, visited, input, localPath)
+			localPath = localPath[:len(localPath)-1]
+		}
+	}
+
+	if visited[start] > 0 {
+		visited[start]--
+	}
+}
+
+func part2(input map[string][]string) int {
+	visited := make(map[string]int)
+
+	localPath := []string{"start"}
+	paths = make([][]string, 0)
+	findAllPathsPart2("start", "end", visited, input, localPath)
+
+	return len(paths)
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("Please provide a file name as argument")
@@ -74,4 +108,5 @@ func main() {
 
 	input := readInput(os.Args[1])
 	fmt.Println("Part 1:", part1(input))
+	fmt.Println("Part 2:", part2(input))
 }

+ 7 - 0
12/small

@@ -0,0 +1,7 @@
+start-A
+start-b
+A-c
+A-b
+b-d
+A-end
+b-end